Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detecting mouse over 1

Status
Not open for further replies.
May 13, 2002
75
GB
I'm trying to create a very simple chart, so far i've got it to plot small rectangles onto a JPanel. What i want is when the user puts the mouse over a point then it can display any metadata about the point in a separate box.

So my question is, how do i detect if the mouse has gone over a point (rectangle) on a JPanel ? I guess there is a listener i should add, mouse listener maybe ? When i've added a listener, what do i do ?

Thanks in advance
 
Use a MouseMotionListener or MouseMotionAdapter to catch mouseMove events. It'll give you the mouse cursor position. Then, compare this position with the position of all your points. The code for this will depend on the way you stored your points. Water is not bad as soon as it stays out human body ;-)
 
Thanks guys, so i've added my MouseMotionListener fine. My original JPanel just has the points drawn on using g.drawRect in the Paint method.

What i thought would be a way to do it so i didn't have to check all my point everytime the mouse moved was to only check against my data when the mouse is over something black i.e. drawn with drawRect.

I thought i could check the background colour of the mouse pointer and when it's over a black pixel then get the co-ords and look up in my data.

However, when i look at the getBackground and getForeground Color at any point (even over a rectangle from drawRect) it's always the same

Foreground
javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]
Background
javax.swing.plaf.ColorUIResource[r=204,g=204,b=204]

So how can i find out if i'm over a black rectangle ?

Thanks
 
Can't you do it in another way :
create a MyPoint class that inherits from Component on wich you add your listener. This class must have a paint method that draws the rectangle on the pannel. Then, all the code needed to display information about the plot will be stored into the plot itself. Water is not bad as soon as it stays out human body ;-)
 
Yes, this seems a much better way to do things, i can then store all my metadata in the MyPoint class and have methods to change it's colour when the mouse is moving over it etc.

I've had a look coding this and it seems to run fine, and the mouse over gives me my metadata but one problem, the point itself isn't actually displayed! It seems that it's paint method isn't being called.

So in the paint(Graphics g) method of my Chart extends JPanel class i do the following:

mP = new MyPoint((int)x,(int)y);
this.add(mP);

And i do this in a loop for all my points. In the constructor of the MyPoint class i have

public MyPoint(int xpos, int ypos)
{
super();
x = xpos;
y = ypos;

addMouseMotionListener(this);

this.setSize(2,2);
this.setBackground(Color.black);
this.setForeground(Color.black);
this.setLocation(x,y);
this.setVisible(true);
this.repaint();
}

the paint(Graphics g) just has g.fillRect((x , y, 5, 5);

Everything runs, and i have a System.out.println line in my mouseMoved event method that displays the points metadata but the points themselves aren't displayed.

Thanks for any help you could give me
 
Well I spoke about a "paint" method but it can be another name that you code by yourself passing it your pannel as parameter and in wich you code the painting of the rectangle the same way you did before. May be your actual "paint" method paints your point outside or below your panel instead of painting it in the panel. Water is not bad as soon as it stays out human body ;-)
 
Fantastic, passed the Graphics object of my JPanel as a parameter and drew the rectangle directly to that. I can mouse over and my data is displayed from the MyPoint class

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top