Hi Friends, Today I will tell you how to make a program which will help you obtain screenshots of your screen and store them as an image .This logic can be extended to implement a Screen Recorder (You may make the program take screenshots at a regular interval using concepts of loop and sleep method in Thread class. After you obtain a sequence of image you may combine them in one video file).
First of all you need to import necessary classes:
import java.awt.Dimension; 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;
Dimension class is used for storing the screen coordinates, rectangle is used for defining a rectangle which will take the screen size with help of dimension object, robot class is responsible for taking of screenshots and toolkit helps to obtain the screen size.
Now, we define the class and the main function:
public class ScreenCapture {
public static void main(String args[])
{
try
{
After this first we will obtain the size of the screen say “x” so that we may tell the robot class that it has to capture a screen of size “x”.
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Now when we have obtained the screen size we have stored that in variable named size. Now we will define a Robot class which will take the screenshot.
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
Here we took a screenshot with help of robot.createScreenCapture(new Rectangle(size)).Rectangle object is made to tell the robot class the size of the screen.We store this in a BufferedImage object so that it can be written to a file.
Now we will write this to an external file:
File save_path=new File("screen.jpg");
ImageIO.write(img, "JPG", save_path);
}
catch(Exception e)
{
System.out.println(“Problem”);
}
}
}
Here we will make a file object which will make a new file called screen.jpg .Now we write the image from the buffered image object and take the file format for saving as JPG.
After we run the program it will capture a screenshot and store that in a file called screen.jpg

import java.awt.Dimension;
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;
public class ScreenCapture {
public static void main(String args[])
{
try
{
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
File save_path=new File("screen.jpg");
ImageIO.write(img, "JPG", save_path);
}
catch(Exception e)
{
}
}
}







See the prices for this post in Mr.Bool Credits System below: