JavaFx 2D在3D应用程序中的一部分

时间:2015-02-20 12:11:48

标签: javafx javafx-3d

我写的应用程序有一个小问题。

我想拥有一个3D字段,右边是一个包含按钮等2D组件的工具栏。

我试着简单地将这些组件添加到我的root-Group中,但之后无法读取文本并且它们会与其他所有组件一起移动。

那么,我该如何分开这两个方面呢?可能有两个场景?

感谢您提供的所有提示:)

1 个答案:

答案 0 :(得分:6)

最好的方法是使用SubScene表示3D节点。您可以将所有2D内容保留在其上而不会出现问题。

您可以阅读SubScene API here

这是您可以使用此节点执行的操作的一小部分示例:

private double mousePosX, mousePosY;
private double mouseOldX, mouseOldY;
private final Rotate rotateX = new Rotate(-20, Rotate.X_AXIS);
private final Rotate rotateY = new Rotate(-20, Rotate.Y_AXIS);

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

    // 3D
    Box box = new Box(5, 5, 5);
    box.setMaterial(new PhongMaterial(Color.GREENYELLOW));

    PerspectiveCamera camera = new PerspectiveCamera(true);
    camera.getTransforms().addAll (rotateX, rotateY, new Translate(0, 0, -20));

    Group root3D = new Group(camera,box);

    SubScene subScene = new SubScene(root3D, 300, 300, true, SceneAntialiasing.BALANCED);
    subScene.setFill(Color.AQUAMARINE);
    subScene.setCamera(camera);

    // 2D
    BorderPane pane = new BorderPane();
    pane.setCenter(subScene);
    Button button = new Button("Reset");
    button.setOnAction(e->{
        rotateX.setAngle(-20);
        rotateY.setAngle(-20);
    });
    CheckBox checkBox = new CheckBox("Line");
    checkBox.setOnAction(e->{
        box.setDrawMode(checkBox.isSelected()?DrawMode.LINE:DrawMode.FILL);
    });
    ToolBar toolBar = new ToolBar(button, checkBox);
    toolBar.setOrientation(Orientation.VERTICAL);
    pane.setRight(toolBar);
    pane.setPrefSize(300,300);

    Scene scene = new Scene(pane);

    scene.setOnMousePressed((MouseEvent me) -> {
        mouseOldX = me.getSceneX();
        mouseOldY = me.getSceneY();
    });
    scene.setOnMouseDragged((MouseEvent me) -> {
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        rotateX.setAngle(rotateX.getAngle()-(mousePosY - mouseOldY));
        rotateY.setAngle(rotateY.getAngle()+(mousePosX - mouseOldX));
        mouseOldX = mousePosX;
        mouseOldY = mousePosY;
    });

    primaryStage.setScene(scene);
    primaryStage.setTitle("3D SubScene");
    primaryStage.show();
}

SubScene

对于更复杂的场景,请查看OpenJFX项目下的3DViewer应用程序。

3DViewer

相关问题