Easy way to find xy mouse coordinates?

So basically I am making a game in which the main class has a loop that runs 60 times per second

I need an easy way to find the xy coordinates of the mouse so that I can draw something along these specific coordinates.

Is there a good way to do this?

+3
source share
2 answers

This code usually doesn't work if you try to run it, but it shows you how to get the mouse position X and Y as integers

import java.awt.MouseInfo;    

public class testmouse {
    public static void main(String[] args){
        int mouseY = MouseInfo.getPointerInfo().getLocation().y;
        int mouseX = MouseInfo.getPointerInfo().getLocation().x;            
    }
}
+5
source

Add MouseMotionListenerto the game zone and see mouseMoved(MouseEvent).

+2
source

All Articles