Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  [SOLVED] Java2d Changing mouse cursor + capturing it  (Read 2897 times)
0 Members and 1 Guest are viewing this topic.
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Posted 2012-04-03 12:46:04 »

Hello guys, how are you?

I wanted to know how to change the mouse cursor to one self-made.
Also, to keep mouse inside the application, it's enough to check cords with public void mouseMoved(MouseEvent arg0)? Or how it should be handled? I'd like to capture the mouse inside the game and force an "alt + tab" to give it back to windows (also, multi platform code will be great!).

Thanks! Smiley

Edit: 4/4/12 - Changed title to Solved. The keeping it inside the game´s window is something that I have yet to work it out. If I run into any problem, I´ll come back here Smiley

Offline theagentd
« Reply #1 - Posted 2012-04-03 12:56:24 »

Custom cursors are very easy to use, see here. Use a MouseMotionListener to detect movement, use a Robot to reset the mouse to the center of the window each frame and use a FocusListener to grab and ungrab the mouse on alt-tab.

Heh, this is actually easier to do with LWJGL... Mouse.setGrabbed() and Mouse.setCursor(). xD

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 781
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-04-03 13:00:51 »

Resetting the mouse to the center works, but unfortunately it messes up OS cursor acceleration: when you move the mouse from A to B slowly, it moves the cursor less far on screen than when you move it from A to B quickly.

Resetting the mouse coordinates interferes with that logic. It might not be a problem in your game, but it certainly should not be ignored.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #3 - Posted 2012-04-03 14:16:26 »

Thank you both!

@Riven: If it's not a problem in my game (did you refer to AEI?) why I should not ignore it? Or better, how should I take care of it?

@theagentd: I WILL go to LWJGL in time, I just wanted to go trough the road of Java2d, Slick and LWJGL (in a 2d game) and later on, a 3d game Smiley

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #4 - Posted 2012-04-03 17:06:45 »

Well, I tried what theagentd said but it didn't work. The cursor turns invisible Sad

Here's the code:

1  
2  
3  
4  
5  
Toolkit toolkit = Toolkit.getDefaultToolkit();  
Image image = toolkit.getImage("sprites/others/cursor.png");  
Point hotSpot = new Point(0,0);  
Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Cursor");
frame.setCursor(cursor);


This piece of code is in the game's engine where the JFrame and the canvas is created and handled, just before the "while(isRunning)" loop. Thanks Smiley

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2012-04-03 19:31:12 »

Maybe the image isn't found.

Don't use toolkit.getImage. Use:
1  
BufferedImage image = ImageIO.read(getClass().getClassLoader().getResource("path/from/root"));

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #6 - Posted 2012-04-03 19:44:56 »

ra4king, just take my money.  Tongue

That worked perfectly! Thanks mate Smiley

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #7 - Posted 2012-04-03 22:07:06 »

Hahaha glad to help Smiley

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #8 - Posted 2012-04-04 14:25:17 »

I changed the title to solved so this thread can rest in peace Smiley

Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #9 - Posted 2012-04-04 23:30:03 »

I wanted to leave this here for the records Smiley

The custom cursor must be 32x32 pixels, otherwise it will be resized by the Toolkit. The best thing to do is make your it in a 32x32 image, put your custom cursor in the upper left of it, and set the rest transparent.

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.

atombrot (23 views)
2014-08-19 09:29:53

Tekkerue (22 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (57 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (36 views)
2014-08-06 19:49:38

BurntPizza (66 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!