JavaFX animation/View 带窗格的 3d

JavaFX animation/View 3d with panes

我第一次使用 JavaFX 尝试制作一个应用程序,我可以用它来演示带有按钮控件的简单动画。为此,我在初级阶段使用了 BoarderPane,左侧、右侧和底部都使用了 Gridpanes。

但是,对于中心,我需要能够绘制一个球体,其中有一条线,我可以针对不同的视图进行旋转,同时能够动画,或者至少快速移动,内线.

我试过在中心使用窗格,但没有用。我试过让它成为自己的场景和子场景,但这是行不通的。而且我不能使用 canvas,因为它仅适用于 2D 动画。

有没有一种方法可以在保持我创建的 BoarderPane 布局的同时为线条设置动画或旋转相机?

我之前曾尝试查看以下内容以了解我可以做什么,但大多数似乎与 BoarderPane 不兼容:

每当您想混合 2D 和 3D(和相机)时,您必须为 3D 内容使用 SubScene container

SubScene provides separation of different parts of a scene, each of which can be rendered with a different camera, depth buffer, or scene anti-aliasing. A SubScene is embedded into the main scene or another sub-scene.

如果你有一个BorderPane容器,你可以完美地将子场景添加到它的中心。

对于类似的用例,您可以查看 here, which is mainly a group that holds a sub scene with an Sphere and a cylinder (both from the FXyz 3D library 中的 Qubit3D class。

您可以轻松地将此组添加到您的 2D 场景中:

private final Rotate rotate = new Rotate(0, 0, 0, 0, javafx.geometry.Point3D.ZERO.add(1, 1, 1));

@Override
public void start(Stage primaryStage) throws Exception {

    final Timeline timeline = new Timeline(
        new KeyFrame(Duration.seconds(10), 
            new KeyValue(rotate.angleProperty(), 360)));

    final Qubit3D qubit = new Qubit3D();

    final BorderPane root = new BorderPane(qubit);

    final Button buttonAnimate = new Button("Animate");
    buttonAnimate.setOnAction(e -> {
        rotate.setAngle(0);
        timeline.playFromStart();
    });

    root.setLeft(new StackPane(buttonAnimate));
    final Button buttonStop = new Button("Stop");
    buttonStop.setOnAction(e -> timeline.stop());
    root.setRight(new StackPane(buttonStop));

    Scene scene = new Scene(root, 600, 400, true, SceneAntialiasing.BALANCED);
    scene.setFill(Color.BISQUE);

    primaryStage.setScene(scene);
    primaryStage.setTitle("Qubit3D Sample");
    primaryStage.show();

    qubit.rotateRod(rotate);

}

我对 Qubit3D 添加的唯一修改是:

public void rotateRod(Rotate rotate) {
    rodSphere.getTransforms().setAll(rotate);
}

如果你运行它:

请注意,您可以与球体交互(通过鼠标拖动事件),同时您还可以 start/stop 球体和杆的完整旋转。