Thursday, May 15, 2008

Robot Class in Java

Robot class extends Object class. It is used to generate system input events for the purposes of self-running demos, test automation, and other applications where mouse and keyboard controls are needed. The main purpose of Robot class is to provide automated testing of Java platform implementations.

Using this class we can generate input events that differ from posting events to the Abstract Windowing Toolkit (AWT) event queue. For example, the method Robot.mouseMove will move the cursor instead of just generating a mouse move event.

But some platform requires special privileges to access low level input control. An AWTException will be thrown if the present platform configurations do not allow input control, when trying to construct the Robot instances. For example, systems like X-Window will throw the exception if XTEST 2.2 standard extension is not enabled(or not supported) by the X-server.

If we are using Robot class for purposes other than self testing must handle these error conditions by using the exception handling mechanisms.

Java Robot objects have the ability to take control away from the user, so we need to be a little careful. For example, if we allow our Java program that uses Robot class to run to an infinite loop, entering keystrokes, clicking the mouse, and making mouse moves, we will find that the only way to regain control of our system is to either switch off the power or press reset button to force our computer to restart.

The Robot class provides many instance methods by which we can produce mouse and keyboard input, just as the inputs being provided by a user. Some most used instance methods available in Robot class are:

createScreenCapture ( Rectangle screenRect )

Returns a BufferedImage reference value. This method is used to take the screen shot of the screen within the specified pixel values.

mouseMove ( int x, int y )

To move the mouse pointer to a specified screen coordinates.

mousePress ( int buttons )

To Press one of the buttons of mouse.

mouseRelease ( int buttons )

To release one of the buttons of mouse.

keyPress ( int keycode )

To Press a specified key of the keyboard.

keyRelease ( int keycode )

To release a specified key of the keyboard.

getPixelColor ( int x, int y )

To get the color of a pixel at the specified screen coordinates.

setAutoDelay ( int ms )

To Set the time in milliseconds this Robot has to sleep after generating an event.
Julian is a professional content writer in Java Development and now working at Telious Technologies.

Article Source: http://EzineArticles.com/?expert=Julian_Jaic

0 comments:

Post a Comment