Take Screen Shot Using Java Program

Here I have attached, Simple program about How to Take Screen Shot Using Java Program. The example program has been tested and output is shared.


package com.javatraineronline;

import javax.swing.JButton;
import javax.swing.JFrame;

public class MyScreenCaptureTool extends JFrame {

	private JButton captureButton;
	private String outputPath;

	public MyScreenCaptureTool(String title, String outputPath) {

		this.outputPath = outputPath;
		super.setSize(400, 150);

		// Defining the JButton Object
		captureButton = new JButton("Click here to Capture Screen");

		// ActionListener for button click event.
		captureButton.addActionListener(new ClickListener(this));

		// Adding the button object to JFrame

	public String getOutputPath() {
		return outputPath;

	public static void main(String[] args) {

		// Defining the MyScreenCaptureTool Object by passing 'Title' and 'Output Path' should be with '\\'
		new MyScreenCaptureTool("My Screen Capture Tool", "C:\\Users\\dineshkrishnan\\Desktop\\images\\"); // You can change accordingly



package com.javatraineronline;

import java.awt.AWTException;
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 java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;

public class ClickListener implements ActionListener {

	private MyScreenCaptureTool myScreenCaptureTool;
	public ClickListener(MyScreenCaptureTool myScreenCaptureTool) {
		this.myScreenCaptureTool = myScreenCaptureTool;
	public void actionPerformed(ActionEvent e) {
		// Getting the unique file name using java.util.UUID
		final String FILENAME = getUniqueFileName();
		//File Extension
		final String FILE_EXTENSION = ".png";
		try {
			// Delaying the screen capturing for 1 sec. to avoid JFrame to be captured in the screen shot 
			// Invoking the method
			if(captureScreen(myScreenCaptureTool.getOutputPath()+FILENAME+FILE_EXTENSION)) {
			} else {
		} catch (Exception e1) {
	// Method to get the random Unique file name
	private String getUniqueFileName() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();

	// Method to capture the screen
	private boolean captureScreen(String fileName) {

		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle screenRectangle = new Rectangle(screenSize);
		Robot robot;
		try {
			robot = new Robot();
			BufferedImage image = robot.createScreenCapture(screenRectangle);
			return ImageIO.write(image, "png", new File(fileName));
		} catch (AWTException e) {
		} catch (IOException e) {
		return false;


Take Screen Shot Using Java Program