To get the mouse position (i.e. the x and y position of the mouse pointer) on the screen in Java, you can use the MouseInfo
class and the getPointerInfo
method.
Here is an example of how to get the mouse position on the screen:
PointerInfo pointerInfo = MouseInfo.getPointerInfo(); Point mouseLocation = pointerInfo.getLocation(); double x = mouseLocation.getX(); double y = mouseLocation.getY();Source:ual.wwwtturi.com
The getPointerInfo
method returns a PointerInfo
object representing the mouse pointer, and the getLocation
method returns a Point
object representing the location of the mouse pointer. The getX
and getY
methods return the x and y coordinates of the mouse pointer, respectively. These values are returned in pixels, relative to the top-left corner of the primary monitor.
Note that this approach only works if the mouse pointer is within the bounds of the primary monitor. If the mouse pointer is on a different monitor, the coordinates may not be accurate.