0
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
System.out.print("Y: "+y);
System.out.print("X: "+x+"\n");

Right now I'm using this, but it gives me the coordinates relative to the entire monitor. Since you can move the program frame anywhere on the monitor, this doesn't help me. How can I get the mouse's coordinates relative to the program window? For instance, the top left corner would be (0, 0) and the bottom right would be (400, 420)

3
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by Slyvr
0

Point b = a.getLocationOnScreen();

I changed it to this and I get an error to change to getLocation()
Does something need to be changed on PointerInfo a?

0

Huzzah, darwin fish for the win again, thanks Ezz
This works:

public void mouseClicked(MouseEvent e) {
	PointerInfo a = MouseInfo.getPointerInfo();
	Point point = new Point(a.getLocation());
	SwingUtilities.convertPointFromScreen(point, e.getComponent());
	x=(int) point.getX();
	y=(int) point.getY();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.