-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy path3D_Solar_System_Viewer.java
78 lines (67 loc) · 2.5 KB
/
3D_Solar_System_Viewer.java
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
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class SolarSystem extends Application {
private static final double AU_SCALE = 50;
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private Group root;
private Sphere sun;
private Sphere[] planets;
private Text[] labels;
@Override
public void start(Stage primaryStage) {
root = new Group();
Scene scene = new Scene(root, WIDTH, HEIGHT, Color.BLACK);
primaryStage.setScene(scene);
primaryStage.setTitle("Solar System");
primaryStage.show();
createSun();
createPlanets();
createLabels();
root.getChildren().addAll(sun);
root.getChildren().addAll(planets);
root.getChildren().addAll(labels);
}
private void createSun() {
sun = new Sphere(30);
sun.setTranslateX(WIDTH / 2);
sun.setTranslateY(HEIGHT / 2);
sun.setTranslateZ(0);
sun.setMaterial(new PhongMaterial(Color.YELLOW));
}
private void createPlanets() {
String[] planetNames = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
double[] planetDistances = {0.39, 0.72, 1.0, 1.52, 5.20, 9.58, 19.18, 30.07};
planets = new Sphere[planetNames.length];
for (int i = 0; i < planetNames.length; i++) {
double radius = 5;
Sphere planet = new Sphere(radius);
planet.setTranslateX(WIDTH / 2 + planetDistances[i] * AU_SCALE);
planet.setTranslateY(HEIGHT / 2);
planet.setTranslateZ(0);
planet.setMaterial(new PhongMaterial(Color.BLUE));
planets[i] = planet;
}
}
private void createLabels() {
String[] planetNames = {"Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
labels = new Text[planetNames.length];
for (int i = 0; i < planetNames.length; i++) {
Text label = new Text(planetNames[i]);
label.setTranslateX(WIDTH / 2 + 30 + i * 80);
label.setTranslateY(HEIGHT - 20);
label.setFill(Color.WHITE);
label.setFont(Font.font("Arial", 12));
labels[i] = label;
}
}
public static void main(String[] args) {
launch(args);
}
}