JavaFx 2D在3D应用程序中的一部分 [英] JavaFx 2D part in 3D application

查看:120
本文介绍了JavaFx 2D在3D应用程序中的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



那么,我该如何分离这两个区域呢?可能有两个场景?



感谢您提供的每一个提示:)

解决方案

最好的方法是使用应用程序。




I have a small problem with an Application I write.

I want to have to have a 3D field and on the right side a toolbar that contains 2D-Components like buttons.

I tried to simply add these Components to my root-Group, but then it is impossible to read the text and they move with all the others.

So, how can I seperate these two areas? Possibly with two scenes?

Thank you for every hint you can give :)

解决方案

The best approach is using a SubScene for the 3D nodes. You can keep all the 2D content on top of it without rendering problems.

You can read about the SubScene API here.

This is a small sample of what you can do with this node:

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

For more complex scenarios, have a look at the 3DViewer application under the OpenJFX project.

这篇关于JavaFx 2D在3D应用程序中的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆