-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolarSystem.java
More file actions
200 lines (161 loc) · 6.61 KB
/
Copy pathsolarSystem.java
File metadata and controls
200 lines (161 loc) · 6.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Import JOGL and Java Swing classes
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.glu.GLU;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Random;
public class SolarSystem implements GLEventListener {
private float angle = 0.0f; // Used to animate planetary rotation
private final GLU glu = new GLU();
private final ArrayList<Star> stars = new ArrayList<>();
private long lastStarUpdateTime = System.currentTimeMillis();
private final long starUpdateInterval = 5000; // Update stars every 5 seconds
private final Random rand = new Random(); // For generating random star positions
private static class Star {
float x, y, brightness;
Star(float x, float y, float brightness) {
this.x = x;
this.y = y;
this.brightness = brightness;
}
}
public static void main(String[] args) {
java.awt.Toolkit.getDefaultToolkit();
if (java.awt.GraphicsEnvironment.isHeadless()) {
System.err.println("ERROR: Running in a headless environment!");
System.exit(1);
}
GLProfile profile;
try {
profile = GLProfile.get(GLProfile.GL2);
} catch (GLException e) {
System.err.println("GL2 profile not available.");
return;
}
GLCapabilities capabilities = new GLCapabilities(profile);
GLCanvas canvas = new GLCanvas(capabilities);
SolarSystem solarSystem = new SolarSystem();
canvas.addGLEventListener(solarSystem);
canvas.setSize(800, 600);
JFrame frame = new JFrame("Enhanced JOGL Solar System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(canvas);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
FPSAnimator animator = new FPSAnimator(canvas, 60, true);
animator.start();
}
@Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClearColor(0f, 0f, 0.05f, 1f);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glEnable(GL2.GL_POINT_SMOOTH);
generateStars();
}
@Override
public void dispose(GLAutoDrawable drawable) {}
@Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
glu.gluLookAt(0, 0, 3.0, 0, 0, 0, 0, 1, 0);
// Update stars every few seconds
long currentTime = System.currentTimeMillis();
if (currentTime - lastStarUpdateTime >= starUpdateInterval) {
generateStars();
lastStarUpdateTime = currentTime;
}
drawStars(gl);
// Sun with glowing effect
drawCircle(gl, 0f, 0f, 0.25f, new float[]{1f, 0.5f, 0.0f});
drawCircle(gl, 0f, 0f, 0.2f, new float[]{1f, 0.8f, 0.0f});
// Mercury
drawOrbit(gl, 0.4f);
drawPlanet(gl, 0.4f, angle * 2.0f, 0.05f, new float[]{0.7f, 0.7f, 0.7f});
// Venus
drawOrbit(gl, 0.55f);
drawPlanet(gl, 0.55f, angle * 1.5f, 0.06f, new float[]{1f, 0.8f, 0.6f});
// Earth and Moon
drawOrbit(gl, 0.7f);
float earthX = (float) Math.cos(Math.toRadians(angle)) * 0.7f;
float earthY = (float) Math.sin(Math.toRadians(angle)) * 0.7f;
drawCircle(gl, earthX, earthY, 0.07f, new float[]{0.2f, 0.5f, 1f});
// Moon revolving around Earth
float moonAngle = angle * 5f;
float moonX = earthX + (float) Math.cos(Math.toRadians(moonAngle)) * 0.1f;
float moonY = earthY + (float) Math.sin(Math.toRadians(moonAngle)) * 0.1f;
drawCircle(gl, moonX, moonY, 0.02f, new float[]{0.9f, 0.9f, 0.9f});
// Mars
drawOrbit(gl, 1.0f);
drawPlanet(gl, 1.0f, -angle * 0.7f, 0.06f, new float[]{1f, 0.3f, 0.2f});
angle += 0.5f;
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL2 gl = drawable.getGL().getGL2();
if (height == 0) height = 1;
float aspect = (float) width / height;
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glu.gluPerspective(45.0f, aspect, 1.0, 10.0);
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();
}
private void drawCircle(GL2 gl, float cx, float cy, float radius, float[] color) {
int numSegments = 100;
gl.glColor3fv(color, 0);
gl.glBegin(GL2.GL_TRIANGLE_FAN);
gl.glVertex2f(cx, cy); // Center
for (int i = 0; i <= numSegments; i++) {
double angle = 2 * Math.PI * i / numSegments;
float x = cx + (float) Math.cos(angle) * radius;
float y = cy + (float) Math.sin(angle) * radius;
gl.glVertex2f(x, y);
}
gl.glEnd();
}
// Draws a planet at its orbit radius and current angle
private void drawPlanet(GL2 gl, float orbitRadius, float angleDeg, float size, float[] color) {
double rad = Math.toRadians(angleDeg);
float x = (float) Math.cos(rad) * orbitRadius;
float y = (float) Math.sin(rad) * orbitRadius;
drawCircle(gl, x, y, size, color);
}
// Draws an orbit ring
private void drawOrbit(GL2 gl, float radius) {
gl.glColor3f(0.5f, 0.5f, 0.5f);
gl.glBegin(GL2.GL_LINE_LOOP);
for (int i = 0; i < 100; i++) {
double angle = 2 * Math.PI * i / 100;
float x = (float) Math.cos(angle) * radius;
float y = (float) Math.sin(angle) * radius;
gl.glVertex2f(x, y);
}
gl.glEnd();
}
// Renders all stars in the background
private void drawStars(GL2 gl) {
gl.glPointSize(1.5f);
gl.glBegin(GL2.GL_POINTS);
for (Star star : stars) {
gl.glColor3f(star.brightness, star.brightness, star.brightness);
gl.glVertex2f(star.x, star.y);
}
gl.glEnd();
}
// Randomly generates star positions and brightness
private void generateStars() {
stars.clear();
for (int i = 0; i < 50; i++) {
float x = rand.nextFloat() * 3.0f - 1.5f; // Random X in range [-1.5, 1.5]
float y = rand.nextFloat() * 3.0f - 1.5f; // Random Y in range [-1.5, 1.5]
float brightness = rand.nextFloat() * 0.5f + 0.5f; // Brightness [0.5, 1.0]
stars.add(new Star(x, y, brightness));
}
}
}