Computersight > Programming > Java

Taking Screenshots with Java

Tell your screen to say cheese.

Taking a screenshot with Java is incredibly easy and can be done with about four lines of code:

try { Robot screenshotRobot = new Robot(); BufferedImage screenshot = createScreenCapture(new Rectangle(x, y, width, height));} catch (Exception e) {}

Obviously in this case, "x" and "y" refer to the coordinates on the screen where the picture starts, and "width" and "height" refer to the width and height of the image to be taken. "screenShot" is where the image is stored and you can save or manipulate it from there. Both the Robot declaration and the screen capture have to be written inside of a "try" statement because either could fail and some operating systems don't allow them. That's pretty much all you need to know to take a picture of the screen with Java.

ScreenCapture Program

Using the above code, you could build a program to take a picture of the screen. Below, I've written a very simple sample program in Java that demonstrates how you could incorporate taking a screenshot into a program. This program takes a picture of the whole screen and the saves the image to a file called "Screenshot.png". Here's the code:

import java.awt.*;import java.awt.image.*;import java.awt.event.*;import javax.swing.*;import javax.imageio.*;import java.io.*; class ScreenCapture extends JFrame{ BufferedImage screenImage;
Dimension screenSize;
File savedImageFile;
Robot screenshotRobot;
JLabel statusLabel; public static void main(String args[]){
new ScreenCapture();
} ScreenCapture(){
setTitle("Java Screen Capturer");
setSize(300, 80);
savedImageFile = new File("Screenshot.png");
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
try{
screenshotRobot = new Robot();
screenImage = screenshotRobot.createScreenCapture(new Rectangle(0, 0, screenSize.width, screenSize.height));
ImageIO.write(screenImage, "PNG", savedImageFile);
statusLabel=new JLabel("Screenshot was successfully taken and saved.");
}
catch(Exception e){
statusLabel=new JLabel("Error: " + e);
}
add(statusLabel);
setVisible(true); this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
}
}

Understanding the Code

The above code may seem complex and somewhat scary, but the ideas are really quite simple and it doesn't take much to understand them. For those of you confused or interested in what a certain portion of code does, let's break it down.

import java.awt.*;import java.awt.image.*;import java.awt.event.*;import javax.swing.*;import javax.imageio.*;import java.io.*;

These are simply all the packages needed to run the program. "Java.awt" is the user interface toolkit. "Java.awt.image" let's us deal with images. "Java.awt.event" allows us to detect when the close button of the program or alt+f4 is pressed. "Javax.swing" allows us to use JFrames and Jlabels. "Javax.imageio" lets us write image files. And finally, "Java.io" allows us to deal with files.

class ScreenCapture extends JFrame{ ...}

This is the main program. All the code for the program goes in here.

BufferedImage screenImage;Dimension screenSize;File savedImageFile;Robot screenshotRobot;JLabel statusLabel;

Here's where we define the things we'll be using in our program. "screenImage" refers to the image we take of the screen. "screenSize" is where we store the dimensions of the screen. "savedImageFile" is the file in which the screenshot is saved. "screenshotRobot" is the Robot we use to take the screenshot. And lastly, "statusLabel" is what we use to display whether or not our program ran successfully.


public static void main(String args[]){
new ScreenCapture();
} This piece of code is our main method and it build the window. setTitle("Java Screen Capturer");
setSize(300, 80);
savedImageFile = new File("Screenshot.png");
screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Here we set the text in the title bar of the program to "Java Screen Capture" and set the size of the window to 300 by 80. After that we give the name of the file where the screenshot is to be saved. And finally, we get the dimensions of the screen.

try{ screenshotRobot = new Robot(); screenImage = screenshotRobot.createScreenCapture(new Rectangle(0, 0, screenSize.width, screenSize.height)); ImageIO.write(screenImage, "PNG", savedImageFile); statusLabel=new JLabel("Screenshot was successfully taken and saved.");}

Next, we attempt to create a Robot and then take a picture of the screen starting at the coordinates 0, 0 and extending across the entire screen. The third line of code in this section saves the image to a "PNG" file. And, if everything went okay, the text in the "statusLabel" is set to "Screenshot was successfully taken and saved."

catch(Exception e){ statusLabel=new JLabel("Error: " + e);}

If something went wrong in our program, the text in the "statusLabel" is set to "Error: " and it displays the problem.

add(statusLabel);
setVisible(true); Here, we add a label to the program which displays whether the program was able to run properly or not. Then using "setVisible(true)" we make our program show up on the screen. this.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } });

This last section of code closes the program when either the close button of the program is pressed or the user closes the program some other way (i.e. alt+f4).

Other Uses for Taking a Screenshot

Taking a screenshot can be very useful for many things. For example, if you're making a drawing program, you could allow the user to take a picture of the screen and manipulate the image. You could also make your program take a picture of the screen when an error occurs so you have a better idea of how to fix the problem. I'm even currently developing a program that can beat certain computer games by itself by constantly taking pictures of the screen and scanning the colors. Whatever the use, taking a picture of the screen is incredibly simple and it's a great way to start programming in Java.

Hopefully you were able to get something out of this article and I apologize for it being so long. If there's anything I forgot to add, feel free to leave a message. Have fun coding in Java!

0
Liked It
I Like It!
Related Articles
How to Make a Screenshot Quickly and Easily  |  How To: Taking a Screenshot on an Ipod Touch / Iphone
More Articles by Frozen Boy
Hiding the Screen Flicker in Java  |  Responding to Mouse Events with Java
Latest Articles in Java
How to Make an Analog Clock for Your Website  |  Collection Framework: List Interface
Comments (0)
Post Your Comment:
Name:  
Copy the code into this box:  
Post comment with your Triond credentials?
Inside Computersight

Communication & Networks

 /

Computers

 /

Hardware

 /

Operating Systems

 /

Programming

 /

Software


Popular Tags
Popular Writers
Powered by
Computersight
About Us
Terms of Use
Privacy Policy
Services
Submit an Article
Advertise with Us
Contact

© 2007 Copyright Stanza Ltd. All Rights Reserved.