diff --git a/printscreen/pom.xml b/printscreen/pom.xml new file mode 100644 index 0000000000..ddb813a7a2 --- /dev/null +++ b/printscreen/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + com.baeldung + corejava-printscreen + 1.0-SNAPSHOT + jar + + How to Print Screen in Java + https://github.com/eugenp/tutorials + + + UTF-8 + 4.12 + + + + + junit + junit + ${junit.version} + test + + + diff --git a/printscreen/src/main/java/org/baeldung/corejava/Screenshot.java b/printscreen/src/main/java/org/baeldung/corejava/Screenshot.java new file mode 100644 index 0000000000..d33761932f --- /dev/null +++ b/printscreen/src/main/java/org/baeldung/corejava/Screenshot.java @@ -0,0 +1,38 @@ +package org.baeldung.corejava;; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.File; + +public class Screenshot { + + private String filePath; + private String filenamePrefix; + private String fileType; + private int timeToWait; + + public Screenshot(String filePath, String filenamePrefix, + String fileType, int timeToWait) { + this.filePath = filePath; + this.filenamePrefix = filenamePrefix; + this.fileType = fileType; + this.timeToWait = timeToWait; + } + + public void getScreenshot() throws Exception { + Thread.sleep(timeToWait); + Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); + Robot robot = new Robot(); + BufferedImage img = robot.createScreenCapture(rectangle); + ImageIO.write(img, fileType, setupFileNamePath()); + } + + private File setupFileNamePath() { + return new File(filePath + filenamePrefix + "." + fileType); + } + + private Rectangle getScreenSizedRectangle(final Dimension d) { + return new Rectangle(0, 0, d.width, d.height); + } +} diff --git a/printscreen/src/test/java/org/baeldung/corejava/ScreenshotTest.java b/printscreen/src/test/java/org/baeldung/corejava/ScreenshotTest.java new file mode 100644 index 0000000000..588c2eea78 --- /dev/null +++ b/printscreen/src/test/java/org/baeldung/corejava/ScreenshotTest.java @@ -0,0 +1,39 @@ +package org.baeldung.corejava; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.*; + + +public class ScreenshotTest { + + private Screenshot screenshot; + private String filePath; + private String fileName; + private String fileType; + private File file; + + @Before + public void setUp() throws Exception { + filePath = ""; + fileName = "Screenshot"; + fileType = "jpg"; + file = new File(filePath + fileName + "." + fileType); + screenshot = new Screenshot(filePath, fileName, fileType, 2000); + } + + @Test + public void testGetScreenshot() throws Exception { + screenshot.getScreenshot(); + assertTrue(file.exists()); + } + + @After + public void tearDown() throws Exception { + file.delete(); + } +} \ No newline at end of file