0

有没有办法监听转型变化?例如,当立方体转动时,我想得到通知。特别是我对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();
}

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

4

1 回答 1

0

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

cube.localToSceneTransformProperty().addListener((value, oldValue, newValue) -> {
     System.out.println("Transformation has changed");
});
于 2015-10-23T07:56:58.810 回答