Have you wondered how tools like Snagit capture the display screen and create an image file. Doing that using java AWT is very easy. Just few lines of code. Also you can save the image as JPG, GIF, PNG etc. by just changing the file format input parameter.
package com.livrona.snippets.gui;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
* This class captures the screen display to a image file
* using java.awt.Robot to capture it and them java.awt.ImageIO
* to write it to a file.
* @author mvohra
*
*/
publicclass ScreenShot {
publicstaticvoid main(String[] args) throws Exception {
// create a new instance, automation event generator
Robot robot =new Robot();
// capture the screen by getting the default screen size
BufferedImage screenShot = robot.createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
// write the capture image to a file using ImageIO
// format can be be JPG, GIF etc.
ImageIO.write(screenShot, "JPG", new File("screen-shot.jpg"));
}
}