Tuesday, November 12, 2013

Robot in Java

Robot is an automation class in AWT package of Java. The Robot class is useful when your Java programs requires test automation, mouse, and keyboard controls. The frequently used methods of the Robot class are shown in the table below.

Method Description
createScreenCapture(Rectangle r) This method returns the image (BufferedImage object) of the captured screen. The dimension of the image is specified by the rectangle r argument.
delay(int mills) The delay method will delay the robot before an event occurs.
getPixelColor(int x,int y) The getPixelColor returns the color at the specified coordinate (x,y) on the screen.
keyPress(int key) This method tells the robot to press a key specified by the key argument. The KeyEvent class defines key constants that can be used in this method.
mouseMove(int x,int y) The mouseMove method will move the mouse pointer to the coordinate (x,y) on the screen.
mousePress(int buttons) This method tells the robot to press the mouse button. The value of buttons argument can be found in the InputEvent class.

In the example code below, the robot is commanded to move to the start button on the Task Bar of Window 7 and the mouse is clicked there. Then the word notepad are typed in the search box and the Enter key is pressed to open the NotePad application.

public class RobotDemo {
public static void main(String[] args){
try {
Robot r=new Robot();
//move to the start button on the Task Bar
r.mouseMove(20, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
r.mousePress(InputEvent.BUTTON1_MASK); //press the left button
//type notepad in the search box
r.keyPress(KeyEvent.VK_N);
r.keyPress(KeyEvent.VK_O);
r.keyPress(KeyEvent.VK_T);
r.keyPress(KeyEvent.VK_E);
r.keyPress(KeyEvent.VK_P);
r.keyPress(KeyEvent.VK_A);
r.keyPress(KeyEvent.VK_D);
//delay the key pressed
r.delay(1000);
//press enter key
r.keyPress(KeyEvent.VK_ENTER);

} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}



}


Another example is an easy screen capture program. To capture any part of the screen, you will drag the transparent capturing rectangle to a location on the screen where you want to capture. Then press the Enter key to capture the screen. In default, the captured area is 400 pixels wide (width) and 300 pixels long (height). If you wan to increase the dimension of the area to be captured, you will press the ] symbol key. Similarly, to decrease the dimension, simply press the [ symbol key. To minimize the transparent capturing rectangle, press the minus (-) key. When your work completes, you can press the ESC key to exit the program.
The images of the captured screen are saved in the current working directory of the program.

Robot screen capture



import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;

class InUI extends JFrame{
int wWin;
int hWin;
int xWin;
int yWin;
InUI(){

wWin=400;
hWin=300;
setSize(wWin,hWin);
Point loc=MouseInfo.getPointerInfo().getLocation();
xWin=loc.x;
yWin=loc.y;
setLocation(xWin,yWin);
addKeyListener(new KeyDetector());
addMouseMotionListener(new MouseMove());
setUndecorated(true);
setOpacity(0.7f);
Container cont=getContentPane();
cont.setLayout(new BorderLayout());
JLabel lbl=new JLabel("<html>Click and drag this capture area.<br>Press Enter to capture the screen.</html>");
lbl.setHorizontalAlignment(JLabel.CENTER);
cont.add(lbl, BorderLayout.CENTER);
setVisible(true);

}

class KeyDetector extends KeyAdapter{
public void keyPressed(KeyEvent e){
System.out.println(e.getKeyCode());
int code=e.getKeyCode();
if(code==27) //exit the program when the Esc key is pressed. 
System.exit(0);
else if(code==45) //minimize the capture
minimize();
else if(code==91) //decrease capture area
decreaseSize();
else if(code==93) //increase capture area
increaseSize();
else if(code==10){ //capture the screen when Enter key is pressed
minimize();
capture();
}
//else if((code==KeyEvent.VK_X) && (e.getModifiers() & KeyEvent.ALT_MASK)!=0 )
//System.exit(0);

}
}
class MouseMove extends MouseMotionAdapter{
public void mouseDragged(MouseEvent e){
//change location of the capture area when mouse is dragged
setWinLocation(e.getXOnScreen(),e.getYOnScreen());
}
}
public void increaseSize(){
Dimension ds=Toolkit.getDefaultToolkit().getScreenSize();
if(wWin<ds.getWidth()){
wWin+=5;
this.setSize(wWin,hWin);
}
if(hWin<ds.getHeight()){
hWin+=5;
this.setSize(wWin,hWin);
}

}
public void decreaseSize(){
if(wWin>5){
wWin-=5;
this.setSize(wWin,hWin);
}
if(hWin>5){
hWin-=5;
this.setSize(wWin,hWin);
}
}
public void setWinLocation(int x,int y){
xWin=x-wWin/2;
yWin=y-wWin/2;
this.setLocation(new Point(xWin,yWin));
}
public void minimize(){
this.setExtendedState(JFrame.ICONIFIED);
}
public void capture(){
try {
Robot rb=new Robot();
//capture the target part of the screen
BufferedImage bi=rb.createScreenCapture(new Rectangle(xWin,yWin,wWin,hWin));
//save the image
ImageIO.write(bi, "png", new File(System.getProperty("user.dir")+File.separator+"image"+System.currentTimeMillis()+".png"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

public class RobotDemo {
public static void main(String[] args){
new InUI();
   }
}

1 comment:

  1. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from Pprogramming

    ReplyDelete