Skip to main content
gamedev.net
🔒 Locked

[java] Java Change Mouse Position within JFrame

Started by Halsafar Dec 8, 2005 at 10:31 PM 3 replies 2.5k views
Original Post
Halsafar
Halsafar
I need to impliment a "snap mouse to grid" for the map editor I am working on. I am able to retrieve the mouse coordinates within the JFrame. The next step would be to determine what the closest snap coordinate is, however since I cannot find out how to change the mouse position I stopped there. I thought on it, and maybe it is not possible for the JVM to move the mouse around?? Any help appreciated. Thanks, Halsafar
Whackjack
Whackjack
Most editors that use a snap-to-grid functionality don't snap the mouse pointer itself to the grid. They snap whatever the mouse is currently "holding" to the grid, or in your case, a tile or piece of the map. The solution would be to just detect how far away the mouse pointer is from any arbitrary point on the grid and draw your map tile (or whatever) accordingly.
Halsafar
Halsafar
So I do not programmatically move the mouse pointer. Instead, I technically don't want to see the mouse pointer at all, I just want the user to see the map piece. This way, I can use mouse movements to move and snap the piece in place.

Is this what your saying?

If so, how do I disable the mouse pointer within the frame? Just picturing this theory in my head, it would be really ugly if the mouse pointer was still following the map piece around but slighlty off due to the snap.

I can catch the event when the mouse enters/exits the bounds of the frame.
Optus
Optus
Here's some invisible cursor code:
ImageIcon emptyIcon = new ImageIcon(new byte[0]);Cursor invisibleCursor = getToolkit().createCustomCursor(emptyIcon.getImage(), new Point(0,0), "Invisible");frame.getContentPane().setCursor(invisibleCursor);


As for your other question, how to move the mouse: java.awt.Robot specfically mouseMove(x,y);
Whackjack
Whackjack
You don't necessarily need to hide the mouse pointer. It's good to use it as a reference point. And as for the mouse pointer becoming off-center from the tile piece, again, that's typical for grid-based editors. Visio does this.

If you're doing it as a grid with lines (and thus rectangles), I would just have the tile move when the mouse pointer goes to the next rectangle.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.