gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:54:42 25 4
gpt4 key购买 nike

我有一组节点,我需要围绕不在该组中的节点旋转该组。我如何实现这一目标?

我尝试了不同的选项来添加 Pane 并使用 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

最佳答案

围绕静止太阳的旋转可以通过以下方式实现:

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

关于java - javafx如何围绕另一个节点旋转一组节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56084650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com