Skip to content

Commit 2bea888

Browse files
authored
Merge pull request eugenp#753 from sameira/master
BAEL-387 Print Screen In Java
2 parents 1dac21c + 3e31921 commit 2bea888

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎printscreen/pom.xml‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>corejava-printscreen</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>How to Print Screen in Java</name>
11+
<url>https://github.com/eugenp/tutorials</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<junit.version>4.12</junit.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>${junit.version}</version>
23+
<scope>test</scope>
24+
</dependency>
25+
</dependencies>
26+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.baeldung.corejava;;
2+
3+
import javax.imageio.ImageIO;
4+
import java.awt.*;
5+
import java.awt.image.BufferedImage;
6+
import java.io.File;
7+
8+
public class Screenshot {
9+
10+
private String filePath;
11+
private String filenamePrefix;
12+
private String fileType;
13+
private int timeToWait;
14+
15+
public Screenshot(String filePath, String filenamePrefix,
16+
String fileType, int timeToWait) {
17+
this.filePath = filePath;
18+
this.filenamePrefix = filenamePrefix;
19+
this.fileType = fileType;
20+
this.timeToWait = timeToWait;
21+
}
22+
23+
public void getScreenshot() throws Exception {
24+
Thread.sleep(timeToWait);
25+
Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
26+
Robot robot = new Robot();
27+
BufferedImage img = robot.createScreenCapture(rectangle);
28+
ImageIO.write(img, fileType, setupFileNamePath());
29+
}
30+
31+
private File setupFileNamePath() {
32+
return new File(filePath + filenamePrefix + "." + fileType);
33+
}
34+
35+
private Rectangle getScreenSizedRectangle(final Dimension d) {
36+
return new Rectangle(0, 0, d.width, d.height);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.baeldung.corejava;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
9+
import static org.junit.Assert.*;
10+
11+
12+
public class ScreenshotTest {
13+
14+
private Screenshot screenshot;
15+
private String filePath;
16+
private String fileName;
17+
private String fileType;
18+
private File file;
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
filePath = "";
23+
fileName = "Screenshot";
24+
fileType = "jpg";
25+
file = new File(filePath + fileName + "." + fileType);
26+
screenshot = new Screenshot(filePath, fileName, fileType, 2000);
27+
}
28+
29+
@Test
30+
public void testGetScreenshot() throws Exception {
31+
screenshot.getScreenshot();
32+
assertTrue(file.exists());
33+
}
34+
35+
@After
36+
public void tearDown() throws Exception {
37+
file.delete();
38+
}
39+
}

0 commit comments

Comments
 (0)