Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (292)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Keep mouse in JFrame  (Read 2496 times)
0 Members and 1 Guest are viewing this topic.
Offline flipperke

Junior Member





« Posted 2007-09-21 13:52:29 »

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)
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #1 - Posted 2007-09-21 14:21:15 »

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

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #2 - Posted 2007-09-21 14:24:20 »

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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline flipperke

Junior Member





« Reply #3 - Posted 2007-09-21 15:13:41 »

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.
Offline flipperke

Junior Member





« Reply #4 - Posted 2007-09-27 10:08:41 »

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

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #5 - Posted 2007-09-27 10:19:48 »

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Offline flipperke

Junior Member





« Reply #6 - Posted 2007-09-27 10:24:02 »

I'm sorry but I don't quite understand what you mean with
Quote
Sign it with appropriate privileges ( createRobot ) to use the Robot ?

I am also a JWS-newbie
Offline CommanderKeith
« Reply #7 - Posted 2007-09-27 10:50:52 »

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!  Cheesy

Offline flipperke

Junior Member





« Reply #8 - Posted 2007-09-27 13:10:47 »

So, now i have signed my jars, i have changed my jnlp-filIe. I added
1  
2  
3  
<security>
           <all-permissions/>
 </security>


The program still runs identically the same as before.

Anyway, thanks for the link!
Offline cylab

JGO Knight


Medals: 30



« Reply #9 - Posted 2007-09-27 13:27:22 »

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

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline flipperke

Junior Member





« Reply #10 - Posted 2007-09-27 13:34:19 »

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...
Offline flipperke

Junior Member





« Reply #11 - Posted 2007-09-27 14:23:00 »

But now, it works, don't even know what I did differently....
Offline ryanm
« League of Dukes »

Senior Member


Projects: 1


Used to be bleb


« Reply #12 - Posted 2007-09-27 16:17:10 »

Webstart caching maybe?
Offline flipperke

Junior Member





« Reply #13 - Posted 2007-09-27 17:14:06 »

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

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #14 - Posted 2007-09-27 17:23:25 »

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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (67 views)
2013-05-17 21:29:12

alaslipknot (76 views)
2013-05-16 21:24:48

gouessej (108 views)
2013-05-16 00:53:38

gouessej (104 views)
2013-05-16 00:17:58

theagentd (115 views)
2013-05-15 15:01:13

theagentd (104 views)
2013-05-15 15:00:54

StreetDoggy (149 views)
2013-05-14 15:56:26

kutucuk (172 views)
2013-05-12 17:10:36

kutucuk (170 views)
2013-05-12 15:36:09

UnluckyDevil (179 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.097 seconds with 20 queries.