Keep mouse in JFrame

Can somebody help me plz. ?

I am trying to make a game (just tiny) and I am trying to keep the pointer(mouse) in the frame. So that, when I leave the frame to lets say, the left side, the pointer gets drawn to the right border of the frame (taking the insets into account).
How would be the best way to achieve this.

Thanks in advance.

(Sorry if this question is asked in a strange way, I’m only doing my best)

May be, you can detect when the mouse exit the frame (mouseExited event) and then move the mouse with an java.awt.Robot object.

Due to the way the mouse events are handled I don’t think it’s possible to implement this behaviour in an entirely reliable way.

As suggested above, you can add a MouseListener to your Frame, and listen for delivery of the mouseExited(…) event.
You can then reposition the mouse cursor using java.awt.Robot#mouseMove(int x, int y).

However, if a mouse click occurs while the mouse is outside the Frame - focus will be lost and everything will go tits. (break)
If you introduce a significant border between the Frame boundaries and the area in which the cursor is intended to loop, it will work better but never be fool-proof.

Extending this idea further, you could constrain the mouse cursor to the centre of the Frame(resetting the position after every mouseMoved/Dragged event), make the cursor invisible, and render a virtual cursor as part of your game loop. (if a game is what you are writing)
This approach will lose the UI benefit of a hardware cursor, but will make it extremely difficult for the cursor to skip outside your Frame.

Fundamentally AWT does not currently expose the necessary functionality to limit the range of mouse movement ( In Windows I believe you’d want to use the gdi ClipCursor function)
So the solutions suggested above are essentially hacks.

Thanks alot!

I had figured out that if I wanted to use a MouseListener, I really needed a big border, and even than, when moving fast, you still can leave the frame.
So now I’m using the tip of constraining the mousepointer to the center (or no mater where, as long as it is not to close to the border).

BTW: I had already made my cursor invisible, I am using a texture of an aircraft.

Could you please explain me how I can constrain the mouse-pointer to a fixed position in a JWS-application?

Sign it with appropriate privileges ( createRobot ) to use the Robot ?

I’m sorry but I don’t quite understand what you mean with

I am also a JWS-newbie

Hi,

This is the best webstart and signing tutorial I’ve seen:

http://www.dallaway.com/acad/webstart/

Tells you how to get a free certificate from Thawte too, saves $200! :smiley:

So, now i have signed my jars, i have changed my jnlp-filIe. I added


<security>
     		<all-permissions/>
 </security>

The program still runs identically the same as before.

Anyway, thanks for the link!

What’s your problem? Didn’t java.awt.Robot#mouseMove(int x, int y) work?

When I run the program, my mouse stays positioned at a fixed place ( more or less in the center of my JFrame).
When I run it as a Java Web Start, the mouse doesn’t stay fixed anymore.
So, yes, java.awt.Robot#mouseMove(int x, int y) doesn’t work.
Maybe I should have gone to the java-forums, but since this is also a newbie-forum…

But now, it works, don’t even know what I did differently…

Webstart caching maybe?

The fact is, there where other things that where different, that did change.

If it were a privileges issue you would not have been able to construct an instance of Robot at all, as it is the constructor that performs the security check.