import javafx.application.Application;
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage;
 
import java.lang.Integer;
 
public class MoveNodesTestV1 extends Application {
    
    VBox root  = new VBox(20);
    VBox vbox1 = new VBox();
    VBox vbox2 = new VBox();
    
    Button createNodes = new Button("Create nodes");       
    TextField numberOfNodes = new TextField("100000");
    Button changeParent = new Button("Add nodes to scenegraph");
    Label time = new Label();
    VBox buttons = new VBox(5);
    
    HBox nodes = new HBox(2);
    
    boolean switchToVBox1 = true;
    
    @Override
    public void start(Stage primaryStage) {
        buttons.getChildren().addAll(createNodes, numberOfNodes, changeParent, time);
        root.getChildren().addAll(buttons, new Label("VBox1"), vbox1, new Label("VBox2"), vbox2);
       
        createNodes.setOnAction( (e) -> {
            createNodes(Integer.parseInt(numberOfNodes.getText()));
        });
        
        changeParent.setOnAction( (e) -> {
           VBox toClear;
           VBox toAdd;
           if (!switchToVBox1) {
               toClear = vbox2;
               toAdd = vbox1;
           } else {
               toClear = vbox1;
               toAdd = vbox2;
           }
           switchToVBox1 = !switchToVBox1;

           long t1 = System.currentTimeMillis();
           toClear.getChildren().clear();
           long t2 = System.currentTimeMillis();
           toAdd.getChildren().add(nodes);
           long t3 = System.currentTimeMillis();

           long res1 = t2 - t1;
           long res2 = t3 - t2;
           long res3 = t3 - t1;

           changeParent.setText("Change parent of nodes");
           time.setText("Time, to remove: " + res1 + " ms,    to add: " + res2 + " ms,    Total: " + res3); 
        });
        
        Scene scene = new Scene(root, 800, 300);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 
    private void createNodes(int n) {
       long t1 = System.currentTimeMillis();
       vbox1.getChildren().clear();
       vbox2.getChildren().clear();
       nodes.getChildren().clear(); 
       for (int x = 0; x < n; x++) {
          Rectangle rect = new Rectangle(0, 0, 10, 20);  
          rect.setFill(Color.BLUE);
          nodes.getChildren().add(rect);
       }
       long t = System.currentTimeMillis() - t1;
       time.setText("Time taken to create " + n + " nodes: " + t + " ms");
       changeParent.setText("Add nodes to scenegraph");
    }

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