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?

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Capture the screen

Build a screen-capture utility based on Java's Robot class

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

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.

Application GUI

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:

  • File offers Save As... and Exit menu items for saving the current capture to a jpeg file, via a file-chooser, and quitting Capture. Although you can directly select these menu items, you'll probably find it more convenient to use their Alt-S and Alt-X keyboard shortcuts.
  • Capture offers Capture and Crop menu items for capturing primary screen device contents and cropping a capture to the contents of the selection rectangle. As with File's menu items, these menu items have their own convenient keyboard shortcuts: Alt-C for Capture and Alt-K for Crop.


Application implementation

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.

Robotic screen capture

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:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

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

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources