倾听变革的变化

时间:2015-10-23 07:16:37

标签: java javafx javafx-3d

有没有办法倾听转型变化?我希望在多维数据集转向时收到通知。特别是我对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永远不会被调用。

1 个答案:

答案 0 :(得分:0)

您可以将changeListener添加到多维数据集localToSceneTransformProperty(),以便在旋转多维数据集时收到通知。

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