0

I am trying to get the coordinates of the mouse every time it is moved at any point on the screen and then log the coordinates, but I am a bit lost on how to do it.

At the moment, I'm trying to use the MouseListener, so is there anyway to make an overlay on the entire screen that is transparent, click-throughable, and able to capture mouse events?

Any help is appreciated, thanks.

  • There's no listener that will do this automatically while the mouse is out side of a window created by Java, instead, you need to use PointInfo, for [example](http://stackoverflow.com/questions/13061122/getting-rgb-value-from-under-mouse-cursor/13061320#13061320), [example](http://stackoverflow.com/questions/18885247/how-do-i-make-my-jwindow-window-always-stay-focused/18885346#18885346), [example](http://stackoverflow.com/questions/21585121/java-getting-mouse-location-on-multiple-monitor-environment/21592711#21592711) – MadProgrammer Dec 13 '15 at 20:18
  • Or [example](http://stackoverflow.com/questions/18158550/zoom-box-for-area-around-mouse-location-on-screen/18158845#18158845) – MadProgrammer Dec 13 '15 at 20:19

2 Answers2

0

Use a MouseMotionListener with a mouseDragged method like :

public void mouseDragged(MouseEvent e) {
    Graphics g = this.getGraphics();
    int x = e.getX(), y = e.getY(); // you have the coordinates
  // if you want to draw a line for example between 2 mouse pos:
    g.drawLine(lastX,lastY,x,y);
    lastX =x;
    lastY =y;
}
Gerard Rozsavolgyi
  • 4,834
  • 4
  • 32
  • 39
0

You need to follow following steps,

Step : 1 : implements MouseMotionListener Interface to your class,

Step : 2 : Override it's 2-method

  1. mouseDragged
  2. mouseMoved

Step : 3 : put do following code into mouseMoved(...),

here, just for explanation purpose i have taken 2-label which will show current mouse's position.

@Override
    public void mouseMoved(MouseEvent e) {
        String xvalue = new Integer(e.getX()).toString();
        String yvalue = new Integer(e.getY()).toString();
        xlable.setText(xvalue); // here , you can write it into you log
        ylable.setText(yvalue); // here , you can write it into you log
    }

Out-put of my Effort,

enter image description here

Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55