Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Java: A platform for platforms?
The java.awt.Robot class provides several opportunities for fun. One opportunity involves building a screen-capture utility. This Java Fun and Games installment presents a utility that uses Robot to capture the contents of the primary screen device.
This installment diverges from my previous installments by not focusing on an applet implementation. Instead, this article presents my screen-capture utility as a Swing application. After introducing this application from a GUI perspective, I explain key areas of its implementation.
My Capture application reveals a GUI that lets you select a portion of a captured image, crop that image to the selection's contents, and save the resulting image to a jpeg file. Figure 1 shows Capture's GUI with an example capture.

Figure 1. A red-and-white dashed rectangle identifies the current selection
Capture's GUI consists of a menu bar and a scrollable window that reveals the captured image. As shown in Figure 1, a selection rectangle (that you drag with the mouse) identifies a rectangular area of the captured image.
The menu bar presents File and Capture menus:
Three source files describe Capture's GUI: Capture.java (starts the application and constructs the GUI), ImageArea.java (describes a component that displays, and lets you select part of and crop a screen capture), and ImageFileFilter.java (restricts file-chooser selections to directories and jpeg filenames). In the sections below, I excerpt code fragments from
these source files to illustrate how Capture works.
To capture the screen with the Robot class, Capture must first create a Robot object. The Capture class's public static void main(String [] args) method attempts to create this object by invoking Robot's public Robot() constructor. If successful, a reference to a Robot configured to the primary screen device's coordinate system returns. If the platform does not support low-level control (which
is true of an environment without a screen device), a java.awt.AWTException is thrown. A java.lang.SecurityException is thrown if the platform doesn't grant permission to create a Robot. Hopefully, you will not encounter either exception.
Assuming a Robot object is created, main() invokes the Capture class's constructor to create the GUI. As part of its GUI creation, Capture obtains the dimensions of the primary screen
device by invoking dimScreenSize = Toolkit.getDefaultToolkit ().getScreenSize ();. Because Robot's public BufferedImage createScreenCapture(Rectangle screenRect) method, which is used to perform a screen capture, requires a java.awt.Rectangle argument, the constructor converts the java.awt.Dimension object to a Rectangle object via rectScreenSize = new Rectangle (dimScreenSize);. The Capture.java excerpt below calls createScreenCapture() when the Capture menu item's action listener is invoked:
| Subject |
|
|
|
|
|
|
|
|
I really liked your stuffBy Anonymous on October 26, 2008, 4:43 amThanks buddy..this was very useful. Must really appreciate it.. that you take so much time to do this
Reply | Read entire comment
View all comments