Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javafx - How can you remove a drawed item via a togglebutton [edited]?

`I tried to create a javafx programm that draws a function when you select it (You will be able to select different types of functions by using a toggleswitch) but i dont have a clue how i can remove the object from the Group ( the object which draws the graph)

grid.setOnAction(e -> {
        if(grid.isSelected()) {
           
            Line xA = new Line(00, 250, 600, 250);
            Line yA = new Line(250, 0, 250, 400);
           
            g.getChildren().addAll(xA, yA);
       
        }else {
            
        }
});

So what i actually want to happen is that if you press a button ( created by "ToggleButton grid = new ToggleButton("Grid"); " a graph should be drawn. There are different graphes to select: linear, cubic,.. and if you toggle the button off the selected function should be cleared. Thats what it looks like at the moment but i dont know how to remove the grid or the function if you toggle it off

here is the whole code: of course there are also the implements but i cant add them here..

 @Override
public void start(Stage Q) throws Exception {
    
    Q.setTitle("Funktionen");
    Q.setMinWidth(700);
    Q.setWidth(700);
    Q.setMaxWidth(700);
    Q.setMinHeight(600);
    Q.setHeight(600);
    Q.setMaxHeight(600);
   
    Label label = new Label("Test");
    label.setFont(new Font("Arial", 20));
    
    FlowPane flowpane = new FlowPane(Orientation.HORIZONTAL);
    final Group g = new Group();
    final Group f = new Group();

   flowpane.setPadding(new Insets(20,20,20,20));
   flowpane.setHgap(50);
   
   
    BorderPane borderpane = new BorderPane();
    borderpane.setBottom(flowpane);
    
 
    borderpane.setCenter(g);
    borderpane.setTop(label);
    borderpane.setAlignment(label, Pos.CENTER);

    
    Scene sc1 = new Scene(borderpane);
    Q.setScene(sc1);
    
    ToggleButton grid = new ToggleButton("Grid");
    grid.setPrefSize(160, 40);
    
    ToggleButton linear1 = new ToggleButton("Linear Function");
    linear1.setPrefSize(160, 40);
   
    ToggleButton quadrat1 = new ToggleButton("Quadratische Funktion");
    quadrat1.setPrefSize(160, 40);
    quadrat1.setUserData(quadrat1);
    ToggleButton kurbisch1 = new ToggleButton("Kurbische Funktion");
    kurbisch1.setPrefSize(160, 40);
    kurbisch1.setUserData(kurbisch1);
    flowpane.getChildren().addAll(grid, linear1, quadrat1, kurbisch1);
    
    grid.setOnAction(e -> {
        if(grid.isSelected()) {
           
            Line xA = new Line(00, 250, 600, 250);
            Line yA = new Line(250, 0, 250, 400);
           
            g.getChildren().addAll(xA, yA);
            
        }else {
            g.getChildren().remove(xA);
        }
});
    linear1.setOnAction(e -> {
        if(linear1.isSelected()) {
            Line[] l = new Line[500];
            int xa = 0, ya = 0;
            for (int i = 0; i <= 500; i++){
                double x = ((double)i-250/30);
                double y = 0.5 * x ;
                int yK = 200 - (int) (y * 0.3);
                if(i > 0) {
                    l[i - 1] = new Line(xa, ya, i, yK);
                    l[i - 1].setStroke(Color.DARKBLUE);
                }
                xa = i;
                ya = yK;
                
        }
        g.getChildren().addAll(l);
    } else {
           
        }
    });
    
    Q.show();
    }
 

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

}

question from:https://stackoverflow.com/questions/65910293/how-can-you-remove-a-drawed-item-via-a-togglebutton-edited

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...