-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobotTest.java
96 lines (79 loc) · 2.51 KB
/
RobotTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package robot;
import javax.swing.*;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
/**
* @author Cay Horstmann
* @version 1.05 2015-08-20
*/
public class RobotTest {
public static void main(String[] args) {
EventQueue.invokeLater(() ->
{
// make frame with a button panel
ButtonFrame frame = new ButtonFrame();
frame.setTitle("ButtonTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
});
// attach a robot to the screen device
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice screen = environment.getDefaultScreenDevice();
try {
final Robot robot = new Robot(screen);
robot.waitForIdle();
new Thread() {
public void run() {
runTest(robot);
}
;
}.start();
} catch (AWTException e) {
e.printStackTrace();
}
}
/**
* Runs a sample test procedure
*
* @param robot the robot attached to the screen device
*/
public static void runTest(Robot robot) {
// simulate a space bar press
robot.keyPress(' ');
robot.keyRelease(' ');
// simulate a tab key followed by a space
robot.delay(2000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(' ');
robot.keyRelease(' ');
// simulate a mouse click over the rightmost button
robot.delay(2000);
robot.mouseMove(220, 40);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// capture the screen and show the resulting image
robot.delay(2000);
BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 400, 300));
ImageFrame frame = new ImageFrame(image);
frame.setVisible(true);
}
}
/**
* A frame to display a captured image
*/
class ImageFrame extends JFrame {
private static final int DEFAULT_WIDTH = 450;
private static final int DEFAULT_HEIGHT = 350;
/**
* @param image the image to display
*/
public ImageFrame(Image image) {
setTitle("Capture");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
JLabel label = new JLabel(new ImageIcon(image));
add(label);
}
}