How do I create mouse event using Robot class?

In this example we are automating the process of creating mouse event using the java.awt.Robot class.

package org.kodejava.example.awt;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class MovingMouseDemo {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // Move mouse cursor to 200, 200
            robot.mouseMove(200, 200);

            // Press the mouse button #1.
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);

            // Scroll the screen up for a mouse with a wheel support.
            robot.mouseWheel(-100);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}

How do I get the colour of a screen pixel?

The example here show us how to get the color of a pixel in the screen. We use the Robot.getPixelColor(int x, int y) method to obtain the Color of the pixel.

package org.kodejava.example.awt;

import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;

public class ColorPickerDemo {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();

            // The the pixel color information at 20, 20
            Color color = robot.getPixelColor(20, 20);

            // Print the RGB information of the pixel color
            System.out.println("Red   = " + color.getRed());
            System.out.println("Green = " + color.getGreen());
            System.out.println("Blue  = " + color.getBlue());

        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}