![]() The big trick about getting the mouse cursor location in Java was learning about the Java MouseInfo class. I hope this Java mouse location tip is helpful. The MouseInfo class doesn't have that limitation, and can give you the mouse coordinates regardless of where the mouse pointer is position on the screen. If you're writing a normal Java/Swing application, you'll probably want to use the MouseListener or MouseMotionListener interfaces (or their corresponding "adapter" classes), but those are limited to only giving you the mouse coordinates when the mouse is within the borders of your application. That means you have to add the MouseListener to that JPanel to get the right coordinates. ![]() You probably paint on a JPanel that you added to the JFrame. ![]() The information returned by GetPointerInfo is associated with the most recent pointer message retrieved by the calling thread. That's because you get the mouse coordinates relative to the JFrame but you don't paint directly on the frame. getLocationOnScreen () // from Component class. emptytreermbselected ( Vector2 position ) Emitted when the right mouse button is pressed if right mouse button selection is active and the tree is empty. MouseInfo.getPointerInfo ().getLocation () and. If you want the position relative to your component (like given by MouseListeners). Use GetPointerFrameInfo to retrieve frame information associated with a message for a set of pointers. Emitted when the right mouse button is pressed in the empty space of the tree. getPointerInfo().getLocation() returns the position relative to the screen. Point p = MouseInfo.getPointerInfo().getLocation() Īfter that call you can access the mouse x and y coordinates as int values like this: GetPointerInfo retrieves information for a single pointer associated with a pointer message. I just ran into this problem in my Java Robot programming, and the short answer is, to get the current mouse cursor location/position, use the getPointerInfo method of the class, like this: For example, when my mouse cursor is at the coordinates (0,0) on the g2d canvas, MouseInfo.getPointerInfo (). Java mouse cursor location FAQ: How do I get the location of the mouse (mouse cursor) in Java?
0 Comments
Leave a Reply. |