java.awt.Shape and mouse event

Hey guys!

I’ve got a little problem and I’d like to seek your advice on it. I have a few circles (Ellipse2D.Float) drawn to a JPanel. Those circles should change their size/color when the mouse hovers over them or the player clicks on them, but I can not figure out how to implement it.

All the circles have their fixed location while the panel’s dimension is also fixed at 600*600.

I’ve tried two things so far, my first idea was to use MouseInfo’s getPointerInfo() to check if any circle contains the current location of the cursor, but I don’t really like that solution. I also tried to create a custom event, but I had never done it before and I couldn’t make it to work.

How would you implement it?

Thank you in advance!
zilik

Use MouseMotionListener. If you are using a game loop update the mouse position in the MouseMotionListener methods. In your game loop check the mouse position against your Shapes.

Thank you for your answer!

The solution you mentioned is exactly the same what I did, but I didn’t really liked it. For example if I have 200 circles I have to loop through them in every game loop, which doesn’t seems like a correct solution for me. I was thinking about something like MouseListener’s mouseEntered() method used on a JPanel where the cursor’s location triggers the event by hovering over the panel, but I could’t make it work as I wanted.

Any advice on it?

You add a MouseMotionListener to the component that you draw on:


myComponent.addMouseMotionListener(new MyMotionListener());

private class MyMotionListener implements MouseMotionListener {
    public void mouseDragged(MouseEvent me) {
    }
    
    public void mouseMoved(MouseEvent me) {
        for(MyCircle c : circleList) {
            if(c.contains(me.getX(),me.getY())
                c.setHighlighted(true);
        }
    }
}

mouseMoved(MouseEvent) is called every single time the mouse is moved.

Thanks guys!

Seems like I got to stay with that solution.

If I do this brute-force check every time, should I divide the panel into smaller rectangles, like 3 600*200 pieces, check the cursor position against them first and then do the check on the circles that are in those pieces? Or it would be pointless?

Are you having performance problems with the current solution?

No, I just don’t like it, and I couldn’t make any better.

Imagine a GUI with some buttons on it, where you would like to check if the user clicks on any of them (or highlight them if the cursor hovers over them). Would you do it this way? Just doesn’t seems right. ???

Are those circles evenly divided? Could you provide an image of what you want?

Kind of. I’m working on a Mill/Mills/Merels game.

link: http://noob.hu/2012/02/29/Merels_0.png

I’m sorry if I was unclear. I’d like those circles (shapes) to act like, lets say a JToggleButton which has 3 states based on mouse interact (idle, mouse over and pressed). Despite the fact that I have only 24 circles here, I’d like to work out a solution which can be applied to more of them, without the need of looping through an array/list with hundreds of elements every time. A circle is defined as a class.

I’m kind of new to game developement (as you might have noticed :P), I do not know things in concepts, that is why I wrote every time that I don’t like the solution.

Image is broken.

However if I understand correctly, if the images are evenly spaced, then all you need to do is basically convert that X and Y position to a grid X and Y position then test containment:


int gridX = me.getX() / GRID_WIDTH;
int gridY = me.getY() / GRID_HEIGHT;

if(circle[gridY][gridX].contains(me.getX(),me.getY()))
    //Collision!