import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;import java.util.Random;
public class DynamicGalaxy extends Application {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final int NUM_STARS = 200;
private static final int NUM_PLANETS = 8;private Random random = new Random();
private Star[] stars;
private Planet[] planets;public static void main(String[] args) {
launch(args);
}@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Dynamic Galaxy");Canvas canvas = new Canvas(WIDTH, HEIGHT);
GraphicsContext gc = canvas.getGraphicsContext2D();Group root = new Group();
root.getChildren().add(canvas);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();initializeStars();
initializePlanets();new AnimationTimer() {
long lastTime = System.nanoTime();@Override
public void handle(long now) {
double deltaTime = (now - lastTime) / 1e9; // 计算时间间隔(秒)
lastTime = now;update(deltaTime);
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, WIDTH, HEIGHT);drawStars(gc);
drawPlanets(gc);
}
}.start();
}private void initializeStars() {
stars = new Star[NUM_STARS];
for (int i = 0; i < NUM_STARS; i++) {
double x = random.nextDouble() * WIDTH;
double y = random.nextDouble() * HEIGHT;
double size = random.nextDouble() * 2 + 1;
stars[i] = new Star(x, y, size);
}
}private void initializePlanets() {
planets = new Planet[NUM_PLANETS];
for (int i = 0; i < NUM_PLANETS; i++) {
double x = random.nextDouble() * WIDTH;
double y = random.nextDouble() * HEIGHT;
double size = random.nextDouble() * 10 + 5;
double speed = random.nextDouble() * 60 + 30;
planets[i] = new Planet(x, y, size, speed);
}
}private void update(double deltaTime) {
for (Planet planet : planets) {
planet.update(deltaTime);
}
}private void drawStars(GraphicsContext gc) {
gc.setFill(Color.WHITE);
for (Star star : stars) {
gc.fillOval(star.getX(), star.getY(), star.getSize(), star.getSize());
}
}private void drawPlanets(GraphicsContext gc) {
gc.setFill(Color.YELLOW);
for (Planet planet : planets) {
gc.fillOval(planet.getX(), planet.getY(), planet.getSize(), planet.getSize());
}
}private class Star {
private double x;
private double y;
private double size;public Star(double x, double y, double size) {
this.x = x;
this.y = y;
this.size = size;
}public double getX() {
return x;
}public double getY() {
return y;
}public double getSize() {
return size;
}
}private class Planet {
private double x;
private double y;
private double size;
private double speed;public Planet(double x, double y, double size, double speed) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
}public double getX() {
return x;
}public double getY() {
return y;
}public double getSize() {
return size;
}public void update(double deltaTime) {
x += speed * deltaTime;
if (x > WIDTH) {
x = 0;
}
}
}
}