2

I wanted to ask if there Is any way to get the Coordinates my coursor has within my window?

like "e.getX()" when using an Event, but without having to use an event?

or is there any sort of algorithm like getting the absolute position of the mouse and subtracting the position of the Frame?

Thanks Already and sorry for my bad english.

Topher
  • 53
  • 1
  • 8
  • Try http://stackoverflow.com/questions/1439022/get-mouse-position – khelwood Mar 21 '15 at 20:18
  • Either use a MouseListener on the component you're interested in monitoring or an AWTListener to monitor global events occurring within your application. However, MouseEvents are contextual to the component that generated it – MadProgrammer Mar 21 '15 at 20:25
  • @khelwood Nah, this would involve the use of a Timer or Thread and would need you to convert the screen position to a relative position within the window. Frankly, been lazy and all, there are simpler solutions, since the op only wants to monitor their application window... – MadProgrammer Mar 21 '15 at 20:27
  • For [example](http://stackoverflow.com/questions/25631731/java-mouse-events-not-registering-on-osx/25632358#25632358) and [example](http://stackoverflow.com/questions/16639950/jframe-glasspane-is-also-over-jdialog-but-shouldnt/16641115#16641115) – MadProgrammer Mar 21 '15 at 20:36

2 Answers2

2

Ok Guys i found the solution now:

 MouseInfo.getPointerInfo().getLocation().x - XYZ.getLocationOnScreen().x
 MouseInfo.getPointerInfo().getLocation().y - XYZ.getLocationOnScreen().y

with XYZ being the reference to the Frame / Panel / whatever being used

Topher
  • 53
  • 1
  • 8
0

From what I can remember, I don't think there is a way to get the mouse coordinates without an event. I think your best bet is to use a public variable (mouseX, mouseY) and set them equal to event.getX() and event.getY(), that way, the mouse X and Y can be easily used throughout your program.

Cj1m
  • 805
  • 3
  • 11
  • 26
  • i know you can use "MouseInfo.getPointerInfo().getLocation().x" to get the mouse coordinates on the screen but is there a way to get it from within a window? – Topher Mar 21 '15 at 20:28