-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathNASA_Solar_System_Simulator.java
More file actions
107 lines (90 loc) · 3.61 KB
/
NASA_Solar_System_Simulator.java
File metadata and controls
107 lines (90 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SolarSystemSimulator extends JPanel {
private static final int SCREEN_WIDTH = 1850;
private static final int SCREEN_HEIGHT = 1850;
private static final int SUN_RADIUS = 50;
// Speed up
private static final CelestialBody[] PLANETS = {
new CelestialBody("Mercury", 10, 100, 0.2, Color.GRAY),
new CelestialBody("Venus", 15, 150, 0.15, Color.ORANGE),
new CelestialBody("Earth", 20, 200, 0.1, Color.BLUE),
new CelestialBody("Mars", 17, 250, 0.08, Color.RED),
new CelestialBody("Jupiter", 40, 350, 0.05, Color.YELLOW),
new CelestialBody("Saturn", 35, 450, 0.04, new Color(210, 180, 140)),
new CelestialBody("Uranus", 30, 550, 0.03, Color.CYAN),
new CelestialBody("Neptune", 30, 650, 0.02, Color.BLUE.darker()),
new CelestialBody("Pluto", 8, 750, 0.01, new Color(160, 82, 45))
};
private static class CelestialBody {
private String name;
private int radius;
private int orbitRadius;
private double orbitSpeed;
private Color color;
private double angle;
public CelestialBody(String name, int radius, int orbitRadius, double orbitSpeed, Color color) {
this.name = name;
this.radius = radius;
this.orbitRadius = orbitRadius;
this.orbitSpeed = orbitSpeed;
this.color = color;
this.angle = 0;
}
public void update(double dt) {
angle += orbitSpeed * dt;
}
public double getX() {
return SCREEN_WIDTH / 2 + Math.cos(angle) * orbitRadius;
}
public double getY() {
return SCREEN_HEIGHT / 2 + Math.sin(angle) * orbitRadius;
}
public void draw(Graphics2D g2d) {
double x = getX();
double y = getY();
// Draw planet
g2d.setColor(color);
g2d.fillOval((int) (x - radius), (int) (y - radius), radius * 2, radius * 2);
// Draw planet name
g2d.setColor(Color.WHITE);
Font font = new Font("Arial", Font.PLAIN, 12);
g2d.setFont(font);
g2d.drawString(name, (int) x - radius, (int) y - radius - 20);
// Draw orbit circle
g2d.setColor(color);
g2d.drawOval((int) (SCREEN_WIDTH / 2 - orbitRadius), (int) (SCREEN_HEIGHT / 2 - orbitRadius),
orbitRadius * 2, orbitRadius * 2);
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Clear the screen
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
CelestialBody sun = new CelestialBody("Sun", SUN_RADIUS, 0, 0, Color.YELLOW);
for (CelestialBody planet : PLANETS) {
planet.update(0.016);
planet.draw(g2d);
}
sun.draw(g2d);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Solar System Simulator");
SolarSystemSimulator panel = new SolarSystemSimulator();
frame.add(panel);
frame.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setResizable(false);
}
}