Checking If the Mouse is Over a Certain Component
I've run into yet another tricky problem. :P
While the user's mouse is over a certain panel I have on my GUI, I want the panel to outline a circle around the user's mouse. I'm already using a Swing Timer to control the movement of existing objects drawn on the panel, and I've overridden the panel's paintComponent(Graphics g) method, so I thought I'd just add a little if-statement to that paintComponent(Graphics g) method so that it also checks to see if the mouse is over the panel.
I'd also need to know how to get the mouse's position with respect to the JPanel that is drawing the circle around the mouse...
However, I'm not finding any fast ways to do this... Can anyone point (heh, mouse pointer <---awful pun) me in the right direction?
Re: Checking If the Mouse is Over a Certain Component
Well, I found a decent solution. No longer need help. :P
Re: Checking If the Mouse is Over a Certain Component
Care to share your solution? A MouseMotionListener should allow you to follow the mouse movement appropriately, and you can repaint as needed.
Re: Checking If the Mouse is Over a Certain Component
Quote:
Care to share your solution?
Yeah, I will.
I use java.awt.MouseInfo, and then to see if the mouse is over the certain panel, I do this:
Code Java:
private boolean mouseIsOverDisplayPanel()
{
if(MouseInfo.getPointerInfo().getLocation().x >= displayPanel.getLocationOnScreen().x
&& MouseInfo.getPointerInfo().getLocation().x <= displayPanel.getLocationOnScreen().x + displayPanel.getWidth()
&& MouseInfo.getPointerInfo().getLocation().y >= displayPanel.getLocationOnScreen().y
&& MouseInfo.getPointerInfo().getLocation().y <= displayPanel.getLocationOnScreen().y + displayPanel.getHeight())
{
return true;
}
else
{
return false;
}
}
Using this code, everything is show relative to the screen, and I can accurately check if the mouse is over this panel whenever I need to.
Re: Checking If the Mouse is Over a Certain Component
Just an alternative: Use a MouseListener and its corresponding mouseEntered/mouseExited methods to set a boolean flag - then check this flag when you paint....and/or use a MouseMotionListener to get the true location of the mouse if needed.
Re: Checking If the Mouse is Over a Certain Component
Yes, that actually occurred to me just a couple hours ago. :P I'm trying to decipher which method is more useful to my program...