Minggu, 17 Agustus 2025

JAVA - Animasi Bumi Bulan Mengelilingi Matahari

 




File : Orbit.java

package shapes;


import java.io.IOException;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.WindowConstants;


/**

 *

 * @author Acer

 */

public class Orbit {

           public static void main(String[] args) throws IOException, InterruptedException {

        JFrame frame = new JFrame("Orbit");

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JComponent orbitPanel = new OrbitPanel(250, 250);

        frame.add(orbitPanel);

        frame.pack();

        frame.setVisible(true);


        while (true) {

            Thread.sleep(20);

            orbitPanel.repaint();

        }

    }


==============================

File : OrbitPanel.java


/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package shapes;


import java.awt.Color;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.RenderingHints;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.geom.AffineTransform;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

import javax.swing.Timer;

import javax.swing.JComponent;

public class OrbitPanel extends JComponent {

        int width;

        int height;


        public OrbitPanel(int width, int height) {

            this.width = width;

            this.height = height;

        }


        @Override

        public Dimension getPreferredSize() {

            return new Dimension(width, height);

        }


        @Override

        public void paint(Graphics g) {

            Graphics2D g2 = (Graphics2D) g;

            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);


            // Clear the background.

            g2.setColor(getBackground());

            g2.fillRect(0, 0, getWidth(), getHeight());


            // Sun transform. Just centre it in the window.

            AffineTransform sunTx = AffineTransform.getTranslateInstance(getWidth() / 2, getHeight() / 2);


            // Draw the sun

            g2.setTransform(sunTx);

            drawBody(g2, 30, Color.YELLOW);


            // Orbital period.

            // One rotation every 10s.

            double percentRotation = System.currentTimeMillis() % 10000 / 10000.0;

            // To radians.

            double angle = Math.PI * 2 * percentRotation;


            // Earth transform.

            // Set the orbital radius to 1/3rd the panel width

            AffineTransform earthTx = AffineTransform.getTranslateInstance(getWidth() / 3, 0);

            // Rotate

            earthTx.preConcatenate(AffineTransform.getRotateInstance(angle));

            // Add the sun transform

            earthTx.preConcatenate(sunTx);


            // Draw the earth

            g2.setTransform(earthTx);

            drawBody(g2, 10, Color.BLUE);


            // Moon transform.

            // Set the orbital radius to 1/10th the panel width

            AffineTransform moonTx = AffineTransform.getTranslateInstance(getWidth() / 10, 0);

            // Rotate

            moonTx.preConcatenate(AffineTransform.getRotateInstance(angle));

            // Add the earth transform (already includes the sun transform)

            moonTx.preConcatenate(earthTx);


            // Draw the moon

            g2.setTransform(moonTx);

            drawBody(g2, 5, Color.DARK_GRAY);

        }


        private void drawBody(Graphics2D g2, int size, Color color) {

            g2.setColor(color);

            g2.fillOval(-size / 2, -size / 2, size, size);

        }

    }



Tidak ada komentar: