From 2127c7935452ca7556b65f913c598bcf36513f24 Mon Sep 17 00:00:00 2001 From: Krzysztof Majewski Date: Thu, 23 Jul 2020 19:00:08 +0200 Subject: [PATCH] BAEL-4289 --- .../baeldung/screenshot/ScreenshotTest.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java diff --git a/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java new file mode 100644 index 0000000000..3df31897e1 --- /dev/null +++ b/core-java-modules/core-java-os/src/test/java/com/baeldung/screenshot/ScreenshotTest.java @@ -0,0 +1,54 @@ +import javax.imageio.ImageIO; +import java.awt.Component; +import java.awt.GraphicsDevice; +import java.awt.GraphicsEnvironment; +import java.awt.Rectangle; +import java.awt.Robot; +import java.awt.Toolkit; +import java.awt.image.BufferedImage; +import java.io.File; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class ScreenshotTest { + + @Test + public void takeScreenshotOfMainScreen() 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()); + imageFile.delete(); + } + + @Test + public void takeScreenshotOfAllScreens() 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()); + imageFile.delete(); + } + + @Test + public void makeScreenshot(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()); + imageFile.delete(); + } + +} \ No newline at end of file