如何在JavaFX中在线框模式和实心模式之间切换

huangapple go评论77阅读模式
英文:

How to change between wireframe and solid in javafx

问题

3D软件允许用户动态更改绘图模式。这可以在JavaFX上实现吗?

英文:

3d software allow user to change draw mode dinamically. It can be implemented on javafx ?

答案1

得分: 4

以下是您要翻译的内容:

使用单选按钮更改绘制模式

如何在JavaFX中在线框模式和实心模式之间切换

在这种方法中,Box 实例使用单选按钮更改其 DrawMode

这是一个可以尝试的 JavaFX 单一类。

App.java

public class App extends Application {
    
    @Override
    public void start(Stage stage) {

        var perspective = new PerspectiveCamera(true);
        perspective.setNearClip(0.1);
        perspective.setFarClip(500);
        perspective.setTranslateZ(-150);
        
        Shape3D cube = new Box(50, 50, 50);
        cube.setCullFace(CullFace.NONE);
        cube.setMaterial(new PhongMaterial(Color.CORAL));

        var toggleGroup = new ToggleGroup();
        var solid = new RadioButton("实心");
        solid.setToggleGroup(toggleGroup);
        solid.setSelected(true);
        var wire = new RadioButton("线框");
        wire.setToggleGroup(toggleGroup);

        var hBox = new HBox(solid, wire);

        toggleGroup.selectedToggleProperty().addListener((o) -> {
            Toggle selectedToggle = toggleGroup.getSelectedToggle();

            if (selectedToggle == solid) {
                cube.setDrawMode(DrawMode.FILL);
            }
            if (selectedToggle == wire) {
                cube.setDrawMode(DrawMode.LINE);

            }

        });

        var group3d = new Group(perspective, cube);

        var subscene = new SubScene(group3d, 300, 400, true, SceneAntialiasing.BALANCED);
        subscene.setCamera(perspective);

        var stack = new StackPane(subscene, hBox);

        stage.setScene(new Scene(stack, 300, 400));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}
英文:

Changing draw mode with radio buttons

如何在JavaFX中在线框模式和实心模式之间切换

In this approach a Box instance change its DrawMode with radiobuttons.

This is a single class javafx you can try .

App.java

public class App extends Application {
@Override
public void start(Stage stage) {
var perspective = new PerspectiveCamera(true);
perspective.setNearClip(0.1);
perspective.setFarClip(500);
perspective.setTranslateZ(-150);
Shape3D cube = new Box(50, 50, 50);
cube.setCullFace(CullFace.NONE);
cube.setMaterial(new PhongMaterial(Color.CORAL));
var toggleGroup = new ToggleGroup();
var solid = new RadioButton("solid");
solid.setToggleGroup(toggleGroup);
solid.setSelected(true);
var wire = new RadioButton("wireframe");
wire.setToggleGroup(toggleGroup);
var hBox = new HBox(solid, wire);
toggleGroup.selectedToggleProperty().addListener((o) -> {
Toggle selectedToggle = toggleGroup.getSelectedToggle();
if (selectedToggle == solid) {
cube.setDrawMode(DrawMode.FILL);
}
if (selectedToggle == wire) {
cube.setDrawMode(DrawMode.LINE);
}
});
var group3d = new Group(perspective, cube);
var subscene = new SubScene(group3d, 300, 400, true, SceneAntialiasing.BALANCED);
subscene.setCamera(perspective);
var stack = new StackPane(subscene, hBox);
stage.setScene(new Scene(stack, 300, 400));
stage.show();
}
public static void main(String[] args) {
launch();
}
}

huangapple
  • 本文由 发表于 2023年1月9日 09:57:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定