如何围绕另一个节点javafx旋转一组节点

时间:2019-05-10 20:46:35

标签: java javafx 3d

我有一组节点,我需要围绕不在该组中的节点旋转该组。我是怎么做到的?

我尝试了添加窗格的其他选项,并使用PathTransition进行了播放,但我的尝试均无效

var objects = new Group();
objects.getChildren().addAll(element1, element2, andSoOn);

var Earth = new Sphere(10);
//i need the group to rotate (orbit) around this node
var movement = new PathTransition(new Duration(360_00), new Circle(150), Earth);
movement.play();//Earth is rotating around the center of the project. this part works fine

当我将组(对象)设置为PathTransition的节点时,它会编译,但是抛出RuntimeException

1 个答案:

答案 0 :(得分:1)

可以实现围绕静止太阳的旋转:

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Orbit extends Application {

    private static final double WIDTH = 500, HEIGHT = 400, EARTH_RADIUS = 150;
    private Rotate earthRotate;
    private Circle earth;

    @Override
    public void start(Stage stage) {

        stage.setTitle("Rotation transformation example");

        var sun = new Sphere(20);
        sun.setTranslateX(WIDTH/2);   sun.setTranslateY(HEIGHT/2);

        earth = new Circle(10);
        earth.translateXProperty().bind(sun.translateXProperty());
        earth.translateYProperty().bind(sun.translateYProperty().subtract(EARTH_RADIUS));
        earthRotate = new Rotate(0, 0, EARTH_RADIUS);
        earth.getTransforms().add(earthRotate);

        Pane root = new Pane(sun, earth);
        Scene scene = new Scene(root, WIDTH, HEIGHT);
        stage.setScene(scene);
        stage.show();
        animate();
    }

    private void animate() {

        Timeline earthTimeline = new Timeline(
                new KeyFrame(Duration.ZERO, new KeyValue(earthRotate.angleProperty(), 0)),
                new KeyFrame(Duration.seconds(5), new KeyValue(earthRotate.angleProperty(), 360))
         );
        earthTimeline.setCycleCount(Timeline.INDEFINITE);
        earthTimeline.play();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here