Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite521cb2

Browse files
committed
Add capture screen snippet
1 parentb955dfd commite521cb2

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

‎README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Update the sample application with the snippet and add a test for it. After prov
2020
*[Factorial](#factorial)
2121
*[Fibonacci](#fibonacci)
2222

23+
###Media
24+
*[Capture screen](#capture-screen)
25+
2326
###String
2427
*[Reverse string](#reverse-string)
2528

@@ -96,6 +99,22 @@ Update the sample application with the snippet and add a test for it. After prov
9699

97100
[⬆ back to top](#table-of-contents)
98101

102+
##Media
103+
104+
###Capture screen
105+
106+
```java
107+
publicstaticvoid captureScreen(String filename) throwsAWTException,IOException {
108+
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
109+
Rectangle screenRectangle=newRectangle(screenSize);
110+
Robot robot=newRobot();
111+
BufferedImage image= robot.createScreenCapture(screenRectangle);
112+
ImageIO.write(image,"png",newFile(filename));
113+
}
114+
```
115+
116+
[⬆ back to top](#table-of-contents)
117+
99118
##String
100119

101120
###Reverse string

‎src/main/java/Library.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
importjavax.imageio.ImageIO;
2+
importjava.awt.*;
3+
importjava.awt.image.BufferedImage;
14
importjava.io.File;
25
importjava.io.IOException;
36
importjava.nio.file.Files;
@@ -88,4 +91,18 @@ public static String reverseString(String s) {
8891
publicstaticList<String>readLines(Stringfilename)throwsIOException {
8992
returnFiles.readAllLines(newFile(filename).toPath());
9093
}
94+
95+
/**
96+
* Capture screenshot and save it to a file
97+
* @param filename the name of the file
98+
* @throws AWTException
99+
* @throws IOException
100+
*/
101+
publicstaticvoidcaptureScreen(Stringfilename)throwsAWTException,IOException {
102+
DimensionscreenSize =Toolkit.getDefaultToolkit().getScreenSize();
103+
RectanglescreenRectangle =newRectangle(screenSize);
104+
Robotrobot =newRobot();
105+
BufferedImageimage =robot.createScreenCapture(screenRectangle);
106+
ImageIO.write(image,"png",newFile(filename));
107+
}
91108
}

‎src/test/java/LibraryTest.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
importorg.junit.Test;
22

3+
importjava.awt.*;
4+
importjava.io.File;
35
importjava.io.IOException;
6+
importjava.nio.file.Files;
47
importjava.util.List;
58

69
importstaticorg.junit.Assert.*;
@@ -94,7 +97,7 @@ public void testReverseString() {
9497
*/
9598
@Test
9699
publicvoidtestReadLines()throwsIOException {
97-
List<String>somelines =Library.readLines("src/main/resources/somelines.txt");
100+
List<String>somelines =Library.readLines("src/test/resources/somelines.txt");
98101
assertEquals(3,somelines.size());
99102
assertEquals("foo",somelines.get(0));
100103
assertEquals("bar",somelines.get(1));
@@ -106,4 +109,18 @@ public void testReadLines() throws IOException {
106109
// catched the expected exception
107110
}
108111
}
112+
/**
113+
* Tests for {@link Library#captureScreen(String)}
114+
*/
115+
@Test
116+
publicvoidtestCaptureScreen()throwsIOException,AWTException {
117+
finalStringfilename ="src/test/resources/screenshot.png";
118+
try {
119+
Library.captureScreen(filename);
120+
Filef =newFile(filename);
121+
assertTrue(f.exists() && !f.isDirectory());
122+
}finally {
123+
Files.delete(newFile(filename).toPath());
124+
}
125+
}
109126
}
File renamed without changes.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp