Java - setting the mouse cursor?

Started by
8 comments, last by Trond A 20 years, 1 month ago
I''m having a hard time setting the mouse cursor to a spot, using Java. Talking about the Windows-cursor (or whatever OS), not a program specific one... I''m not trying to _get_ the coordinates (A few ppl I''ve asked seem to think so, dunno if it''s due to my ability to express myself...) -Trond
-Trond
Advertisement
I think you have to create a java.awt.Cursor object either with Cursor.getPredefinedCursor() or Cursor.getSystemCustomCursor() or java.awt.Toolkit.createCustomCursor().

Then you call the setCursor() method of your window and it should do the trick.
Well you are being a little ambiguous. What do you mean by ''setting the mouse cursor to a spot''? You have a graphic of a spot which you want to use as the cursor? Or you want to move the cursor to specific screen coordinates?
Thanks for the quick replies!

I have checked out the Cursor class and added an object of it to the frame, but it doesn''t have any set-functions...

I''m trying to put the cursor on a specific spot on the screen. I am trying to control a first-person camera using the mouse, and need to keep the cursor inside my window, or else the screen edges will block the movement. Have to reset the mouse after each time I''ve checked the movement difference since last frame.

-Trond
-Trond
In AWT and Swing, every top level container (Frame, Dialog, JFrame, JDialog, JWindow) inherits from java.awt.Window which exposes a setCursor() method. So your frame should have a setCursor() method, unless you are using another GUI library.

What error did you get ? I never used custom cursors so I may not be able to help you much, but you could post the code if it is short enough.
You''re looking for java.awt.Robot
overnhet: Yes, I''ve tried to use them. They do have a setCursor-command, but that''s just the cursor appearance I think. I couldn''t figure out how to set the actual cursor _position_.

OrangyTang: Yes! That worked! Excellent Thanks :D

Thanks all of you for your time!
-Trond
-Trond
Now I understand. You previous post wasn''t really clear (to me) about what didn''t work.

OTOH I wouldn''t implement it that way, if I had to. I would rather :
* turn mouse cursor off
* keep tracks of the previous mouse coordinates,
* on mouse motion event : compute dx, dy
* update camera orientation / position accordingly

Setting the mouse cursor to particular position continuously is not a bad idea, but AFAIK the Robot will generate new events when you use its methods, which should add a bit of overhead and may or may not suit your needs.
This is how I do it now. I though about making a transparent cursor for the application, it''s kind of annoying when it''s in
the middle of the view all the time.

public void mouseMoved(MouseEvent p_mevt)	{		if (!mousemovement)		{			return;		}		float l_x = p_mevt.getX();		float l_x_diff;		float l_y = p_mevt.getY();		float l_y_diff;//I set registermouse to false when I call the//moveMouse() function, so it won''t screw up//my rotation		if (!registermouse)		{			registermouse = true;			mouse_prev_x = l_x;			mouse_prev_y = l_y;			return;		}		float mouse_speed = 0.01f;		//horizontal rotation		if (mouse_prev_x >= 0)		{			l_x_diff = l_x - mouse_prev_x;			if (l_x_diff < -20)			{				l_x_diff = -20;			}			if (l_x_diff > 20)			{				l_x_diff = 20;			}			camera_direction_lr -= (l_x_diff * mouse_speed) * (500 / (float)getWidth());//The last part here (500/width) is to get equal mouse//sensitivity on all window resolutions...		}		//vertical rotation		if (mouse_prev_y >= 0)		{			l_y_diff = l_y - mouse_prev_y;			if (l_y_diff < -20)			{				l_y_diff = -20;			}			if (l_y_diff > 20)			{				l_y_diff = 20;			}			camera_direction_ud -= (l_y_diff * mouse_speed) * (500 / (float)getHeight());						if (camera_direction_ud <= -Math.PI / 2)			{				camera_direction_ud = - (float)Math.PI / 2;			}			if (camera_direction_ud >= Math.PI / 2)			{				camera_direction_ud = (float)Math.PI / 2;			}		}		try		{//mouseMove gives us a new event, and if we handle that//we''re back were we started...registermouse = false;			Robot rbt = new Robot();//put mouse cursor in window center (approx)			rbt.mouseMove((int) (getSize().getWidth() / 2 + getLocation().getX()), (int) (getSize().getHeight() / 2 + getLocation().getY()));		}		catch (Exception e)		{		}	}


overnet:
The mouse-move command does create new events, and I just "filtered" them out by setting the "registermouse" boolean.

quote:
OTOH I wouldn''t implement it that way, if I had to. I would rather :
* turn mouse cursor off
* keep tracks of the previous mouse coordinates,
* on mouse motion event : compute dx, dy
* update camera orientation / position accordingly

That is basically what I''m doing. The reason why I need to reset the cursor, is that sooner or later it hits the screen edge, and then the rotation would stop. Of course, I cant rely on the user holding the mouse cursor inside the window (or desktop) bounds.

If you know of one way to actually get the mouse _movement_ by not comparing, I would be more than happy to know about it!

Thanks again for the input,
-Trond
-Trond
quote:Original post by Trond A
This is how I do it now. I though about making a transparent cursor for the application, it''s kind of annoying when it''s in
the middle of the view all the time.

According to Toolkit.createCustomCursor() doc, if you pass null for the image it should hide the cursor.

quote:Original post by Trond A
overnet:
The mouse-move command does create new events, and I just "filtered" them out by setting the "registermouse" boolean.

That is basically what I''m doing. The reason why I need to reset the cursor, is that sooner or later it hits the screen edge, and then the rotation would stop. Of course, I cant rely on the user holding the mouse cursor inside the window (or desktop) bounds.

I didn''t thought about it.

Unfortunately you have to use a trick like your "registermouse" boolean to handle mouse events generated by the Robot. Or you could make a quick rejection test by comparing mouse event coords against the frame center.

quote:Original post by Trond A
If you know of one way to actually get the mouse _movement_ by not comparing, I would be more than happy to know about it!

There is none I am aware of.

This topic is closed to new replies.

Advertisement