/** * testRobot.java * @author: Stephen Steffes * @version: 1.0 * March 1, 2006 * Purpose: Example using java robot. Moves mouse around in a circle, opens notepad, types "hello world", take a screen grab, reports pixel color at (0,0). */ import java.awt.*; //Contains Robot and Color import java.awt.event.*; //Contains KeyEvent import java.io.*; //Contains println import java.lang.*; //Contains Math public class testRobot { //User variables public static int STD_DELAY=100; //standard delay number to use for pressing keys (milli sec) public static double RADIUS=100; //radius of circle to outline with mouse (pixels) public static int OFFSETX=300,OFFSETY=300; //(x,y) offset to put center of circle //Key binding variables public static int PLUS=107, LEFT=37, UP=38, RIGHT=39, DOWN=40, NUMPAD5=101, PERIOD=46, PRINT_SCREEN=154; //System variables public static Robot robot; public static Color c; public static void main(String[] args) throws AWTException{ //set up variables robot = new Robot(); //wait an initial 1 sec robot.delay(1000); //move mouse around in a circle int x,y; for(double angle=0;angle<360;angle++){ x=(int)(RADIUS*Math.cos(angle*Math.PI/180))+OFFSETX; y=(int)(RADIUS*Math.sin(angle*Math.PI/180))+OFFSETY; robot.mouseMove(x,y); System.out.println("Moving mouse to (" +x+", "+y+")"); robot.delay(10); } //open notepad combo(KeyEvent.VK_WINDOWS,KeyEvent.VK_R); type("notepad"); press(KeyEvent.VK_ENTER); //hello world type("hello"); press(KeyEvent.VK_SPACE); type("world"); //report color at (0,0) c=robot.getPixelColor(0,0); //get (0,0) pixel System.out.println("Color of pixel at (0,0) (upper left corner of screen) is Red: "+c.getRed()+", Green: "+c.getGreen()+", Blue: "+c.getBlue()); //take screen grab screenGrab(); } ////////////////////////////////////////////// //Press c-'a', (ex. control-a) public static void combo(int c,int a){ robot.keyPress(c); press(a); robot.keyRelease(c); } ////////////////////////////////////////////// //Take a screen grab public static void screenGrab(){ combo(KeyEvent.VK_CONTROL,PRINT_SCREEN); System.out.println("Screen shot waiting on clipboard."); } ///////////////////////////////////////////////// //Type a series of letters represented in the String 'a'. //This will only type actual letters (lower case only), no white spaces or non-alphabet characters public static void type(String a){ for(int i=0; i