Moving the mouse cursor programatically

From JDK1.3, you can use the robot class, very useful for testing purposes.
Following example shows you how to take a desktop screenshot (0,0) –> (200,200)
as well as manipulating the mouse cursor. The cursor will traverse diagonally
from the top left corner to the bottom right.

import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main() throws Exception {
      Robot robot = new Robot();
      BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 200, 200));
      getContentPane().add(new JLabel(new ImageIcon(image)));
      //setSize(200, 200);
      pack();
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public void animateMouse() throws Exception {
      Robot robot = new Robot();
 
      double w = getSize().getWidth();
      double h = getSize().getHeight();
 
      double x1=0, y1=0;
      double dx = w/h;
      double dy = h/w;
      if (dx > dy) dx = 1; else dy = 1;
      do {
         robot.mouseMove((int) x1, (int) y1);
         x1 += dx;
         y1 += dy;
 
         try { Thread.sleep(10); } catch(InterruptedException e) { }
      } while (x1 < w || y1 < h);
   }
 
   public static void main(String args[]) throws Exception {
      Main main = new Main();
      main.show();
      main.animateMouse();
   }
}