Monday 9 February 2015

Robot class in Java - Type automatically and Capture screen with Robot class

Welcome to Logically Proven Blog.
This post teaches you what Robot class is and how to make use of Robot class in Java. We are going to look at some examples how to use this Robot class.

Before jumping into the main concept, let me tell you about the package “java.awt”. Because the Robot class is a part of “java.awt” package.

What is a Package?

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders organized on your computer to keep images in one folder, videos in one folder. The software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things in an organized by placing then into related classed and interfaces into packages.



 The Java platform provides an enormous class library (a set of packages) suitable for use in your own applications. This library is known as the “Application Programming Interface” or “API for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the file system; a socket object allows for the creation and use of network sockets. This allows you, the programmer, to focus on the design and organize a particular application in a well manner, rather than the infrastructure required to make it work.
 
Package java.awt Description

This package contains all the classes for creating user interfaces and for painting graphics and images. A user interface object such as a button or a scrollbar is called, in AWT terminology, a component. The component class is the root of all AWT components.

Some components fire events when a user interacts with the components. For example, click on a button or mouse hover on a particular component. The AWTEvent class and its subclasses are used to represent the events that AWT components can fire.

 A container is a component that can contain components and other containers. A container can also have a layout manager that controls the visual placement of components in the container. The AWT package contains several layout manager classes and an interface for building your own layout manager.

Each Component object is limited in its maximum size and its location because the values are stored as an integer. Also, a platform may further restrict maximum size and location coordinates. The exact maximum values are dependent on the platform. There is no way to change these maximum values, either in Java code or in native code. These limitations also impose restrictions on component layout. If the bounds of a Component object exceed a platform limit, there is no way to properly arrange them within a Container object. The object's bounds are defined by any object's coordinate in combination with its size on a respective axis.

java.awt.Robot

public class Robot extends Object

Class object is the root of the class hierarchy. Every class has object as a super class. All objects, including arrays, implements the methods of this class.

Robot class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

AWTException - if the platform configuration does not allow low-level input control. This exception is always thrown when GraphicsEnvironment.isHeadless() returns true.

SecurityException - if createRobot permission is not granted

 Sample programs that make use of “Robot” class

Typing on computer without typing manually

This program opens an editor like Notepad where the program will start typing automatically.

To open an editor-
 Runtime.getRuntime().exec("notepad.exe");

To type in notepad, we can make use of Robot class.
Thread.sleep(500);
r.keyPress(KeyEvent.VK_H);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_I);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_R);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_B);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
r.keyPress(KeyEvent.VK_T);
A Starting delay of 5 seconds because to give a time to open the notepad editor, else our program writes anywhere else. After each letter some delay seems to be somebody typing.

Screenshot

Follwing is the program to capture a screen of particular dimension.

Dimension d_size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(d_size));
File path_to_save = new File("Robot.jpg");
ImageIO.write(img, "JPG", path_to_save); 
Here Dimension gets your screen size and puts dimension in the d_size variable. createScreenCapture function takes a screenshot. ImageIO.write is responsible for writing the screenshot as a JPG file.

 Get Pixel Color

Using this program you can get the color of a pixel at the given screen coordinates.
The parameters are the X and Y positions of pixel.
public Color getPixelColor(int x,int y)
Similarly you can use the Robot class for moving the mouse to avoid the system from sleeping in certain cases, press the mouse buttons, key press, key release etc.

Please refer Robot class in java.awt package for more details,

Credits: Oracle
 
Please write your comments if you find anything is incorrect or do you want share more information about the topic discussed above.

Logically Proven,
Learn, Teach, Share

Karthik Byggari

Author & Editor

Computer Science graduate, Techie, Founder of logicallyproven, Love to Share and Read About pprogramming related things.

2 comments:

 
biz.