Skip to content

Commit 4db81e9

Browse files
committed
Adding Print Screen in Java
1 parent 298c5e3 commit 4db81e9

3 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎printscreen/pom.xml‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>junit</groupId>
26+
<artifactId>junit</artifactId>
27+
<version>4.12</version>
28+
<scope>test</scope>
29+
</dependency>
30+
</dependencies>
31+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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() {
24+
try {
25+
Thread.sleep(timeToWait);
26+
Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
27+
Robot robot = new Robot();
28+
BufferedImage img = robot.createScreenCapture(rectangle);
29+
ImageIO.write(img, fileType, setupFileNamePath());
30+
} catch (Exception ex) {
31+
System.out.println("Error occurred while getting the Print Screen.");
32+
}
33+
}
34+
35+
private File setupFileNamePath() {
36+
return new File(filePath + filenamePrefix + "." + fileType);
37+
}
38+
39+
private Rectangle getScreenSizedRectangle(final Dimension d) {
40+
return new Rectangle(0, 0, d.width, d.height);
41+
}
42+
}
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)