Groovy Documentation

com.wordpress.tipsforjava.swing
[Java] Class ScreenImage

java.lang.Object
  com.wordpress.tipsforjava.swing.ScreenImage

public class ScreenImage
extends Object

Convenience class to create and optionally save to a file a BufferedImage of an area on the screen. Generally there are four different scenarios. Create an image of: a) an entire component b) a region of the component c) the entire desktop d) a region of the desktop The first two use the Swing paint() method to draw the component image to the BufferedImage. The latter two use the AWT Robot to create the BufferedImage. The created image can then be saved to a file by usig the writeImage(...) method. The type of file must be supported by the ImageIO write method. Although this class was originally designed to create an image of a component on the screen it can be used to create an image of components not displayed on a GUI. Behind the scenes the component will be given a size and the component will be layed out. The default size will be the preferred size of the component although you can invoke the setSize() method on the component before invoking a createImage(...) method. The default functionality should work in most cases. However the only foolproof way to get a image to is make sure the component has been added to a realized window with code something like the following: JFrame frame = new JFrame(); frame.setContentPane( someComponent ); frame.pack(); ScreenImage.createImage( someComponent );

Authors:
Rob Camick
Darryl Burke


Field Summary
private static List types

 
Constructor Summary
ScreenImage()

 
Method Summary
static BufferedImage createDesktopImage()

Convenience method to create a BufferedImage of the desktop

static BufferedImage createImage(JComponent component)

static BufferedImage createImage(JComponent component, Rectangle region)

static BufferedImage createImage(Component component)

static BufferedImage createImage(Rectangle region)

Create a BufferedImage from a rectangular region on the screen.

static void layoutComponent(Component component)

static void writeImage(BufferedImage image, String fileName)

Write a BufferedImage to a File.

 
Methods inherited from class Object
wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll
 

Field Detail

types

private static List types


 
Constructor Detail

ScreenImage

ScreenImage()


 
Method Detail

createDesktopImage

public static BufferedImage createDesktopImage()
Convenience method to create a BufferedImage of the desktop
Throws:
AWTException see Robot class constructors
IOException if an error occurs during writing
Parameters:
fileName - name of file to be created or null
Returns:
image the image for the given region


createImage

public static BufferedImage createImage(JComponent component)


createImage

public static BufferedImage createImage(JComponent component, Rectangle region)


createImage

public static BufferedImage createImage(Component component)


createImage

public static BufferedImage createImage(Rectangle region)
Create a BufferedImage from a rectangular region on the screen. This will include Swing components JFrame, JDialog and JWindow which all extend from Component, not JComponent.
Throws:
AWTException see Robot class constructors
Parameters:
region - region on the screen to create image from
Returns:
image the image for the given region


layoutComponent

static void layoutComponent(Component component)


writeImage

public static void writeImage(BufferedImage image, String fileName)
Write a BufferedImage to a File.
Throws:
IOException if an error occurs during writing
Parameters:
image - image to be written
fileName - name of file to be created


 

Groovy Documentation