If I draw some circles using Java2D. Is there a way display some text when I hover over any of the circles? i.e. I want to display the ID of that circle and some other stuff.
You'll have to save all the centers and radius and test it against the current mouse position.
it's pretty simple operation. If the distance of the mouse position and the center of one of the circle is smaller then the radius, the mouse is inside it and you can draw the hover message you want.
there is a question here that shows the math: http://stackoverflow.com/questions/481144/how-do-you-test-if-a-point-is-inside-a-circle
Hope that helps...
There is a Polygon class that might do it for you (the contains method), but none of the implementing classes is a circle :S
There are a number of ways to accomplish what you want. This is one solution. I assume that you are using Ellipse2D to create the circle. And I assume that you are drawing the circle on a JComponent like a JPanel.
So you declare the Ellipse.
Shape circle = new Ellispe2D.Double(x, y, width, height);
Then you implement MouseMotionListener to detect when the user moves the Mouse over the JPanel.
public void mouseMoved(MouseEvent e){
if(circle.contains(e.getPoint())){
//the mouse pointer is over the circle. So set a Message or whatever you want to do
msg = "You are over circle 1";
}else{
msg = "You are not over the circle";
}
}
Then in the paint() or paintComponent method (whichever one you are overriding to do the painting):
g2.fill(circle);
g2.drawString(msg, 10, 10); //write out the message
I don't know if You can do this directly. But You can use simple math to check cursors position: (x-a)^2+(y-b)^2=r^2 where x,y is cursors position a,b is circles center and r is radius.