【问题标题】:When I move the cursor over a 2D stuff the moving 3D objects will freeze in JavaFX当我将光标移动到 2D 东西上时,移动的 3D 对象将在 JavaFX 中冻结
【发布时间】:2021-09-12 17:02:50
【问题描述】:

我使用 Scene 来显示 2D 内容,而 SubScene 则用于 3D 内容。

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

问题是当我将光标移动到 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);
    }
}

我的错误在哪里?

【问题讨论】:

  • 不得从 fx 应用程序线程更改活动场景图中的节点!一定要使用 fx 并发支持(不要使用 java util thread Support)
  • 改为使用Timeline ,例如this

标签: javafx 3d


【解决方案1】:

here 所述,您的计时器incorrecly 会从另一个线程更改场景。而是使用Timeline,如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;

/**
 * https://stackoverflow.com/q/69153580/230513
 */
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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2012-04-18
    • 2011-01-24
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多