Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll take a look at a few different ways of taking a screenshot in Java.

2. Taking a Screenshot Using Robot

In our first example, we’re going to take a screenshot of the main screen.

For that, we’ll use the createScreenCapture() method from the Robot class. It takes a Rectangle as a parameter that sets the bounds for the screenshot and returns a BufferedImage object. The BufferedImage can be further used to create an image file:

@Test
public void givenMainScreen_whenTakeScreenshot_thenSaveToFile() throws Exception {
    Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
    BufferedImage capture = new Robot().createScreenCapture(screenRect);

    File imageFile = new File("single-screen.bmp");
    ImageIO.write(capture, "bmp", imageFile );
    assertTrue(imageFile .exists());
}

The dimensions of the screen are accessible through the Toolkit class by using its getScreenSize() method. On systems with multiple screens, the primary display is used by default.

After capturing the screen into BufferedImage, we can write it to the file with ImageIO.write(). To do so, we’ll need two additional parameters. The image format and the image file itself. In our example, we’re using the .bmp format, but others like .png, .jpg or .gif are also available.

3. Taking a Screenshot of Multiple Screens

It’s also possible to take a screenshot of multiple displays at once. Just like with the previous example, we can use the createScreenCapture() method from the Robot class. But this time the bounds of the screenshot need to cover all required screens.

In order to get all of the displays, we’ll use the GraphicsEnvironment class and its getScreenDevices() method.

Next, we’re going to fetch the bound of each individual screen and create a Rectangle that will fit all of them:

@Test
public void givenMultipleScreens_whenTakeScreenshot_thenSaveToFile() throws Exception {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] screens = ge.getScreenDevices();

    Rectangle allScreenBounds = new Rectangle();
    for (GraphicsDevice screen : screens) {
        Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
        allScreenBounds.width += screenBounds.width;
        allScreenBounds.height = Math.max(allScreenBounds.height, screenBounds.height);
    }

    BufferedImage capture = new Robot().createScreenCapture(allScreenBounds);
    File imageFile = new File("all-screens.bmp");
    ImageIO.write(capture, "bmp", imageFile);
    assertTrue(imageFile.exists());
}

While iterating over the displays, we always sum up the widths and choose just one maximum height as the screens will be concatenated horizontally.

Going further we need to save the screenshot image. As in the previous example, we can use the ImageIO.write() method.

4. Taking a Screenshot of a Given GUI Component

We can also take a screenshot of a given UI component.

The dimensions can be easily accessed via the getBounds() method as every component is aware of its size and location.

In this case, we’re not going to use the Robot API. Instead, we’re going to use the paint() method from the Component class that will draw the content directly into the BufferedImage:

@Test
public void givenComponent_whenTakeScreenshot_thenSaveToFile(Component component) throws Exception {
    Rectangle componentRect = component.getBounds();
    BufferedImage bufferedImage = new BufferedImage(componentRect.width, componentRect.height, BufferedImage.TYPE_INT_ARGB);
    component.paint(bufferedImage.getGraphics());

    File imageFile = new File("component-screenshot.bmp");
    ImageIO.write(bufferedImage, "bmp", imageFile );
    assertTrue(imageFile.exists());
}

After getting the component’s bound we need to create the BufferedImage. For this, we need the width, height, and image type. In this case, we’re using BufferedImage.TYPE_INT_ARGB which refers to an 8-bit color image.

We then move forward to invoke the paint() method to fill the BufferedImage and same as in previous examples we’re saving it to a file with ImageIO.write() method.

5. Conclusion

In this tutorial, we’ve learned several ways how to take screenshots using Java.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW