Home > Software engineering >  JavaFX - Remove Shape With Button setOnAction
JavaFX - Remove Shape With Button setOnAction

Time:05-26

I am trying to create an application where a user inputs a number of sides, and upon clicking a button, removes any current shape from the Scene and generates a shape with the specified number of sides.

Whilst the logic to generate the shape is working, implementation root.getChildren().remove(shape); does not appear to be working?

public class genShape extends Application 
{
    @Override    
    public void start(Stage stage) 
    {       
        // configure text fields for input         
        TextField shapeNoSidesField = new TextField();         
        shapeNoSidesField.setMaxWidth(23);         
        
        // configure label fields        
        Label shapeNoSidesLabel = new Label("Enter Desired Number Of Shape Sides:");         
        shapeNoSidesLabel.setTextFill(Color.RED);         
        shapeNoSidesLabel.setFont(Font.font("Arial", 10));   

        // configure generate shape button         
        Button generateShapeButton = new Button();         
        generateShapeButton.setText("Generate Shape");
        
        // configure an HBox input components                       
        HBox inputComponents = new HBox(40);
        inputComponents.setAlignment(Pos.CENTER);        
        inputComponents.getChildren().addAll(shapeNoSidesLabel, shapeNoSidesField);  
        
        // configure vbox
        VBox root = new VBox(50);         
        root.setAlignment(Pos.CENTER);        
        root.getChildren().addAll(inputComponents, generateShapeButton); 
        
        
        // Generate shape based on user Integral input. Perform input checks.
        generateShapeButton.setOnAction( e ->                     
        {   
            Shape shape = null;
            root.getChildren().remove(shape);
            // Perform input checks using helper variables. Assert no of sides field not null.
            boolean shapeFieldIsEmpty = shapeNoSidesField.getText().isEmpty();
            if(shapeFieldIsEmpty)        
            {                               
                System.out.println("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
                display.setText("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
            }
            else 
            {
                // Retrieve user required number of sides
                int numberOfSides = Integer.parseInt(shapeNoSidesField.getText());
                switch(numberOfSides) 
                {
                    // User enters shape requirement of three sides (Polygon object)
                    case 3:
                        shape = new Polygon();
                        ((Polygon) shape).getPoints().addAll(new Double[]
                                {
                                    100., 70.,
                                    100., 250.,
                                    400., 250.
                                });
                        root.getChildren().add(shape);
                        break;
                    case 4:
                        shape = new Rectangle();
                        ((Rectangle) shape).setX(150f);
                        ((Rectangle) shape).setY(75.0f); 
                        ((Rectangle) shape).setWidth(300.0f); 
                        ((Rectangle) shape).setHeight(150.0f);
                        root.getChildren().add(shape);
                        break;
                    default:
                        break;
                }
      
            }                                                                        
        }                                     
    ); 
        
        // create a new scene and add it to the stage         
        Scene scene = new Scene(root, 500, 500);         
        stage.setScene(scene);         
        stage.setTitle("Shapes");        
        stage.show();             
        }
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        launch(args);     
    }
}

Any help greatly appreciated, thank you!

CodePudding user response:

Just make shape an instance variable. Also note there is no need for all the ugly downcasting.

public class GenShape extends Application 
{

    private Shape shape ;

    @Override    
    public void start(Stage stage) 
    {       
        // configure text fields for input         
        TextField shapeNoSidesField = new TextField();         
        shapeNoSidesField.setMaxWidth(23);         
        
        // configure label fields        
        Label shapeNoSidesLabel = new Label("Enter Desired Number Of Shape Sides:");         
        shapeNoSidesLabel.setTextFill(Color.RED);         
        shapeNoSidesLabel.setFont(Font.font("Arial", 10));   

        // configure generate shape button         
        Button generateShapeButton = new Button();         
        generateShapeButton.setText("Generate Shape");
        
        // configure an HBox input components                       
        HBox inputComponents = new HBox(40);
        inputComponents.setAlignment(Pos.CENTER);        
        inputComponents.getChildren().addAll(shapeNoSidesLabel, shapeNoSidesField);  
        
        // configure vbox
        VBox root = new VBox(50);         
        root.setAlignment(Pos.CENTER);        
        root.getChildren().addAll(inputComponents, generateShapeButton); 
        
        
        // Generate shape based on user Integral input. Perform input checks.
        generateShapeButton.setOnAction( e ->                     
        {   
            if (shape != null) {
                root.getChildren().remove(shape);
            }

            // Perform input checks using helper variables. Assert no of sides field not null.
            boolean shapeFieldIsEmpty = shapeNoSidesField.getText().isEmpty();
            if(shapeFieldIsEmpty)        
            {                               
                System.out.println("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
                display.setText("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
            }
            else 
            {
                // Retrieve user required number of sides
                int numberOfSides = Integer.parseInt(shapeNoSidesField.getText());
                switch(numberOfSides) 
                {
                    // User enters shape requirement of three sides (Polygon object)
                    case 3:
                        Polygon poly = new Polygon();
                        poly.getPoints().addAll(new Double[]
                                {
                                    100., 70.,
                                    100., 250.,
                                    400., 250.
                                });
                        root.getChildren().add(poly);
                        this.shape = poly ;
                        break;
                    case 4:
                        Rectangle rect = new Rectangle();
                        rect.setX(150f);
                        rect.setY(75.0f); 
                        rect.setWidth(300.0f); 
                        rect.setHeight(150.0f);
                        root.getChildren().add(rect);
                        this.shape = rect ;
                        break;
                    default:
                        break;
                }
      
            }                                                                        
        }                                     
    ); 
        
        // create a new scene and add it to the stage         
        Scene scene = new Scene(root, 500, 500);         
        stage.setScene(scene);         
        stage.setTitle("Shapes");        
        stage.show();             
        }
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        launch(args);     
    }
}
  • Related