Search code examples
javapositionmouse

(Java) Get Mouse Coordinates Within Window


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.


Solution

  • 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