JavaFX Circle check if mouse inside

Description

JavaFX Circle check if mouse inside

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

public class Main extends Application {
   @Override//from w w w .j  a  v  a 2  s .  c  om
   public void start(Stage primaryStage) {
      // Create a pane
      Pane pane = new Pane();

      // Create a circle and set its properties
      Circle circle = new Circle(100, 60, 50);
      circle.setFill(Color.WHITE);
      circle.setStroke(Color.BLACK);
      pane.getChildren().add(circle);

      // Create and register the handle
      pane.setOnMouseMoved(e -> {
         pane.getChildren().clear();
         Text text = new Text(e.getX(), e.getY(), "Mouse point is " + 
            (circle.contains(e.getX(), e.getY()) ? "inside " : "outside ") +
            "the circle");
         pane.getChildren().addAll(circle, text);
      });

      // Create a Scene and place it in the stage
      Scene scene = new Scene(pane, 350, 150);
      primaryStage.setTitle("java2s.com");
      primaryStage.setScene(scene);
      primaryStage.show();
   }
}



PreviousNext

Related