Class ScreenImage
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedImage
Convenience method to create a BufferedImage of the desktopstatic BufferedImage
createImage
(Component component) static BufferedImage
createImage
(Rectangle region) Create a BufferedImage from a rectangular region on the screen.static BufferedImage
createImage
(JComponent component) Create a BufferedImage for Swing components.static BufferedImage
createImage
(JComponent component, Rectangle region) Create a BufferedImage for Swing components.static void
writeImage
(BufferedImage image, String fileName) Write a BufferedImage to a File.
-
Constructor Details
-
ScreenImage
public ScreenImage()
-
-
Method Details
-
createImage
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
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 fromregion
- The region of the component to be captured to an image- Returns:
- image the image for the given region
-
createDesktopImage
Convenience method to create a BufferedImage of the desktop- Returns:
- image the image for the given region
- Throws:
AWTException
- see Robot class constructorsIOException
- if an error occurs during writing
-
createImage
- Throws:
AWTException
-
createImage
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
Write a BufferedImage to a File.- Parameters:
image
- image to be writtenfileName
- name of file to be created- Throws:
IOException
- if an error occurs during writing
-