Screen capture in java example (using Swing)

                Have you ever imagined that by writing a few lines of code in JAVA , you can actually take a snapshot of your computer screen(you can actually capture the screen).This really sounds amazing.You cannot only take a screenshot but also can save it in the desired image file format say .png or .jpeg file. The piece of code which I am going to take you through, captures the window in which you are working and saves it in the same directory.

Steps :
              These are the basic steps to capture the screen by creating a Java Desktop Application which on execution opens a windows form with button and on clicking the button, screen gets captured & saved

  • Make a new Java project
  • Then, make a new Java class which extends JFrame (a component of Swing)
  • Make a Default Constructor in this class
  • Take a JButton’s reference and add an ActionListener on the button
  • Add this Button to the JFrame
  • And then in the ActionListener method of the button, write the Java code.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;

public class HelloWorldFrame extends JFrame implements ActionListener {

    JButton b;//Reference of Button component of Swing

    public HelloWorldFrame() {
        b = new JButton("Click Here");
        b.setBounds(380, 290, 120, 60);// It changes layout related information and takes x-coordinate,y-coordinate,width and height as the parameters
        b.addActionListener(this);// Adding ActionListener on the Button
        setSize(1000, 700);// sets the width & height of the window

    public void actionPerformed(ActionEvent e)//This method is invoked when an action occurs 
        if (e.getSource() == b) //It matches the object on which the event occurred
            try {
                Toolkit tk = Toolkit.getDefaultToolkit(); //Toolkit class returns the default toolkit
                Dimension d = tk.getScreenSize();

//Dimension class object stores width & height of the toolkit screen
// toolkit.getScreenSize() determines the size of the screen

                Rectangle rec = new Rectangle(0, 0, d.width, d.height);  
//Creates a Rectangle with screen dimensions, here we are capturing the entire screen,if you want you can change it accordingly (i.e you can also capture a particular area on the screen)          
                Robot ro = new Robot(); //a very important class to capture the screen image
                BufferedImage img = ro.createScreenCapture(rec);

//createScreenCapture() method takes Rectangle class instance as argument and returns BufferedImage

                File f;
                f = new File("myimage.jpg"); // File class is used to write the above generated buffered image to a file
                ImageIO.write(img, "jpg", f);
//ImageIO is an API which is used to convert BufferedImage to a png/jpg image     
            } catch (Exception ex) {

    public static void main(String[] args) {
        HelloWorldFrame obj = new HelloWorldFrame();


Captured Screen

Saved Image                  
Download Source Code



  1. how do i set an if statement where i set a timer and it clicks a screenshot every 30 seconds automatically and stores it to a folder with a diffferent name every time?

  2. is it possible to put all of The Buffered Images to a single file so to make a seamless video output? And if so how will u capture images back to back without any delay so the video contains at least 30 frames per second?
    by the thank u for a nice tutorial (Y).

  3. but it just capturing the screen not the mouse location and pointer in the screen