how-to

Graphics

How to get current mouse screen position ?

For this sample you must use:

import java.awt.*;

This sample code writes current mouse screen position. You obtain it as java.awt.Point class.

PointerInfo pi = MouseInfo.getPointerInfo();
if ( pi != null )
  System.out.println( pi.getLocation().toString() );

The output could be:

java.awt.Point[x=254,y=271]

The java.awt.MouseInfo.getPointerInfo() could returns null on system without a mouse.