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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…