Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] hide the Cursor???

Started by lldanll Jul 10, 2004 at 5:50 PM 5 replies 2.8k views
Original Post
lldanll
lldanll
Is it possible to Hide the Cursor with a Java Application.. I am playing around with a game where i want to have cross hairs I drew used as the aimer which follows the mouse pointer around on the screen...is there a way where I can hide the cursor so the user does not see their mouse pointer when they scroll over the window and only see the cross hairs? or would i just have to make a custom cursor with my pic somehow and have use the frame.setCursor(whatever the set to custom cursor line is)...
Sir Sapo
Sir Sapo
There are 5 premade cursors in the Java API already, they are
CROSSHAIR_CURSOR a plus sign (i think this is the one you want)
DEFAULT_CURSOR the arrow
HAND_CURSOR a little gloved hand
TEXT_CURSOR that I shaped one
WAIT_CURSOR an hourglass


In order to use these crosshairs, Im assuming you game is an application, just call the setCursor method in your public static void main method.

If your application is called Game , then when you created the instance of it in your main method put this.

Game game = new Game();
game.setCursor(CROSSHAIR_CURSOR);

I haven't tried this yet , but I just read it in a book so it should work.
My Current Project Angels 22 (4E5)
capn_midnight
capn_midnight
Sir Sapo, close, that technique is deprecated, you should use instead
yourJFrame.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));

that is, to use the premade crosshair cursor.

skip putting the cursor in place yourself, use your picture as a custom cursor:
ImageIcon temp=new ImageIcon("yourImage.png");Cursor yourCursor=Toolkit.getDefaultToolkit().createCustomeCursor(temp.getImage(), new Point(temp.getWidth()/2, temp.getHeight/2), "YourCrosshairCursor");yourFrame.setCursor(yourCursor);

the new Point(int x, int y) refers to the "hotspot" on the cursor, i.e., the part that is actually the pointer. In your case, the Point is probably in the middle of the image.
lldanll
lldanll
Capn midnight the code you gave me to use a custom cursor worked great! thanks for the help!..

Sir Sapo thanks anyways, i already knew about the premade cursors but i wanted to use one i drew up myself..thanks for the help though
Rickmeister
Rickmeister
If you actually want to hide the cursor, make a transparent image and use it as a cursor..

BufferedImage cursor = new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);Cursor transparentCursor = Toolkit.getDefaultToolkit().CreateCustomCursor(cursor,new Point(0,0),"TransparentCursor");this.setCursor(transparentCursor);
lldanll
lldanll
Quote:
Original post by Rickmeister
If you actually want to hide the cursor, make a transparent image and use it as a cursor..

BufferedImage cursor = new BufferedImage(16,16,BufferedImage.TYPE_INT_ARGB);Cursor transparentCursor = Toolkit.getDefaultToolkit().CreateCustomCursor(cursor,new Point(0,0),"TransparentCursor");this.setCursor(transparentCursor);


Yeah thats what I ended up doin because the image i used as a crosshair looked like crap when it was used as a cursor.. it was all distorted and stuff so i just hide the cursor and used mouse motion listener and set the cross hairs to the mouse x and y

Topic Locked

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

Sign in to reply to this topic.