JavaFX MouseEvent click to add circle

Description

JavaFX MouseEvent click to add circle


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;

public class Main extends Application {
  @Override/*ww  w. j a  va2  s  .  c om*/
  public void start(Stage primaryStage) {
    final double WIDTH = 500.0;
    final double HEIGHT = 500.0;
    final double RADIUS = Math.min(WIDTH, HEIGHT) / 30;

    Pane pane = new Pane();

    pane.setOnMouseClicked(e -> {
      switch (e.getButton()) {
        case PRIMARY:
          Circle circle = new Circle(e.getX(), e.getY(), RADIUS);
          circle.setFill(Color.WHITE);
          circle.setStroke(Color.BLACK);
          circle.setOnMouseClicked(f -> {
            pane.getChildren().remove(f.getSource());
          });
          pane.getChildren().add(circle);
          break;
      }
    });

    Scene scene = new Scene(pane, WIDTH, HEIGHT);
    primaryStage.setTitle("java2s.com");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

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



PreviousNext

Related