|
| 1 | +import java.awt.Color; |
| 2 | +import java.awt.Font; |
| 3 | +import java.awt.Graphics; |
| 4 | +import java.awt.Graphics2D; |
| 5 | +import java.awt.RenderingHints; |
| 6 | +import java.awt.event.WindowAdapter; |
| 7 | +import java.awt.event.WindowEvent; |
| 8 | +import java.awt.geom.Ellipse2D; |
| 9 | +import java.util.ArrayList; |
| 10 | +import javax.swing.JFrame; |
| 11 | +import javax.swing.JPanel; |
| 12 | + |
| 13 | +class CelestialBody { |
| 14 | + private String name; |
| 15 | + private int radius; |
| 16 | + private int orbitRadius; |
| 17 | + private double orbitSpeed; |
| 18 | + private Color color; |
| 19 | + private double angle; |
| 20 | + |
| 21 | + public CelestialBody(String name, int radius, int orbitRadius, double orbitSpeed, Color color) { |
| 22 | + this.name = name; |
| 23 | + this.radius = radius; |
| 24 | + this.orbitRadius = orbitRadius; |
| 25 | + this.orbitSpeed = orbitSpeed; |
| 26 | + this.color = color; |
| 27 | + this.angle = 0; |
| 28 | + } |
| 29 | + |
| 30 | + public void update(double dt) { |
| 31 | + angle += orbitSpeed * dt; |
| 32 | + } |
| 33 | + |
| 34 | + public double getX() { |
| 35 | + return Main.SCREEN_WIDTH / 2 + Math.cos(angle) * orbitRadius; |
| 36 | + } |
| 37 | + |
| 38 | + public double getY() { |
| 39 | + return Main.SCREEN_HEIGHT / 2 + Math.sin(angle) * orbitRadius; |
| 40 | + } |
| 41 | + |
| 42 | + public double getVolume() { |
| 43 | + return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3); |
| 44 | + } |
| 45 | + |
| 46 | + public double getSurfaceArea() { |
| 47 | + return 4.0 * Math.PI * Math.pow(radius, 2); |
| 48 | + } |
| 49 | + |
| 50 | + public double getOrbitalVelocity() { |
| 51 | + if (orbitSpeed == 0) { |
| 52 | + return Double.POSITIVE_INFINITY; |
| 53 | + } |
| 54 | + return 2.0 * Math.PI * orbitRadius / orbitSpeed; |
| 55 | + } |
| 56 | + |
| 57 | + public void draw(Graphics2D g2) { |
| 58 | + double x = getX(); |
| 59 | + double y = getY(); |
| 60 | + |
| 61 | + // Display the name |
| 62 | + Font font = new Font("Arial", Font.PLAIN, 16); |
| 63 | + g2.setFont(font); |
| 64 | + g2.setColor(Color.WHITE); |
| 65 | + g2.drawString(name, (int) x, (int) y + radius + 20); |
| 66 | + |
| 67 | + // Perform scientific calculations for each planet |
| 68 | + double volume = getVolume(); |
| 69 | + double surfaceArea = getSurfaceArea(); |
| 70 | + double orbitalVelocity = getOrbitalVelocity(); |
| 71 | + |
| 72 | + // Display the calculations on console |
| 73 | + System.out.println("Planet: " + name); |
| 74 | + System.out.printf("Volume: %.2f%n", volume); |
| 75 | + System.out.printf("Surface Area: %.2f%n", surfaceArea); |
| 76 | + System.out.printf("Orbital Velocity: %.2f%n", orbitalVelocity); |
| 77 | + System.out.println("-------------------"); |
| 78 | + |
| 79 | + // Draw the body |
| 80 | + Ellipse2D.Double planetShape = new Ellipse2D.Double(x - radius, y - radius, radius * 2, radius * 2); |
| 81 | + g2.setColor(color); |
| 82 | + g2.fill(planetShape); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class DrawingPanel extends JPanel { |
| 87 | + private ArrayList<CelestialBody> planets; |
| 88 | + private CelestialBody sun; |
| 89 | + |
| 90 | + public DrawingPanel(ArrayList<CelestialBody> planets, CelestialBody sun) { |
| 91 | + this.planets = planets; |
| 92 | + this.sun = sun; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected void paintComponent(Graphics g) { |
| 97 | + super.paintComponent(g); |
| 98 | + Graphics2D g2 = (Graphics2D) g; |
| 99 | + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 100 | + |
| 101 | + for (CelestialBody planet : planets) { |
| 102 | + planet.update(1.0 / 50.0); |
| 103 | + planet.draw(g2); |
| 104 | + } |
| 105 | + |
| 106 | + sun.draw(g2); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +public class Main extends JFrame { |
| 111 | + public static final int SCREEN_WIDTH = 1850; |
| 112 | + public static final int SCREEN_HEIGHT = 1850; |
| 113 | + public static final int SUN_RADIUS = 50; |
| 114 | + |
| 115 | + public static final Object[][] PLANETS = { |
| 116 | + {"Mercury", 10, 100, 0.02, new Color(128, 128, 128)}, |
| 117 | + {"Venus", 15, 150, 0.015, new Color(255, 165, 0)}, |
| 118 | + {"Earth", 20, 200, 0.01, new Color(0, 0, 255)}, |
| 119 | + {"Mars", 17, 250, 0.008, new Color(255, 0, 0)}, |
| 120 | + {"Jupiter", 40, 350, 0.005, new Color(255, 215, 0)}, |
| 121 | + {"Saturn", 35, 450, 0.004, new Color(210, 180, 140)}, |
| 122 | + {"Uranus", 30, 550, 0.003, new Color(0, 255, 255)}, |
| 123 | + {"Neptune", 30, 650, 0.002, new Color(0, 0, 139)}, |
| 124 | + {"Pluto", 8, 750, 0.001, new Color(165, 42, 42)} |
| 125 | + }; |
| 126 | + |
| 127 | + public Main() { |
| 128 | + setTitle("Planetary System"); |
| 129 | + setSize(SCREEN_WIDTH, SCREEN_HEIGHT); |
| 130 | + setResizable(false); |
| 131 | + setLocationRelativeTo(null); |
| 132 | + |
| 133 | + ArrayList<CelestialBody> planets = new ArrayList<>(); |
| 134 | + for (Object[] planetData : PLANETS) { |
| 135 | + String name = (String) planetData[0]; |
| 136 | + int radius = (int) planetData[1]; |
| 137 | + int orbitRadius = (int) planetData[2]; |
| 138 | + double orbitSpeed = (double) planetData[3]; |
| 139 | + Color color = (Color) planetData[4]; |
| 140 | + CelestialBody planet = new CelestialBody(name, radius, orbitRadius, orbitSpeed, color); |
| 141 | + planets.add(planet); |
| 142 | + } |
| 143 | + |
| 144 | + CelestialBody sun = new CelestialBody("Sun", SUN_RADIUS, 0, 0, new Color(255, 255, 0)); |
| 145 | + DrawingPanel panel = new DrawingPanel(planets, sun); |
| 146 | + add(panel); |
| 147 | + |
| 148 | + addWindowListener(new WindowAdapter() { |
| 149 | + @Override |
| 150 | + public void windowClosing(WindowEvent e) { |
| 151 | + System.exit(0); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + public static void main(String[] args) { |
| 157 | + Main main = new Main(); |
| 158 | + main.setVisible(true); |
| 159 | + } |
| 160 | +} |
0 commit comments