javajavafx3djavafx-3d

How to change between wireframe and solid in javafx


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


Solution

  • Changing draw mode with radio buttons

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