Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

javafx - Java FX: How can I show a new Polygon on scene after redrawing

I have a program which is draw a shape, E.g. Rectangle. If a user select another shape from the list, E.g. Triangle, after clicking on Draw button, the new shape will be drawn. My program is alright if drawing for 1 shape but if I select another shape then the previous shape still there on the scene with the new shape. I have tried to remove that note (which used for showing the shape on scene) from scene before drawing, then after drawing, add it again but seems it doesn't work. Anyone can help with this please?

     [public class UI extends Application 
    { 
        
    // create object of the Shapes class as an attribute        
        Shapes shapeType;   
        Shape shapeFX  = new Polygon(); 
        private String shape, color;
        private VBox root = new VBox(25); 
        private Scene scene;


    
    @Override    
    public void start(Stage stage)     
    { 
        ...
        
        
        // create and configure a button to perform the calculations         
        Button drawButton = new Button();         
        drawButton.setText("DRAW SHAPE");       
        drawButton.setOnAction( e ->  
                            {  
                                
                                if(...)        
                                {     
                                    //some validations
                                }  
                                else
                                {                                     
                                    drawShape(colorsList.getValue(), shapeFX);
                                   
                                }                                                                                           
                            }                                    
                            );
           
                
        root.setAlignment(Pos.TOP_CENTER);          
        root.getChildren().addAll(inputShape, inputColor, drawButton, display); 
    
        ...   
    
        }     

    public static void main(String[] args)     
    {         
        launch(args);     
    }
    public void drawShape(String c, Shape localFX)
    {
        //remove the current shape from VBox
        if (root.getChildren().size() == 5)
        {
            root.getChildren().remove(this.shapeFX);
        }
        //draw new shape
        shapeType.draw(color, localFX);
        
        //add new shape to VBox
        root.getChildren().add(this.shapeFX);   
    }
}][1]

                            
question from:https://stackoverflow.com/questions/65946468/java-fx-how-can-i-show-a-new-polygon-on-scene-after-redrawing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...