JavaFX_2D动画_如何让一个圆旋转另一个也在旋转的圆

问题描述 投票:0回答:1

我只想制作一个月球旋转地球旋转太阳的2D动画。 我可以看到一些太阳系代码,但它们太复杂,对我来说很难阅读。 谁能帮我解决这个问题吗?

一个简单的 JavaFX 2D 动画代码,使 一个物体旋转另一个物体,另一个物体也在旋转另一个物体。

java animation javafx 2d
1个回答
0
投票

答案的改编:

上述答案中的地球被地球系统取代。地球系统由地球、月球和月球轨道组成。地球系统存储在

Group
.

在地球系统中添加地球系统总尺寸(月球轨道+月球宽度)的透明背景,使系统在月球绕地球运行时保持恒定尺寸。

地球系统绕太阳运行。月球将围绕地球运行。

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SolApp extends Application {
    private static final double
            S = 400,             // extent of the solar system
            C = S / 2,           // center of the solar system
            SR = S / 10, ER = S / 40, LR = S / 80, // sun/earth/moon radius
            O = S * 3/8, LO = S / 20, // sun/moon orbit radius
            ES = (LO + LR) * 2,  // extent of the earth system
            EC = ES / 2;         // center of the earth system
    private static final Duration T = Duration.seconds(15);
    private static final Color INDIA_INK = Color.web("#3c3f4a");

    @Override
    public void start(Stage stage) {
        Circle sun = new Circle(
                C, C,
                SR,
                Color.ORANGERED
        );

        Circle earth = new Circle(
                EC, EC,
                ER,
                Color.PALETURQUOISE
        );

        Circle moon = new Circle(
                EC + LO, EC,
                LR,
                Color.SILVER
        );

        Circle lunarOrbit = new Circle(EC, EC, LO);
        lunarOrbit.setFill(null);
        lunarOrbit.setStroke(Color.GRAY);
        lunarOrbit.setStrokeWidth(1);

        Circle earthOrbit = new Circle(C, C, O);
        earthOrbit.setFill(null);
        earthOrbit.setStroke(Color.GREEN);
        earthOrbit.setStrokeWidth(2);

        Rectangle earthSystemExtent = new Rectangle(ES, ES, Color.TRANSPARENT);
        Group earthSystem = new Group(earthSystemExtent, earth, lunarOrbit, moon);
        earthSystem.setTranslateX(C + O - EC);
        earthSystem.setTranslateY(C - EC);

        Pane solarSystem = new Pane(sun, earthOrbit, earthSystem);
        solarSystem.setPrefSize(S, S);
        solarSystem.setBackground(Background.fill(INDIA_INK));

        Animation earthOrbiter = getPathTransitionOrbiter(
                lunarOrbit,
                moon
        );
        earthOrbiter.setRate(12);

        Animation solarOrbiter = getPathTransitionOrbiter(
                earthOrbit,
                earthSystem
        );

        stage.setScene(new Scene(solarSystem));
        stage.show();

        solarOrbiter.play();
        earthOrbiter.play();
    }

    private static Transition getPathTransitionOrbiter(
            Circle orbit,
            Node satellite
    ) {
        PathTransition orbiter = new PathTransition(T, orbit, satellite);
        
        orbiter.setInterpolator(Interpolator.LINEAR);
        orbiter.setCycleCount(Animation.INDEFINITE);
        
        return orbiter;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.