I get the mouse coordinates using the MouseInfo.getPointerInfo (). GetLocation (). X; method; but it displays the real coordinates of the cursor on the screen (for example, I have windows with an image. Moving the window to the right side of the screen and clicking on the image, and then doing the same thing only by moving the window to the left side I get different values.) I also need get the coordinates of the cursor relative to the window (if I click on the upper left corner of the window, the method should return zero coordinates).

  • Find out the position of the window relative to the monitor and simple mathematical actions ; find out the coordinates you need - JVic
  • and what are these actions? - Dmitriy Mironov

1 answer 1

An important question is where do you get the coordinates? If in the event handler of the mouse, it is better to use the getX() and getY() methods of the MouseEvent event. If you still need to use MouseInfo.getPointerInfo().getLocation() , then there is an easy way to convert screen coordinates to window coordinates:

 SwingUtilities.convertPointFromScreen(MouseInfo.getPointerIn‌​fo().getLocation(), component)