How to take a screenshot using Robot
BufferedImage createScreenCapture(Rectangle rect)
This method takes java.awt.Rectangle as parameter. You can use any of the seven constructors of this class to create it's object, however i don't recommend the first one which just creates a Rectangle object that is invisible when used to capture the screen. All the constructors do the same thing, their parameters differ instead. For brevity and ease, in this post i will be using the third constructor which takes 4 integer values which represent the x, y, width, height respectively. The returned
java.awt.image.BufferedImageobject is returned. This is used to write the image to a file. Here is the example.
public static void main(String args) throws Exception
// Throws AWTException
Robot rb=new Robot();
Scanner s=new Scanner(System.in);
System.out.println("Enter the x,y,width,height of capture");
// Get the BufferedImage object for writing to a fiel
BufferedImage bim=rb.createScreenCapture(new Rectangle(x,y,width,height));
/* Throws IOException
* Write in PNG format. You can choose whatever format you want.
* Write the image.
* If you know the extension of the format.
* Write to a file specified