Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

When you need to perform a print screen action on your desktop, there is an built-in ‘PrntScr' button on the keyboard to help you with it. Sometimes that's enough.

But the issue arises when you need to do that action programmatically. Simply put, you may need to save a current screenshot as an image file using Java.

Let's have a look how we can do that.

2. The Robot Class

Java java.awt.Robot class is the main API we're going to be using. This call contains a method called ‘createScreenCapture‘ which takes a screenshot when a specific shape is passed:


As the above method returns a java.awt.image.BufferedImage instance, all you have to do is to write the retrieved image to a file using the javax.imageio.ImageIO utility class.

3. Capturing and Saving the Image File

The Java code for image capturing and saving is as follows:

public void getScreenshot(int timeToWait) throws Exception {
    Rectangle rec = new Rectangle(
    Robot robot = new Robot();
    BufferedImage img = robot.createScreenCapture(rectangle);
    ImageIO.write(img, "jpg", setupFileNamePath());

Here, it is possible to capture a part of the screen by setting the required size to the java.awt.Rectangle instance. However, in the above example, it has been set to capture the full screen, by setting the current screen size.

4. Conclusion

In this tutorial, we had a quick look on the usage of a print screen in Java. The source code of the examples above can be found in the GitHub project.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!