danger mouse
danger mouse

Reputation: 1497

How to get circles to change colour with mouse over

I want to set up a grid of circles (non-overlapping) so that, when the mouse pointer is over one of them, that circle changes colour. I have experimented and so far have two options:

  1. Use a container e.g. JPanel. Use MouseMotionListener.mouseMoved(MouseEvent e) to get the x and y coordinates of the mouse pointer at all times. Then, if the coordinates lie within one of the circles, use repaint() to repaint the whole container.

  2. Set each circle as a container. Use MouseListener.mouseEntered(MouseEvent e) to detect when the mouse pointer moves over a circle. Then redraw that container only.

Is #2 the best approach? If so, how can I set up a circular container? Is there a better approach than either of the above?

Upvotes: 1

Views: 327

Answers (1)

camickr
camickr

Reputation: 324098

If so, how can I set up a circular container?

Check out Playing With Shapes.

You can use the ShapeComponent to create a circles that act just like components. So you can build your grid just like you would with any other Swing component.

Upvotes: 2

Related Questions