Class ScreenImage

java.lang.Object
org.jjazz.uiutilities.api.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 );

FROM: https://tips4java.wordpress.com/2008/10/13/screen-image/ (camickr on SO)

  • Constructor Details

    • ScreenImage

      public ScreenImage()
  • Method Details

    • createImage

      public static BufferedImage createImage(JComponent component)
      Create a BufferedImage for Swing components. The entire component will be captured to an image.
      Parameters:
      component - Swing component to create image from
      Returns:
      image the image for the given region
    • createImage

      public static BufferedImage createImage(JComponent component, Rectangle region)
      Create a BufferedImage for Swing components. All or part of the component can be captured to an image.
      Parameters:
      component - Swing component to create image from
      region - The region of the component to be captured to an image
      Returns:
      image the image for the given region
    • createDesktopImage

      public static BufferedImage createDesktopImage() throws AWTException, IOException
      Convenience method to create a BufferedImage of the desktop
      Returns:
      image the image for the given region
      Throws:
      AWTException - see Robot class constructors
      IOException - if an error occurs during writing
    • createImage

      public static BufferedImage createImage(Component component) throws AWTException
      Throws:
      AWTException
    • createImage

      public static BufferedImage createImage(Rectangle region) throws AWTException
      Create a BufferedImage from a rectangular region on the screen.
      Parameters:
      region - region on the screen to create image from
      Returns:
      image the image for the given region
      Throws:
      AWTException - see Robot class constructors
    • writeImage

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