小小银河系(Java实现)

本文介绍了如何使用JavaFX库创建一个简单的动态星系应用程序,通过AnimationTimer实现恒星和行星的随机生成与移动,展示在画布上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值