Print screen using java

Is there any api for capure my screen ..?
You don't need any api. Java have the inbuild functionalities for this. Using the Robot class you can print your screen. Here is the code sample and explanation.

Example code:

import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Robot;
import javax.imageio.ImageIO;

class ScreenRecorder {
  public static void main(String args[]) {
    try {
      Toolkit tool = Toolkit.getDefaultToolkit();
      Dimension d = tool.getScreenSize();
      Rectangle rect = new Rectangle(d);
      Robot robot = new Robot();
      File f = new File("screenshot.jpg");
      BufferedImage img = robot.createScreenCapture(rect);
    } catch(Exception e){
  • Import the classes you need.

  • Using the ToolKit you can get your screensize.

  • Form a Rectangle object with this width and height

  • capture the screen for this full width and height.

  • There is a Thread.sleep for 2 sec for my convenience

  • You can specify your own width and height by

    rect = new Rectangle(640,480);

I think this is help you more. Now am working on record the desktop using java. Soon i will come up with good product.

Captured Image:


Show Comments