Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to Capture Screen through a Java program using Robot class ?.

Program to demonstrate screen capture through a Java program using Robot class.

package com.hubberspot.example;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class RobotScreenCapture {
 
   public static void main(String[] args) {
  
 try {
   
 // 1. Create a Robot Object 
 Robot robot = new Robot();
        // 2. Create a Dimension Object for the screen capture 
 // by providing width and height for the screen area 
 Dimension dimension = new Dimension(300,300);
 // 3. Create a Rectangle by taking the dimension reference
 // to create a screen capture of area specified by dimension 
 Rectangle screen = new Rectangle(dimension);
 // 4. Calling createScreenCapture method of Robot class by 
 // passing it Rectangle reference. It will return a reference 
 // to a BufferedImage Object.  
 BufferedImage buffer = robot.createScreenCapture(screen);
 // 5. Create a file to store the image 
 File screenCapture = new File("screen.jpg");
 // 6. Call the write method of ImageIO class by passing
 // it BufferedImage and File created above 
 ImageIO.write(buffer, "jpg", screenCapture);
   
 } catch (AWTException e) {   
  e.printStackTrace();
 } catch (IOException e) {  
  e.printStackTrace();
 } 

   }

}




Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot