-1

I'm using Scene for showing 2D stuff over a SubScene for 3D things.

I'm moving the cube using a timer.

The problem is when I move the cursor to over a 2D stuff the moving 3D box will freeze.

Here's a video about the problem: https://drive.google.com/file/d/1Gix2uUBCFNnTxXUtyMsv9MUtQsnx0aLG/view?usp=sharing

And here's the code:

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);
    }
}

Where is my mistake?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
tommY
  • 109
  • 4
  • 1
    you __must not__ change a node that is in an active scenegraph off the fx application thread! Do use fx concurrency support (do not use java util thread Support) – kleopatra Sep 12 '21 at 20:34
  • 1
    Instead, use a `Timeline `, like [this](https://stackoverflow.com/a/37516327/230513). – trashgod Sep 12 '21 at 21:47

1 Answers1

3

As noted here, your timer incorrecly alters the scene from another thread. Instead, use a Timeline, as shown here. The variation below moves the Box from the SubScene origin at the top left to the lower right, while also rotating it about the Y_AXIS. Scroll the mouse to dolly the camera to and fro.

image

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);
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045