|
| 1 | +import javafx.animation.AnimationTimer; |
| 2 | +import javafx.application.Application; |
| 3 | +import javafx.scene.Group; |
| 4 | +import javafx.scene.Scene; |
| 5 | +import javafx.scene.canvas.Canvas; |
| 6 | +import javafx.scene.canvas.GraphicsContext; |
| 7 | +import javafx.scene.paint.Color; |
| 8 | +import javafx.scene.text.Font; |
| 9 | +import javafx.scene.text.TextAlignment; |
| 10 | +import javafx.stage.Stage; |
| 11 | + |
| 12 | +public class CelestialBodies extends Application { |
| 13 | + private static final int SCREEN_WIDTH = 1900; |
| 14 | + private static final int SCREEN_HEIGHT = 1900; |
| 15 | + |
| 16 | + private static final int SUN_RADIUS = 50; |
| 17 | + |
| 18 | + private static final CelestialBody[] PLANETS = { |
| 19 | + new CelestialBody("Mercury", 10, 100, 0.02, Color.rgb(128, 128, 128)), |
| 20 | + new CelestialBody("Venus", 15, 150, 0.015, Color.rgb(255, 165, 0)), |
| 21 | + new CelestialBody("Earth", 20, 200, 0.01, Color.rgb(0, 0, 255)), |
| 22 | + new CelestialBody("Mars", 17, 250, 0.008, Color.rgb(255, 0, 0)), |
| 23 | + new CelestialBody("Jupiter", 40, 350, 0.005, Color.rgb(255, 215, 0)), |
| 24 | + new CelestialBody("Saturn", 35, 450, 0.004, Color.rgb(210, 180, 140)), |
| 25 | + new CelestialBody("Uranus", 30, 550, 0.003, Color.rgb(0, 255, 255)), |
| 26 | + new CelestialBody("Neptune", 30, 650, 0.002, Color.rgb(0, 0, 139)), |
| 27 | + new CelestialBody("Pluto", 8, 750, 0.001, Color.rgb(165, 42, 42)) |
| 28 | + }; |
| 29 | + |
| 30 | + private Group root; |
| 31 | + |
| 32 | + @Override |
| 33 | + public void start(Stage primaryStage) { |
| 34 | + root = new Group(); |
| 35 | + Scene scene = new Scene(root, SCREEN_WIDTH, SCREEN_HEIGHT, Color.BLACK); |
| 36 | + primaryStage.setScene(scene); |
| 37 | + primaryStage.setTitle("Celestial Bodies"); |
| 38 | + primaryStage.show(); |
| 39 | + |
| 40 | + createCelestialBodies(); |
| 41 | + } |
| 42 | + |
| 43 | + private void createCelestialBodies() { |
| 44 | + Canvas canvas = new Canvas(SCREEN_WIDTH, SCREEN_HEIGHT); |
| 45 | + root.getChildren().add(canvas); |
| 46 | + |
| 47 | + GraphicsContext gc = canvas.getGraphicsContext2D(); |
| 48 | + gc.setTextAlign(TextAlignment.CENTER); |
| 49 | + gc.setFont(Font.font("Arial", 16)); |
| 50 | + |
| 51 | + AnimationTimer animationTimer = new AnimationTimer() { |
| 52 | + @Override |
| 53 | + public void handle(long now) { |
| 54 | + gc.clearRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); |
| 55 | + |
| 56 | + for (CelestialBody planet : PLANETS) { |
| 57 | + planet.update(); |
| 58 | + drawCelestialBody(gc, planet); |
| 59 | + } |
| 60 | + |
| 61 | + drawCelestialBody(gc, SUN); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + animationTimer.start(); |
| 66 | + } |
| 67 | + |
| 68 | + private void drawCelestialBody(GraphicsContext gc, CelestialBody body) { |
| 69 | + double x = body.getX(); |
| 70 | + double y = body.getY(); |
| 71 | + |
| 72 | + // Perform scientific calculations for each planet |
| 73 | + double volume = body.calculateVolume(); |
| 74 | + double surfaceArea = body.calculateSurfaceArea(); |
| 75 | + double orbitalVelocity = body.calculateOrbitalVelocity(); |
| 76 | + |
| 77 | + // Print calculations to console |
| 78 | + System.out.println("Scientific Calculations:"); |
| 79 | + System.out.println("Planet: " + body.getName()); |
| 80 | + System.out.println("Volume: " + String.format("%.2f", volume)); |
| 81 | + System.out.println("Surface Area: " + String.format("%.2f", surfaceArea)); |
| 82 | + System.out.println("Orbital Velocity: " + String.format("%.2f", orbitalVelocity)); |
| 83 | + System.out.println("------------------------"); |
| 84 | + |
| 85 | + // Render the calculations as text on the screen |
| 86 | + gc.setFill(Color.WHITE); |
| 87 | + gc.fillText("Volume: " + String.format("%.2f", volume), x, y + body.getRadius() + 20); |
| 88 | + gc.fillText("Surface Area: " + String.format("%.2f", surfaceArea), x, y + body.getRadius() + 40); |
| 89 | + gc.fillText("Orbital Velocity: " + String.format("%.2f", orbitalVelocity), x, y + body.getRadius() + 60); |
| 90 | + |
| 91 | + // Draw the body |
| 92 | + gc.setFill(body.getColor()); |
| 93 | + gc.fillOval(x - body.getRadius(), y - body.getRadius(), body.getRadius() * 2, body.getRadius() * 2); |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String[] args) { |
| 97 | + launch(args); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class CelestialBody { |
| 102 | + private String name; |
| 103 | + private double radius; |
| 104 | + private double orbitRadius; |
| 105 | + private double orbitSpeed; |
| 106 | + private Color color; |
| 107 | + private double angle; |
| 108 | + |
| 109 | + public CelestialBody(String name, double radius, double orbitRadius, double orbitSpeed, Color color) { |
| 110 | + this.name = name; |
| 111 | + this.radius = radius; |
| 112 | + this.orbitRadius = orbitRadius; |
| 113 | + this.orbitSpeed = orbitSpeed; |
| 114 | + this.color = color; |
| 115 | + this.angle = 0; |
| 116 | + } |
| 117 | + |
| 118 | + public void update() { |
| 119 | + angle += orbitSpeed; |
| 120 | + } |
| 121 | + |
| 122 | + public double getX() { |
| 123 | + return SCREEN_WIDTH / 2 + Math.cos(angle) * orbitRadius; |
| 124 | + } |
| 125 | + |
| 126 | + public double getY() { |
| 127 | + return SCREEN_HEIGHT / 2 + Math.sin(angle) * orbitRadius; |
| 128 | + } |
| 129 | + |
| 130 | + public double getRadius() { |
| 131 | + return radius; |
| 132 | + } |
| 133 | + |
| 134 | + public String getName() { |
| 135 | + return name; |
| 136 | + } |
| 137 | + |
| 138 | + public Color getColor() { |
| 139 | + return color; |
| 140 | + } |
| 141 | + |
| 142 | + public double calculateVolume() { |
| 143 | + double volume = (4 / 3) * Math.PI * Math.pow(radius, 3); |
| 144 | + return volume; |
| 145 | + } |
| 146 | + |
| 147 | + public double calculateSurfaceArea() { |
| 148 | + double surfaceArea = 4 * Math.PI * Math.pow(radius, 2); |
| 149 | + return surfaceArea; |
| 150 | + } |
| 151 | + |
| 152 | + public double calculateOrbitalVelocity() { |
| 153 | + if (orbitSpeed == 0) { |
| 154 | + return Double.POSITIVE_INFINITY; |
| 155 | + } |
| 156 | + double orbitalVelocity = 2 * Math.PI * orbitRadius / orbitSpeed; |
| 157 | + return orbitalVelocity; |
| 158 | + } |
| 159 | +} |
0 commit comments