当我将光标移到 2D 对象上时,移动的 3D 对象将在 JavaFX 中冻结

When I move the cursor over a 2D stuff the moving 3D objects will freeze in JavaFX

我正在使用 Scene 在 3D 事物的 SubScene 上显示 2D 事物。

我正在使用计时器移动立方体。

问题是当我将光标移动到 2D 对象上时,移动的 3D 框会冻结。

这是关于该问题的视频:https://drive.google.com/file/d/1Gix2uUBCFNnTxXUtyMsv9MUtQsnx0aLG/view?usp=sharing

代码如下:

package application;
    
import java.util.Timer;
import java.util.TimerTask;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;

public class Main extends Application {
    public static TimerTask timertask1;
    public static Timer timer = new Timer();
    public static Box b1 = new Box(5,5,5);
    @Override public void start(Stage primaryStage) {
        AnchorPane globalRoot = new AnchorPane();
        Scene scene = new Scene(globalRoot, 1366, 768, true);
        PerspectiveCamera camera = new PerspectiveCamera(true);
        Group root3D = new Group();
        SubScene sub = new SubScene(root3D,1366,768,false,SceneAntialiasing.BALANCED);
        camera.getTransforms().addAll(new Rotate(30, Rotate.X_AXIS), new Translate(0, 0, -80));
        sub.setCamera(camera);
        sub.setFill(Color.BLACK);
        globalRoot.getChildren().add(sub);
        root3D.getChildren().add(b1);
        globalRoot.getChildren().add(new Button("Just a button"));
        primaryStage.setFullScreen(true);
        primaryStage.setFullScreenExitHint("");
        primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
        primaryStage.setScene(scene);
        primaryStage.show();
       startTimer();
    }
     public static void startTimer() { 
            timertask1 = new TimerTask() {
                @Override public void run() {               
                    b1.setTranslateX(b1.getTranslateX()+0.1);                   
                }}; timer.scheduleAtFixedRate(timertask1, 0, 10);}
    public static void main(String[] args) {
        launch(args);
    }
}

我的错误在哪里?

如前所述, your timer alters the scene from another thread. Instead, use a Timeline, as shown here。下面的变体将 Box 从左上角的 SubScene 原点移动到右下角,同时还围绕 Y_AXIS 旋转它。滚动鼠标来回移动相机。

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.control.Button;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * 
 */
public class Main extends Application {

    private static final int WIDTH = 640;
    private static final int HEIGHT = 480;
    private final Box b1 = new Box(100, 100, 100);
    private final Rotate r = new Rotate(0, Rotate.Y_AXIS);

    @Override
    public void start(Stage primaryStage) {
        AnchorPane globalRoot = new AnchorPane();
        Scene scene = new Scene(globalRoot, WIDTH, HEIGHT);
        PerspectiveCamera camera = new PerspectiveCamera();
        camera.setTranslateZ(-100);
        Group root3D = new Group();
        SubScene sub = new SubScene(root3D, WIDTH, HEIGHT);
        sub.setCamera(camera);
        sub.setFill(Color.BLACK);
        b1.getTransforms().add(r);
        root3D.getChildren().add(b1);
        globalRoot.getChildren().add(sub);
        globalRoot.getChildren().add(new Button("Just a button"));
        primaryStage.setScene(scene);
        scene.setOnScroll((final ScrollEvent e) -> {
            camera.setTranslateZ(camera.getTranslateZ() + e.getDeltaY());
        });
        primaryStage.show();
        Animation animation = createTimeline(new Point3D(b1.getTranslateX(),
            b1.getTranslateY(), b1.getTranslateZ()), new Point3D(WIDTH, HEIGHT, 0));
        animation.play();
    }

    public static void startTimer() {
    }

    private Timeline createTimeline(Point3D p1, Point3D p2) {
        Timeline t = new Timeline();
        t.setCycleCount(Timeline.INDEFINITE);
        t.setAutoReverse(true);
        KeyValue keyX = new KeyValue(b1.translateXProperty(), p2.getX() - p1.getX());
        KeyValue keyY = new KeyValue(b1.translateYProperty(), p2.getY() - p1.getY());
        KeyValue keyR = new KeyValue(r.angleProperty(), 2 * 360);
        KeyFrame keyFrame = new KeyFrame(Duration.seconds(1), keyX, keyY, keyR);
        t.getKeyFrames().add(keyFrame);
        return t;
    }

    public static void main(String[] args) {
        launch(args);
    }
}