I’m creating a menu for a game I’m currently designing and I’m using images I’ve designed in photoshop to use as buttons for the menus, but I’m wondering if there is any way to detect if the mouse is hovering over the image.
I’ve done this for regular rectangles previously but this shape is not a basic rectangle (Click HERE to view the shape).
The shape is being draw as a BufferedImage with Graphics2D.
Thank you in advance for the help!gya
2
Answers
You can add a MouseListener to the object, and write code for a MouseEntered event. This will be triggered when the Mouse first enters the bounds of the component, but not when it clicks. Oracle has good documentation on this.
https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html
You can map out your polygons and figure out which one was selected by adding a
MouseListener
. Next time you redraw, you can show the selection.You can check if the event’s point is contained within a polygon using:
Here is an example:
Here is a screenshot.