Executing Print Screen option using java

Description

PrintScreen.java file enables the print screen option in java using Robot object that captures the screen periodically (1 sec) and store it as image.

  • Filename:PrintScreen.java

import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Robot;
import java.io.File;
import javax.imageio.ImageIO;

class PrintScreen {
PrintScreen(int i) {
try {
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
Rectangle rect = new Rectangle(d.width,d.height);
Robot robot = new Robot();
File f = new File("image"+i+".jpg");
BufferedImage img = robot.createScreenCapture(rect);
ImageIO.write(img,"jpeg",f);
tool.beep();

}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
try {
Thread.sleep(5000);
for (int n=0;n<3;n++) {
Thread.sleep(1000);
PrintScreen sr = new PrintScreen(n);
}
}
catch(Exception e) {
System.out.println(e);
}
}

}