JavaFX MouseEvent inside a circle

Description

JavaFX MouseEvent inside a circle


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

public class Main extends Application {
   @Override// ww w .j av  a 2s. c  o m
   public void start(Stage primaryStage) {
      Pane pane = new Pane();
      Circle circle = new Circle(100, 60, 50);
      circle.setFill(Color.WHITE);
      circle.setStroke(Color.BLACK);
      Text text = new Text();
      pane.getChildren().addAll(circle, text);

      pane.setOnMouseMoved(e -> {
         if (circle.contains(e.getX(), e.getY())) {
            text.setText("Mouse point is inside the circle");
         } else {
            text.setText("Mouse point is outside the circle");
         }

         text.setX(e.getX());
         text.setY(e.getY());
      });

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

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



PreviousNext

Related