Reputation: 1370
I want to detect whether the given point is in my controls area/bound or not. example - Point(100, 100) I want to know whether it is on my button or not?
Upvotes: 1
Views: 1488
Reputation: 1370
I solved it using getGraphics() function
`Node n = tabShapes.getGraphic();
System.out.println("x = "+pt.x +" y = "+pt.y);
Point2D p = new Point2D(pt.x, pt.y);
boolean f = n.contains(n.screenToLocal(p));`
Upvotes: 0
Reputation: 82461
Transform the point to the Node
coordinate system and use Node.contains
to check, if the point is actually inside the Node
:
The following example checks every 500 ms, if the screen coordinates (100, 100) are in the Button
's bounds
@Override
public void start(Stage primaryStage) {
Button btn = new Button("Get (100, 100) in here");
Point2D pt = new Point2D(100, 100);
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), evt ->
System.out.println(
btn.contains(btn.screenToLocal(pt))
)
));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 200, 50);
primaryStage.setScene(scene);
primaryStage.show();
}
Upvotes: 1
Reputation: 7074
You can create a java.awt.Rectangle
variable marking the boundaries of your buttons and areas. Then use the method Rectangle#contains
to test if the Point
in question is within this area.
You may want to reference the Javadocs here: https://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html
Upvotes: 0