// purpose: take snapshots of images import java.io.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; import javax.imageio.*; public class Capturer { public static final String DEFAULT_FILENAME = "cs1112-draw.jpg"; // method takeSnapshot(): saves a snapshot of the image to default file public static void takeSnapshot( BufferedImage image ) { Capturer.takeSnapshot( image, DEFAULT_FILENAME ); } // method takeSnapshot(): saves a snapshot of the image to filename public static void takeSnapshot( BufferedImage image, String filename ) { try { File f = new File( filename ); ImageIO.write( image, "jpg", f ); } catch ( Exception e ) { System.out.println( "Unable to capture image and save to " + filename ); } } // method takeSnapshot(): takes a snapshot of the pane and calls it filename public static void takeSnapshot( Container pane, String filename ) { // determine region on desktop showing the drawing Point origin = new Point(); SwingUtilities.convertPointToScreen( origin, pane ); Rectangle region = new Rectangle( origin, pane.getSize() ); // capture region as an image try { BufferedImage image = new Robot().createScreenCapture( region ); // save image to file File f = new File( filename ); ImageIO.write( image, "jpg", f ); } catch ( Exception e ) { System.out.println( "Unable to capture image and save to " + filename ); } } // method takeSnapshot(): takes a snapshot of the pane to default file public static void takeSnapshot( Container pane ) { Capturer.takeSnapshot( pane, DEFAULT_FILENAME ); } }