how-to

Graphics

How to capture screenshot

For this sample you must use:

import java.awt.*;
import javax.imageio;

You can make screenshot and save it into file:

try {
  /* -- create robot */

  Robot r = new Robot();

  /* -- save part of screen into image */

  BufferedImage image = r.createScreenCapture( new Rectangle( 200, 200, 250, 250 ) );

  try {
    /* - save image into .jpg file */
    ImageIO.write( image, "jpg", new File( "c:\\test.jpg" ) );
  } catch (IOException ex) {
    Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
  }

} catch (AWTException ex) {
  Logger.getLogger(Window.class.getName()).log(Level.SEVERE, null, ex);
}

This example save only a small part (50x50 pixels) of the screen.

When you need to save whole screen use this Robot.createScreenCapture() code :

BufferedImage image = r.createScreenCapture( new Rectangle( Toolkit.getDefaultToolkit().getScreenSize() ) );