Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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 3993 times)
0 Members and 1 Guest are viewing this topic.
Offline flipperke

Junior Devvie





« Posted 2007-09-21 11: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 12: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 Knight


Medals: 14


falling into the abyss of reality


« Reply #2 - Posted 2007-09-21 12: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! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline flipperke

Junior Devvie





« Reply #3 - Posted 2007-09-21 13: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 Devvie





« Reply #4 - Posted 2007-09-27 08: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 Knight


Medals: 14


falling into the abyss of reality


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

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

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline flipperke

Junior Devvie





« Reply #6 - Posted 2007-09-27 08: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 08: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 Devvie





« Reply #8 - Posted 2007-09-27 11: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 Ninja


Medals: 55



« Reply #9 - Posted 2007-09-27 11: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!
Legends of Yore - The Casual Retro Roguelike
Offline flipperke

Junior Devvie





« Reply #10 - Posted 2007-09-27 11: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 Devvie





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

But now, it works, don't even know what I did differently....
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


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

Webstart caching maybe?
Offline flipperke

Junior Devvie





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

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

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #14 - Posted 2007-09-27 15: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! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Gibbo3771 (12 views)
2014-11-24 19:59:16

trollwarrior1 (35 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (66 views)
2014-11-07 19:57:14

TehJavaDev (70 views)
2014-11-03 22:04:50

BurntPizza (68 views)
2014-11-03 18:54:52

moogie (83 views)
2014-11-03 06:22:04
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!