我有一组节点,我需要围绕不在该组中的节点旋转该组。我是如何做到这一点的?
我尝试了不同的选项来添加窗格并使用 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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句