监听转换变化

Listen for transformation changes

有没有办法监听转换变化?我想在例如立方体转动时收到通知。我尤其对 getLocalToSceneTransform.

感兴趣

这是我的尝试:

@Override
public void start(Stage primaryStage) throws Exception {
    final Group root = new Group();
    final Scene scene = new Scene(root);

    final Box cube = new Box(1, 1, 1);
    cube.setRotationAxis(Rotate.Y_AXIS);
    cube.setMaterial(new PhongMaterial(Color.RED));
    root.getChildren().add(cube);
    cube.getLocalToSceneTransform().addEventHandler(TransformChangedEvent.TRANSFORM_CHANGED, (e) -> {
        // never get called
        System.out.println("Transformation has changed");
    });

    final Camera camera = new PerspectiveCamera(true);
    camera.setTranslateZ(-4);
    scene.setCamera(camera);

    final Timeline turnCube = new Timeline();
    turnCube.getKeyFrames().add(new KeyFrame(Duration.seconds(0), new KeyValue(cube.rotateProperty(), 0)));
    turnCube.getKeyFrames().add(new KeyFrame(Duration.seconds(5), new KeyValue(cube.rotateProperty(), 360)));
    turnCube.setCycleCount(Timeline.INDEFINITE);
    turnCube.play();

    primaryStage.setWidth(1024);
    primaryStage.setHeight(768);
    primaryStage.setScene(scene);
    primaryStage.show();
}

当立方体转动时,EventHandler 永远不会被调用。

您可以将 changeListener 添加到立方体的 localToSceneTransformProperty() 以在立方体旋转时收到通知。

cube.localToSceneTransformProperty().addListener((value, oldValue, newValue) -> {
     System.out.println("Transformation has changed");
});