/* * Create a BufferedImage for Swing components. * All or part of the component can be captured to an image. * * @param component Swing component to create image from * @param region The region of the component to be captured to an image * @param fileName name of file to be created or null * @return image the image for the given region * @exception IOException if an error occurs during writing */ public static BufferedImage createImage(JComponent component, Rectangle region, String fileName) throws IOException { boolean opaqueValue = component.isOpaque(); component.setOpaque(true); BufferedImage image = new BufferedImage(region.width, region.height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); g2d.setClip(region); component.paint(g2d); g2d.dispose(); component.setOpaque(opaqueValue); ScreenCapture.writeImage(image, fileName); return image; }
/** * Create a BufferedImage from a rectangular region on the screen. * * @param region region on the screen to create image from * @param fileName name of file to be created or null * @return image the image for the given region * @exception AWTException see Robot class constructors * @exception IOException if an error occurs during writing */ public static BufferedImage createImage(Rectangle region, String fileName) throws AWTException, IOException { BufferedImage image = new Robot().createScreenCapture(region); ScreenCapture.writeImage(image, fileName); return image; }