How to Take a Screenshot in Java
The Java "Robot" class includes a screen capturing method that lets you take a screenshot of the desktop. You use the screenshot to create an image, store in a database or email to a user. The image includes the currently active application and the user's background, and the process works on any Windows operating system.
Instructions
-
-
1
Right-click the Java file you want to use to take the screenshot. Click "Open With," and then click the Java editor you want to use.
-
2
Create a variable that points to the Java Robot class. Add the following code to your screenshot function:
Robot screen = new Robot();
-
-
3
Define the rectangle size. You can use the entire screen or define the dimensions. The following code defines a variable that is 500 pixels in width and 500 pixels in height:
Rectangle imageSize = new Rectangle(0, 0, 500, 500);
-
4
Capture the desktop image. The following code takes a screenshot capture of the desktop:
BufferedImage image = screen.createScreenCapture(imageSize);
-
1