author | serb |
Tue, 21 Aug 2018 13:57:18 -0700 | |
changeset 53320 | 42037e059f2c |
parent 52248 | 2e330da7cbf4 |
child 53677 | 01980f91eac1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
50482
18f8e3b6f3b7
8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI
serb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.awt; |
|
27 |
||
887 | 28 |
import java.awt.event.InputEvent; |
29 |
import java.awt.event.KeyEvent; |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
30 |
import java.awt.geom.AffineTransform; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
31 |
import java.awt.image.BaseMultiResolutionImage; |
887 | 32 |
import java.awt.image.BufferedImage; |
33 |
import java.awt.image.DataBufferInt; |
|
34 |
import java.awt.image.DirectColorModel; |
|
50482
18f8e3b6f3b7
8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI
serb
parents:
47216
diff
changeset
|
35 |
import java.awt.image.MultiResolutionImage; |
887 | 36 |
import java.awt.image.Raster; |
37 |
import java.awt.image.WritableRaster; |
|
38 |
import java.awt.peer.RobotPeer; |
|
27506
41ccac32e143
8056911: Remove internal API usage from ExtendedRobot class
dermashov
parents:
25859
diff
changeset
|
39 |
|
22060
cd4f9d7dbeda
8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents:
5506
diff
changeset
|
40 |
import sun.awt.AWTPermissions; |
2 | 41 |
import sun.awt.ComponentFactory; |
42 |
import sun.awt.SunToolkit; |
|
887 | 43 |
import sun.awt.image.SunWritableRaster; |
50482
18f8e3b6f3b7
8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI
serb
parents:
47216
diff
changeset
|
44 |
import sun.java2d.SunGraphicsEnvironment; |
2 | 45 |
|
46 |
/** |
|
47 |
* This class is used to generate native system input events |
|
48 |
* for the purposes of test automation, self-running demos, and |
|
49 |
* other applications where control of the mouse and keyboard |
|
50 |
* is needed. The primary purpose of Robot is to facilitate |
|
51 |
* automated testing of Java platform implementations. |
|
52 |
* <p> |
|
53 |
* Using the class to generate input events differs from posting |
|
54 |
* events to the AWT event queue or AWT components in that the |
|
55 |
* events are generated in the platform's native input |
|
35667 | 56 |
* queue. For example, {@code Robot.mouseMove} will actually move |
2 | 57 |
* the mouse cursor instead of just generating mouse move events. |
58 |
* <p> |
|
59 |
* Note that some platforms require special privileges or extensions |
|
60 |
* to access low-level input control. If the current platform configuration |
|
35667 | 61 |
* does not allow input control, an {@code AWTException} will be thrown |
2 | 62 |
* when trying to construct Robot objects. For example, X-Window systems |
63 |
* will throw the exception if the XTEST 2.2 standard extension is not supported |
|
64 |
* (or not enabled) by the X server. |
|
65 |
* <p> |
|
66 |
* Applications that use Robot for purposes other than self-testing should |
|
67 |
* handle these error conditions gracefully. |
|
68 |
* |
|
69 |
* @author Robi Khan |
|
70 |
* @since 1.3 |
|
71 |
*/ |
|
72 |
public class Robot { |
|
73 |
private static final int MAX_DELAY = 60000; |
|
74 |
private RobotPeer peer; |
|
75 |
private boolean isAutoWaitForIdle = false; |
|
76 |
private int autoDelay = 0; |
|
3232
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
77 |
private static int LEGAL_BUTTON_MASK = 0; |
2 | 78 |
|
79 |
private DirectColorModel screenCapCM = null; |
|
80 |
||
81 |
/** |
|
82 |
* Constructs a Robot object in the coordinate system of the primary screen. |
|
83 |
* |
|
84 |
* @throws AWTException if the platform configuration does not allow |
|
85 |
* low-level input control. This exception is always thrown when |
|
86 |
* GraphicsEnvironment.isHeadless() returns true |
|
35667 | 87 |
* @throws SecurityException if {@code createRobot} permission is not granted |
2 | 88 |
* @see java.awt.GraphicsEnvironment#isHeadless |
89 |
* @see SecurityManager#checkPermission |
|
90 |
* @see AWTPermission |
|
91 |
*/ |
|
92 |
public Robot() throws AWTException { |
|
93 |
if (GraphicsEnvironment.isHeadless()) { |
|
94 |
throw new AWTException("headless environment"); |
|
95 |
} |
|
96 |
init(GraphicsEnvironment.getLocalGraphicsEnvironment() |
|
97 |
.getDefaultScreenDevice()); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Creates a Robot for the given screen device. Coordinates passed |
|
44145
a6c61a817414
8033128: Javadoc change is required for java.awt.Robot(GraphicsDevice screen) constructor
serb
parents:
44134
diff
changeset
|
102 |
* to Robot method calls like mouseMove, getPixelColor and |
a6c61a817414
8033128: Javadoc change is required for java.awt.Robot(GraphicsDevice screen) constructor
serb
parents:
44134
diff
changeset
|
103 |
* createScreenCapture will be interpreted as being in the same coordinate |
a6c61a817414
8033128: Javadoc change is required for java.awt.Robot(GraphicsDevice screen) constructor
serb
parents:
44134
diff
changeset
|
104 |
* system as the specified screen. Note that depending on the platform |
a6c61a817414
8033128: Javadoc change is required for java.awt.Robot(GraphicsDevice screen) constructor
serb
parents:
44134
diff
changeset
|
105 |
* configuration, multiple screens may either: |
2 | 106 |
* <ul> |
107 |
* <li>share the same coordinate system to form a combined virtual screen</li> |
|
108 |
* <li>use different coordinate systems to act as independent screens</li> |
|
109 |
* </ul> |
|
110 |
* <p> |
|
111 |
* If screen devices are reconfigured such that the coordinate system is |
|
112 |
* affected, the behavior of existing Robot objects is undefined. |
|
113 |
* |
|
114 |
* @param screen A screen GraphicsDevice indicating the coordinate |
|
115 |
* system the Robot will operate in. |
|
116 |
* @throws AWTException if the platform configuration does not allow |
|
117 |
* low-level input control. This exception is always thrown when |
|
118 |
* GraphicsEnvironment.isHeadless() returns true. |
|
35667 | 119 |
* @throws IllegalArgumentException if {@code screen} is not a screen |
2 | 120 |
* GraphicsDevice. |
35667 | 121 |
* @throws SecurityException if {@code createRobot} permission is not granted |
2 | 122 |
* @see java.awt.GraphicsEnvironment#isHeadless |
123 |
* @see GraphicsDevice |
|
124 |
* @see SecurityManager#checkPermission |
|
125 |
* @see AWTPermission |
|
126 |
*/ |
|
127 |
public Robot(GraphicsDevice screen) throws AWTException { |
|
128 |
checkIsScreenDevice(screen); |
|
129 |
init(screen); |
|
130 |
} |
|
131 |
||
132 |
private void init(GraphicsDevice screen) throws AWTException { |
|
133 |
checkRobotAllowed(); |
|
134 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
|
135 |
if (toolkit instanceof ComponentFactory) { |
|
136 |
peer = ((ComponentFactory)toolkit).createRobot(this, screen); |
|
137 |
disposer = new RobotDisposer(peer); |
|
138 |
sun.java2d.Disposer.addRecord(anchor, disposer); |
|
139 |
} |
|
3232
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
140 |
initLegalButtonMask(); |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
141 |
} |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
142 |
|
43722
25ba19c20260
8143077: Deprecate InputEvent._MASK in favor of InputEvent._DOWN_MASK
serb
parents:
35667
diff
changeset
|
143 |
@SuppressWarnings("deprecation") |
3232
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
144 |
private static synchronized void initLegalButtonMask() { |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
145 |
if (LEGAL_BUTTON_MASK != 0) return; |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
146 |
|
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
147 |
int tmpMask = 0; |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
148 |
if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled()){ |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
149 |
if (Toolkit.getDefaultToolkit() instanceof SunToolkit) { |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
150 |
final int buttonsNumber = ((SunToolkit)(Toolkit.getDefaultToolkit())).getNumberOfButtons(); |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
151 |
for (int i = 0; i < buttonsNumber; i++){ |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
152 |
tmpMask |= InputEvent.getMaskForButton(i+1); |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
153 |
} |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
154 |
} |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
155 |
} |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
156 |
tmpMask |= InputEvent.BUTTON1_MASK| |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
157 |
InputEvent.BUTTON2_MASK| |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
158 |
InputEvent.BUTTON3_MASK| |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
159 |
InputEvent.BUTTON1_DOWN_MASK| |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
160 |
InputEvent.BUTTON2_DOWN_MASK| |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
161 |
InputEvent.BUTTON3_DOWN_MASK; |
aacdc74b37d2
6855323: Robot(GraphicsDevice) constructor initializes LEGAL_BUTTON_MASK variable improperly
dcherepanov
parents:
2810
diff
changeset
|
162 |
LEGAL_BUTTON_MASK = tmpMask; |
2 | 163 |
} |
164 |
||
165 |
/* determine if the security policy allows Robot's to be created */ |
|
166 |
private void checkRobotAllowed() { |
|
167 |
SecurityManager security = System.getSecurityManager(); |
|
168 |
if (security != null) { |
|
22060
cd4f9d7dbeda
8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents:
5506
diff
changeset
|
169 |
security.checkPermission(AWTPermissions.CREATE_ROBOT_PERMISSION); |
2 | 170 |
} |
171 |
} |
|
172 |
||
173 |
/* check if the given device is a screen device */ |
|
174 |
private void checkIsScreenDevice(GraphicsDevice device) { |
|
175 |
if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) { |
|
176 |
throw new IllegalArgumentException("not a valid screen device"); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
private transient Object anchor = new Object(); |
|
181 |
||
182 |
static class RobotDisposer implements sun.java2d.DisposerRecord { |
|
183 |
private final RobotPeer peer; |
|
184 |
public RobotDisposer(RobotPeer peer) { |
|
185 |
this.peer = peer; |
|
186 |
} |
|
187 |
public void dispose() { |
|
188 |
if (peer != null) { |
|
189 |
peer.dispose(); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
private transient RobotDisposer disposer; |
|
195 |
||
196 |
/** |
|
197 |
* Moves mouse pointer to given screen coordinates. |
|
198 |
* @param x X position |
|
199 |
* @param y Y position |
|
200 |
*/ |
|
201 |
public synchronized void mouseMove(int x, int y) { |
|
23274
3b6993da89df
8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents:
5506
diff
changeset
|
202 |
peer.mouseMove(x, y); |
2 | 203 |
afterEvent(); |
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Presses one or more mouse buttons. The mouse buttons should |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
208 |
* be released using the {@link #mouseRelease(int)} method. |
2 | 209 |
* |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
210 |
* @param buttons the Button mask; a combination of one or more |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
211 |
* mouse button masks. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
212 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
213 |
* It is allowed to use only a combination of valid values as a {@code buttons} parameter. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
214 |
* A valid combination consists of {@code InputEvent.BUTTON1_DOWN_MASK}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
215 |
* {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
216 |
* and values returned by the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
217 |
* {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} method. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
218 |
* |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
219 |
* The valid combination also depends on a |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
220 |
* {@link Toolkit#areExtraMouseButtonsEnabled() Toolkit.areExtraMouseButtonsEnabled()} value as follows: |
2 | 221 |
* <ul> |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
222 |
* <li> If support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
223 |
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
224 |
* then it is allowed to use only the following standard button masks: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
225 |
* {@code InputEvent.BUTTON1_DOWN_MASK}, {@code InputEvent.BUTTON2_DOWN_MASK}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
226 |
* {@code InputEvent.BUTTON3_DOWN_MASK}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
227 |
* <li> If support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
228 |
* {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
229 |
* then it is allowed to use the standard button masks |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
230 |
* and masks for existing extended mouse buttons, if the mouse has more then three buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
231 |
* In that way, it is allowed to use the button masks corresponding to the buttons |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
232 |
* in the range from 1 to {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
233 |
* <br> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
234 |
* It is recommended to use the {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
235 |
* method to obtain the mask for any mouse button by its number. |
2 | 236 |
* </ul> |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
237 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
238 |
* The following standard button masks are also accepted: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
239 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
240 |
* <li>{@code InputEvent.BUTTON1_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
241 |
* <li>{@code InputEvent.BUTTON2_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
242 |
* <li>{@code InputEvent.BUTTON3_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
243 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
244 |
* However, it is recommended to use {@code InputEvent.BUTTON1_DOWN_MASK}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
245 |
* {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} instead. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
246 |
* Either extended {@code _DOWN_MASK} or old {@code _MASK} values |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
247 |
* should be used, but both those models should not be mixed. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
248 |
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
249 |
* and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
250 |
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
251 |
* that does not exist on the mouse and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java |
2 | 252 |
* @see #mouseRelease(int) |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
253 |
* @see InputEvent#getMaskForButton(int) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
254 |
* @see Toolkit#areExtraMouseButtonsEnabled() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
255 |
* @see java.awt.MouseInfo#getNumberOfButtons() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
256 |
* @see java.awt.event.MouseEvent |
2 | 257 |
*/ |
258 |
public synchronized void mousePress(int buttons) { |
|
259 |
checkButtonsArgument(buttons); |
|
260 |
peer.mousePress(buttons); |
|
261 |
afterEvent(); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Releases one or more mouse buttons. |
|
266 |
* |
|
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
267 |
* @param buttons the Button mask; a combination of one or more |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
268 |
* mouse button masks. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
269 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
270 |
* It is allowed to use only a combination of valid values as a {@code buttons} parameter. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
271 |
* A valid combination consists of {@code InputEvent.BUTTON1_DOWN_MASK}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
272 |
* {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
273 |
* and values returned by the |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
274 |
* {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} method. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
275 |
* |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
276 |
* The valid combination also depends on a |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
277 |
* {@link Toolkit#areExtraMouseButtonsEnabled() Toolkit.areExtraMouseButtonsEnabled()} value as follows: |
2 | 278 |
* <ul> |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
279 |
* <li> If the support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
280 |
* {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
281 |
* then it is allowed to use only the following standard button masks: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
282 |
* {@code InputEvent.BUTTON1_DOWN_MASK}, {@code InputEvent.BUTTON2_DOWN_MASK}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
283 |
* {@code InputEvent.BUTTON3_DOWN_MASK}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
284 |
* <li> If the support for extended mouse buttons is |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
285 |
* {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
286 |
* then it is allowed to use the standard button masks |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
287 |
* and masks for existing extended mouse buttons, if the mouse has more then three buttons. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
288 |
* In that way, it is allowed to use the button masks corresponding to the buttons |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
289 |
* in the range from 1 to {@link java.awt.MouseInfo#getNumberOfButtons() MouseInfo.getNumberOfButtons()}. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
290 |
* <br> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
291 |
* It is recommended to use the {@link InputEvent#getMaskForButton(int) InputEvent.getMaskForButton(button)} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
292 |
* method to obtain the mask for any mouse button by its number. |
2 | 293 |
* </ul> |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
294 |
* <p> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
295 |
* The following standard button masks are also accepted: |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
296 |
* <ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
297 |
* <li>{@code InputEvent.BUTTON1_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
298 |
* <li>{@code InputEvent.BUTTON2_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
299 |
* <li>{@code InputEvent.BUTTON3_MASK} |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
300 |
* </ul> |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
301 |
* However, it is recommended to use {@code InputEvent.BUTTON1_DOWN_MASK}, |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
302 |
* {@code InputEvent.BUTTON2_DOWN_MASK}, {@code InputEvent.BUTTON3_DOWN_MASK} instead. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
303 |
* Either extended {@code _DOWN_MASK} or old {@code _MASK} values |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
304 |
* should be used, but both those models should not be mixed. |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
305 |
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
306 |
* and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() disabled} by Java |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
307 |
* @throws IllegalArgumentException if the {@code buttons} mask contains the mask for extra mouse button |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
308 |
* that does not exist on the mouse and support for extended mouse buttons is {@link Toolkit#areExtraMouseButtonsEnabled() enabled} by Java |
2 | 309 |
* @see #mousePress(int) |
1962
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
310 |
* @see InputEvent#getMaskForButton(int) |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
311 |
* @see Toolkit#areExtraMouseButtonsEnabled() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
312 |
* @see java.awt.MouseInfo#getNumberOfButtons() |
6c293d33645b
6315717: Support for mouse with multiple scroll wheels and 4 or more buttons
dav
parents:
888
diff
changeset
|
313 |
* @see java.awt.event.MouseEvent |
2 | 314 |
*/ |
315 |
public synchronized void mouseRelease(int buttons) { |
|
316 |
checkButtonsArgument(buttons); |
|
317 |
peer.mouseRelease(buttons); |
|
318 |
afterEvent(); |
|
319 |
} |
|
320 |
||
321 |
private void checkButtonsArgument(int buttons) { |
|
322 |
if ( (buttons|LEGAL_BUTTON_MASK) != LEGAL_BUTTON_MASK ) { |
|
323 |
throw new IllegalArgumentException("Invalid combination of button flags"); |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
/** |
|
328 |
* Rotates the scroll wheel on wheel-equipped mice. |
|
329 |
* |
|
330 |
* @param wheelAmt number of "notches" to move the mouse wheel |
|
331 |
* Negative values indicate movement up/away from the user, |
|
332 |
* positive values indicate movement down/towards the user. |
|
333 |
* |
|
334 |
* @since 1.4 |
|
335 |
*/ |
|
336 |
public synchronized void mouseWheel(int wheelAmt) { |
|
337 |
peer.mouseWheel(wheelAmt); |
|
338 |
afterEvent(); |
|
339 |
} |
|
340 |
||
341 |
/** |
|
342 |
* Presses a given key. The key should be released using the |
|
35667 | 343 |
* {@code keyRelease} method. |
2 | 344 |
* <p> |
345 |
* Key codes that have more than one physical key associated with them |
|
35667 | 346 |
* (e.g. {@code KeyEvent.VK_SHIFT} could mean either the |
2 | 347 |
* left or right shift key) will map to the left key. |
348 |
* |
|
35667 | 349 |
* @param keycode Key to press (e.g. {@code KeyEvent.VK_A}) |
350 |
* @throws IllegalArgumentException if {@code keycode} is not |
|
2 | 351 |
* a valid key |
352 |
* @see #keyRelease(int) |
|
353 |
* @see java.awt.event.KeyEvent |
|
354 |
*/ |
|
355 |
public synchronized void keyPress(int keycode) { |
|
356 |
checkKeycodeArgument(keycode); |
|
357 |
peer.keyPress(keycode); |
|
358 |
afterEvent(); |
|
359 |
} |
|
360 |
||
361 |
/** |
|
362 |
* Releases a given key. |
|
363 |
* <p> |
|
364 |
* Key codes that have more than one physical key associated with them |
|
35667 | 365 |
* (e.g. {@code KeyEvent.VK_SHIFT} could mean either the |
2 | 366 |
* left or right shift key) will map to the left key. |
367 |
* |
|
35667 | 368 |
* @param keycode Key to release (e.g. {@code KeyEvent.VK_A}) |
369 |
* @throws IllegalArgumentException if {@code keycode} is not a |
|
2 | 370 |
* valid key |
371 |
* @see #keyPress(int) |
|
372 |
* @see java.awt.event.KeyEvent |
|
373 |
*/ |
|
374 |
public synchronized void keyRelease(int keycode) { |
|
375 |
checkKeycodeArgument(keycode); |
|
376 |
peer.keyRelease(keycode); |
|
377 |
afterEvent(); |
|
378 |
} |
|
379 |
||
380 |
private void checkKeycodeArgument(int keycode) { |
|
381 |
// rather than build a big table or switch statement here, we'll |
|
382 |
// just check that the key isn't VK_UNDEFINED and assume that the |
|
383 |
// peer implementations will throw an exception for other bogus |
|
384 |
// values e.g. -1, 999999 |
|
385 |
if (keycode == KeyEvent.VK_UNDEFINED) { |
|
386 |
throw new IllegalArgumentException("Invalid key code"); |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* Returns the color of a pixel at the given screen coordinates. |
|
392 |
* @param x X position of pixel |
|
393 |
* @param y Y position of pixel |
|
394 |
* @return Color of the pixel |
|
395 |
*/ |
|
396 |
public synchronized Color getPixelColor(int x, int y) { |
|
53320 | 397 |
checkScreenCaptureAllowed(); |
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
398 |
AffineTransform tx = GraphicsEnvironment. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
399 |
getLocalGraphicsEnvironment().getDefaultScreenDevice(). |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
400 |
getDefaultConfiguration().getDefaultTransform(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
401 |
x = (int) (x * tx.getScaleX()); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
402 |
y = (int) (y * tx.getScaleY()); |
23274
3b6993da89df
8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents:
5506
diff
changeset
|
403 |
Color color = new Color(peer.getRGBPixel(x, y)); |
2 | 404 |
return color; |
405 |
} |
|
406 |
||
407 |
/** |
|
408 |
* Creates an image containing pixels read from the screen. This image does |
|
409 |
* not include the mouse cursor. |
|
410 |
* @param screenRect Rect to capture in screen coordinates |
|
411 |
* @return The captured image |
|
35667 | 412 |
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero |
413 |
* @throws SecurityException if {@code readDisplayPixels} permission is not granted |
|
2 | 414 |
* @see SecurityManager#checkPermission |
415 |
* @see AWTPermission |
|
416 |
*/ |
|
417 |
public synchronized BufferedImage createScreenCapture(Rectangle screenRect) { |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
418 |
return createCompatibleImage(screenRect, false)[0]; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
419 |
} |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
420 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
421 |
/** |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
422 |
* Creates an image containing pixels read from the screen. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
423 |
* This image does not include the mouse cursor. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
424 |
* This method can be used in case there is a scaling transform |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
425 |
* from user space to screen (device) space. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
426 |
* Typically this means that the display is a high resolution screen, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
427 |
* although strictly it means any case in which there is such a transform. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
428 |
* Returns a {@link java.awt.image.MultiResolutionImage}. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
429 |
* <p> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
430 |
* For a non-scaled display, the {@code MultiResolutionImage} |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
431 |
* will have one image variant: |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
432 |
* <ul> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
433 |
* <li> Base Image with user specified size. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
434 |
* </ul> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
435 |
* <p> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
436 |
* For a high resolution display where there is a scaling transform, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
437 |
* the {@code MultiResolutionImage} will have two image variants: |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
438 |
* <ul> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
439 |
* <li> Base Image with user specified size. This is scaled from the screen. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
440 |
* <li> Native device resolution image with device size pixels. |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
441 |
* </ul> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
442 |
* <p> |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
443 |
* Example: |
44134
0821d790675b
8175066: Compilation error due to tag in JDK-8162959
pkbalakr
parents:
44130
diff
changeset
|
444 |
* <pre>{@code |
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
445 |
* Image nativeResImage; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
446 |
* MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(frame.getBounds()); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
447 |
* List<Image> resolutionVariants = mrImage.getResolutionVariants(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
448 |
* if (resolutionVariants.size() > 1) { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
449 |
* nativeResImage = resolutionVariants.get(1); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
450 |
* } else { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
451 |
* nativeResImage = resolutionVariants.get(0); |
44134
0821d790675b
8175066: Compilation error due to tag in JDK-8162959
pkbalakr
parents:
44130
diff
changeset
|
452 |
* } |
0821d790675b
8175066: Compilation error due to tag in JDK-8162959
pkbalakr
parents:
44130
diff
changeset
|
453 |
* }</pre> |
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
454 |
* @param screenRect Rect to capture in screen coordinates |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
455 |
* @return The captured image |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
456 |
* @throws IllegalArgumentException if {@code screenRect} width and height are not greater than zero |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
457 |
* @throws SecurityException if {@code readDisplayPixels} permission is not granted |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
458 |
* @see SecurityManager#checkPermission |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
459 |
* @see AWTPermission |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
460 |
* |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
461 |
* @since 9 |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
462 |
*/ |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
463 |
public synchronized MultiResolutionImage |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
464 |
createMultiResolutionScreenCapture(Rectangle screenRect) { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
465 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
466 |
return new BaseMultiResolutionImage( |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
467 |
createCompatibleImage(screenRect, true)); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
468 |
} |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
469 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
470 |
private synchronized BufferedImage[] |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
471 |
createCompatibleImage(Rectangle screenRect, boolean isHiDPI) { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
472 |
|
2 | 473 |
checkScreenCaptureAllowed(); |
474 |
||
23274
3b6993da89df
8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents:
5506
diff
changeset
|
475 |
checkValidRect(screenRect); |
2 | 476 |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
477 |
BufferedImage lowResolutionImage; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
478 |
BufferedImage highResolutionImage; |
2 | 479 |
DataBufferInt buffer; |
480 |
WritableRaster raster; |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
481 |
BufferedImage[] imageArray; |
2 | 482 |
|
483 |
if (screenCapCM == null) { |
|
484 |
/* |
|
485 |
* Fix for 4285201 |
|
486 |
* Create a DirectColorModel equivalent to the default RGB ColorModel, |
|
487 |
* except with no Alpha component. |
|
488 |
*/ |
|
489 |
||
490 |
screenCapCM = new DirectColorModel(24, |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
491 |
/* red mask */ 0x00FF0000, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
492 |
/* green mask */ 0x0000FF00, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
493 |
/* blue mask */ 0x000000FF); |
2 | 494 |
} |
495 |
||
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
496 |
int[] bandmasks = new int[3]; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
497 |
bandmasks[0] = screenCapCM.getRedMask(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
498 |
bandmasks[1] = screenCapCM.getGreenMask(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
499 |
bandmasks[2] = screenCapCM.getBlueMask(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
500 |
|
887 | 501 |
// need to sync the toolkit prior to grabbing the pixels since in some |
502 |
// cases rendering to the screen may be delayed |
|
503 |
Toolkit.getDefaultToolkit().sync(); |
|
44152
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
504 |
|
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
505 |
GraphicsConfiguration gc = GraphicsEnvironment |
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
506 |
.getLocalGraphicsEnvironment() |
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
507 |
.getDefaultScreenDevice(). |
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
508 |
getDefaultConfiguration(); |
50482
18f8e3b6f3b7
8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI
serb
parents:
47216
diff
changeset
|
509 |
gc = SunGraphicsEnvironment.getGraphicsConfigurationAtPoint( |
44152
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
510 |
gc, screenRect.getCenterX(), screenRect.getCenterY()); |
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
511 |
|
af74e15ef830
8176097: Window set location to a display with different DPI does not properly work
alexsch
parents:
44145
diff
changeset
|
512 |
AffineTransform tx = gc.getDefaultTransform(); |
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
513 |
double uiScaleX = tx.getScaleX(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
514 |
double uiScaleY = tx.getScaleY(); |
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
50482
diff
changeset
|
515 |
int[] pixels; |
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
516 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
517 |
if (uiScaleX == 1 && uiScaleY == 1) { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
518 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
519 |
pixels = peer.getRGBPixels(screenRect); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
520 |
buffer = new DataBufferInt(pixels, pixels.length); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
521 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
522 |
bandmasks[0] = screenCapCM.getRedMask(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
523 |
bandmasks[1] = screenCapCM.getGreenMask(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
524 |
bandmasks[2] = screenCapCM.getBlueMask(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
525 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
526 |
raster = Raster.createPackedRaster(buffer, screenRect.width, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
527 |
screenRect.height, screenRect.width, bandmasks, null); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
528 |
SunWritableRaster.makeTrackable(buffer); |
2 | 529 |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
530 |
highResolutionImage = new BufferedImage(screenCapCM, raster, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
531 |
false, null); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
532 |
imageArray = new BufferedImage[1]; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
533 |
imageArray[0] = highResolutionImage; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
534 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
535 |
} else { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
536 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
537 |
int sX = (int) Math.floor(screenRect.x * uiScaleX); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
538 |
int sY = (int) Math.floor(screenRect.y * uiScaleY); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
539 |
int sWidth = (int) Math.ceil(screenRect.width * uiScaleX); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
540 |
int sHeight = (int) Math.ceil(screenRect.height * uiScaleY); |
52248
2e330da7cbf4
8211300: Convert C-style array declarations in JDK client code
tvaleev
parents:
50482
diff
changeset
|
541 |
int[] temppixels; |
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
542 |
Rectangle scaledRect = new Rectangle(sX, sY, sWidth, sHeight); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
543 |
temppixels = peer.getRGBPixels(scaledRect); |
2 | 544 |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
545 |
// HighResolutionImage |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
546 |
pixels = temppixels; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
547 |
buffer = new DataBufferInt(pixels, pixels.length); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
548 |
raster = Raster.createPackedRaster(buffer, scaledRect.width, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
549 |
scaledRect.height, scaledRect.width, bandmasks, null); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
550 |
SunWritableRaster.makeTrackable(buffer); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
551 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
552 |
highResolutionImage = new BufferedImage(screenCapCM, raster, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
553 |
false, null); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
554 |
|
2 | 555 |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
556 |
// LowResolutionImage |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
557 |
lowResolutionImage = new BufferedImage(screenRect.width, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
558 |
screenRect.height, highResolutionImage.getType()); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
559 |
Graphics2D g = lowResolutionImage.createGraphics(); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
560 |
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
561 |
RenderingHints.VALUE_INTERPOLATION_BILINEAR); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
562 |
g.setRenderingHint(RenderingHints.KEY_RENDERING, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
563 |
RenderingHints.VALUE_RENDER_QUALITY); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
564 |
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
565 |
RenderingHints.VALUE_ANTIALIAS_ON); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
566 |
g.drawImage(highResolutionImage, 0, 0, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
567 |
screenRect.width, screenRect.height, |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
568 |
0, 0, scaledRect.width, scaledRect.height, null); |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
569 |
g.dispose(); |
2 | 570 |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
571 |
if(!isHiDPI) { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
572 |
imageArray = new BufferedImage[1]; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
573 |
imageArray[0] = lowResolutionImage; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
574 |
} else { |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
575 |
imageArray = new BufferedImage[2]; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
576 |
imageArray[0] = lowResolutionImage; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
577 |
imageArray[1] = highResolutionImage; |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
578 |
} |
2 | 579 |
|
44130
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
580 |
} |
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
581 |
|
f3516efccffa
8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents:
43722
diff
changeset
|
582 |
return imageArray; |
2 | 583 |
} |
584 |
||
585 |
private static void checkValidRect(Rectangle rect) { |
|
586 |
if (rect.width <= 0 || rect.height <= 0) { |
|
587 |
throw new IllegalArgumentException("Rectangle width and height must be > 0"); |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
private static void checkScreenCaptureAllowed() { |
|
592 |
SecurityManager security = System.getSecurityManager(); |
|
593 |
if (security != null) { |
|
22060
cd4f9d7dbeda
8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents:
5506
diff
changeset
|
594 |
security.checkPermission(AWTPermissions.READ_DISPLAY_PIXELS_PERMISSION); |
2 | 595 |
} |
596 |
} |
|
597 |
||
598 |
/* |
|
599 |
* Called after an event is generated |
|
600 |
*/ |
|
601 |
private void afterEvent() { |
|
602 |
autoWaitForIdle(); |
|
603 |
autoDelay(); |
|
604 |
} |
|
605 |
||
606 |
/** |
|
35667 | 607 |
* Returns whether this Robot automatically invokes {@code waitForIdle} |
2 | 608 |
* after generating an event. |
35667 | 609 |
* @return Whether {@code waitForIdle} is automatically called |
2 | 610 |
*/ |
611 |
public synchronized boolean isAutoWaitForIdle() { |
|
612 |
return isAutoWaitForIdle; |
|
613 |
} |
|
614 |
||
615 |
/** |
|
35667 | 616 |
* Sets whether this Robot automatically invokes {@code waitForIdle} |
2 | 617 |
* after generating an event. |
35667 | 618 |
* @param isOn Whether {@code waitForIdle} is automatically invoked |
2 | 619 |
*/ |
620 |
public synchronized void setAutoWaitForIdle(boolean isOn) { |
|
621 |
isAutoWaitForIdle = isOn; |
|
622 |
} |
|
623 |
||
624 |
/* |
|
625 |
* Calls waitForIdle after every event if so desired. |
|
626 |
*/ |
|
627 |
private void autoWaitForIdle() { |
|
628 |
if (isAutoWaitForIdle) { |
|
629 |
waitForIdle(); |
|
630 |
} |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Returns the number of milliseconds this Robot sleeps after generating an event. |
|
25162 | 635 |
* |
636 |
* @return the delay duration in milliseconds |
|
2 | 637 |
*/ |
638 |
public synchronized int getAutoDelay() { |
|
639 |
return autoDelay; |
|
640 |
} |
|
641 |
||
642 |
/** |
|
643 |
* Sets the number of milliseconds this Robot sleeps after generating an event. |
|
25162 | 644 |
* |
645 |
* @param ms the delay duration in milliseconds |
|
646 |
* @throws IllegalArgumentException If {@code ms} |
|
647 |
* is not between 0 and 60,000 milliseconds inclusive |
|
2 | 648 |
*/ |
649 |
public synchronized void setAutoDelay(int ms) { |
|
650 |
checkDelayArgument(ms); |
|
651 |
autoDelay = ms; |
|
652 |
} |
|
653 |
||
654 |
/* |
|
655 |
* Automatically sleeps for the specified interval after event generated. |
|
656 |
*/ |
|
657 |
private void autoDelay() { |
|
658 |
delay(autoDelay); |
|
659 |
} |
|
660 |
||
661 |
/** |
|
662 |
* Sleeps for the specified time. |
|
35667 | 663 |
* To catch any {@code InterruptedException}s that occur, |
664 |
* {@code Thread.sleep()} may be used instead. |
|
25162 | 665 |
* |
666 |
* @param ms time to sleep in milliseconds |
|
667 |
* @throws IllegalArgumentException if {@code ms} |
|
668 |
* is not between 0 and 60,000 milliseconds inclusive |
|
669 |
* @see java.lang.Thread#sleep |
|
2 | 670 |
*/ |
671 |
public synchronized void delay(int ms) { |
|
672 |
checkDelayArgument(ms); |
|
673 |
try { |
|
674 |
Thread.sleep(ms); |
|
675 |
} catch(InterruptedException ite) { |
|
676 |
ite.printStackTrace(); |
|
677 |
} |
|
678 |
} |
|
679 |
||
680 |
private void checkDelayArgument(int ms) { |
|
681 |
if (ms < 0 || ms > MAX_DELAY) { |
|
682 |
throw new IllegalArgumentException("Delay must be to 0 to 60,000ms"); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* Waits until all events currently on the event queue have been processed. |
|
688 |
* @throws IllegalThreadStateException if called on the AWT event dispatching thread |
|
689 |
*/ |
|
690 |
public synchronized void waitForIdle() { |
|
691 |
checkNotDispatchThread(); |
|
32493
31bd2a308840
8076178: [macosx] Few open swing and awt reg-tests fail after their update to avoid SunToolkit.realSync
serb
parents:
27509
diff
changeset
|
692 |
SunToolkit.flushPendingEvents(); |
31bd2a308840
8076178: [macosx] Few open swing and awt reg-tests fail after their update to avoid SunToolkit.realSync
serb
parents:
27509
diff
changeset
|
693 |
((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); |
2 | 694 |
} |
695 |
||
696 |
private void checkNotDispatchThread() { |
|
697 |
if (EventQueue.isDispatchThread()) { |
|
698 |
throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread"); |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
/** |
|
703 |
* Returns a string representation of this Robot. |
|
704 |
* |
|
705 |
* @return the string representation. |
|
706 |
*/ |
|
32493
31bd2a308840
8076178: [macosx] Few open swing and awt reg-tests fail after their update to avoid SunToolkit.realSync
serb
parents:
27509
diff
changeset
|
707 |
@Override |
2 | 708 |
public synchronized String toString() { |
709 |
String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle(); |
|
710 |
return getClass().getName() + "[ " + params + " ]"; |
|
711 |
} |
|
712 |
} |