In Java we can take screen shot by using classes and methods defined in java.awt and javax.imageio. In order to take a screen shot first we need to decide the dimension of the screen and create a rectangle with this dimension. There is a class called ToolKit used to take the dimension of the screen.
Toolkit toolkit = Toolkit.getDefaultToolkit();
Rectangle screenSize = new Rectangle(0,0, toolkit.getScreenSize ().width,toolkit.getScreenSize().height);
The class defined in java.awt package called Robot is used to define a robot for the given screen device. createScreenCapture method in Robot class can be used to create an image containing pixels read from the screen.
Robot robot = new Robot();
BufferedImage bfIimage = robot.createScreenCapture( screensize);
Now we can create an image file (.jpg, .png etc) using ImageIO API.
ImageIO.write(bfIimage, "png", new File("c://screenshot.png"));