src/java.desktop/share/classes/java/awt/Robot.java
author serb
Mon, 04 Jun 2018 20:32:19 -0700
changeset 50482 18f8e3b6f3b7
parent 47216 71c04702a3d5
child 52248 2e330da7cbf4
permissions -rw-r--r--
8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI 8190326: Robot.mouseMove uses scaling factor of main display on unscaled second display Reviewed-by: prr, kcr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3956
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    28
import java.awt.event.InputEvent;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    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
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    32
import java.awt.image.BufferedImage;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    33
import java.awt.image.DataBufferInt;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    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
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    36
import java.awt.image.Raster;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    37
import java.awt.image.WritableRaster;
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.awt.ComponentFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.awt.SunToolkit;
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * This class is used to generate native system input events
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * for the purposes of test automation, self-running demos, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * other applications where control of the mouse and keyboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * is needed. The primary purpose of Robot is to facilitate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * automated testing of Java platform implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Using the class to generate input events differs from posting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * events to the AWT event queue or AWT components in that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * events are generated in the platform's native input
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
    56
 * queue. For example, {@code Robot.mouseMove} will actually move
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * the mouse cursor instead of just generating mouse move events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Note that some platforms require special privileges or extensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * to access low-level input control. If the current platform configuration
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
    61
 * does not allow input control, an {@code AWTException} will be thrown
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * when trying to construct Robot objects. For example, X-Window systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * will throw the exception if the XTEST 2.2 standard extension is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * (or not enabled) by the X server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Applications that use Robot for purposes other than self-testing should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * handle these error conditions gracefully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @author      Robi Khan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @since       1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
public class Robot {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final int MAX_DELAY = 60000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private RobotPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private boolean isAutoWaitForIdle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private DirectColorModel screenCapCM = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Constructs a Robot object in the coordinate system of the primary screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @throws  AWTException if the platform configuration does not allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * low-level input control.  This exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * GraphicsEnvironment.isHeadless() returns true
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
    87
     * @throws  SecurityException if {@code createRobot} permission is not granted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see     java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @see     SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @see     AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public Robot() throws AWTException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new AWTException("headless environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        init(GraphicsEnvironment.getLocalGraphicsEnvironment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            .getDefaultScreenDevice());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <li>share the same coordinate system to form a combined virtual screen</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <li>use different coordinate systems to act as independent screens</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * If screen devices are reconfigured such that the coordinate system is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * affected, the behavior of existing Robot objects is undefined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param screen    A screen GraphicsDevice indicating the coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                  system the Robot will operate in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws  AWTException if the platform configuration does not allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * low-level input control.  This exception is always thrown when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * GraphicsEnvironment.isHeadless() returns true.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   119
     * @throws  IllegalArgumentException if {@code screen} is not a screen
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          GraphicsDevice.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   121
     * @throws  SecurityException if {@code createRobot} permission is not granted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @see     java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see     GraphicsDevice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see     SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see     AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public Robot(GraphicsDevice screen) throws AWTException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        checkIsScreenDevice(screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        init(screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void init(GraphicsDevice screen) throws AWTException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        checkRobotAllowed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (toolkit instanceof ComponentFactory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            peer = ((ComponentFactory)toolkit).createRobot(this, screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            disposer = new RobotDisposer(peer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            sun.java2d.Disposer.addRecord(anchor, disposer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /* determine if the security policy allows Robot's to be created */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private void checkRobotAllowed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* check if the given device is a screen device */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private void checkIsScreenDevice(GraphicsDevice device) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new IllegalArgumentException("not a valid screen device");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private transient Object anchor = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    static class RobotDisposer implements sun.java2d.DisposerRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        private final RobotPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        public RobotDisposer(RobotPeer peer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            this.peer = peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (peer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                peer.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private transient RobotDisposer disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Moves mouse pointer to given screen coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param x         X position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param y         Y position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        afterEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public synchronized void mousePress(int buttons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        checkButtonsArgument(buttons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        peer.mousePress(buttons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        afterEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Releases one or more mouse buttons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public synchronized void mouseRelease(int buttons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        checkButtonsArgument(buttons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        peer.mouseRelease(buttons);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        afterEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private void checkButtonsArgument(int buttons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if ( (buttons|LEGAL_BUTTON_MASK) != LEGAL_BUTTON_MASK ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throw new IllegalArgumentException("Invalid combination of button flags");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Rotates the scroll wheel on wheel-equipped mice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param wheelAmt  number of "notches" to move the mouse wheel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *                  Negative values indicate movement up/away from the user,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *                  positive values indicate movement down/towards the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public synchronized void mouseWheel(int wheelAmt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        peer.mouseWheel(wheelAmt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        afterEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Presses a given key.  The key should be released using the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   343
     * {@code keyRelease} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Key codes that have more than one physical key associated with them
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   346
     * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * left or right shift key) will map to the left key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   349
     * @param   keycode Key to press (e.g. {@code KeyEvent.VK_A})
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   350
     * @throws  IllegalArgumentException if {@code keycode} is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          a valid key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see     #keyRelease(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @see     java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public synchronized void keyPress(int keycode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        checkKeycodeArgument(keycode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        peer.keyPress(keycode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        afterEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Releases a given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Key codes that have more than one physical key associated with them
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   365
     * (e.g. {@code KeyEvent.VK_SHIFT} could mean either the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * left or right shift key) will map to the left key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   368
     * @param   keycode Key to release (e.g. {@code KeyEvent.VK_A})
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   369
     * @throws  IllegalArgumentException if {@code keycode} is not a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *          valid key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see  #keyPress(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @see     java.awt.event.KeyEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public synchronized void keyRelease(int keycode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        checkKeycodeArgument(keycode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        peer.keyRelease(keycode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        afterEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private void checkKeycodeArgument(int keycode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // rather than build a big table or switch statement here, we'll
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // just check that the key isn't VK_UNDEFINED and assume that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // peer implementations will throw an exception for other bogus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // values e.g. -1, 999999
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (keycode == KeyEvent.VK_UNDEFINED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            throw new IllegalArgumentException("Invalid key code");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns the color of a pixel at the given screen coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param   x       X position of pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param   y       Y position of pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return  Color of the pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public synchronized Color getPixelColor(int x, int y) {
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   397
        AffineTransform tx = GraphicsEnvironment.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   398
                getLocalGraphicsEnvironment().getDefaultScreenDevice().
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   399
                getDefaultConfiguration().getDefaultTransform();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   400
        x = (int) (x * tx.getScaleX());
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   401
        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
   402
        Color color = new Color(peer.getRGBPixel(x, y));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Creates an image containing pixels read from the screen.  This image does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * not include the mouse cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param   screenRect      Rect to capture in screen coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @return  The captured image
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   411
     * @throws  IllegalArgumentException if {@code screenRect} width and height are not greater than zero
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   412
     * @throws  SecurityException if {@code readDisplayPixels} permission is not granted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @see     SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @see     AWTPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public synchronized BufferedImage createScreenCapture(Rectangle screenRect) {
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   417
        return createCompatibleImage(screenRect, false)[0];
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   418
    }
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
     * Creates an image containing pixels read from the screen.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   422
     * This image does not include the mouse cursor.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   423
     * 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
   424
     * from user space to screen (device) space.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   425
     * Typically this means that the display is a high resolution screen,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   426
     * 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
   427
     * Returns a {@link java.awt.image.MultiResolutionImage}.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   428
     * <p>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   429
     * For a non-scaled display, the {@code MultiResolutionImage}
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   430
     * will have one image variant:
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   431
     * <ul>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   432
     * <li> Base Image with user specified size.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   433
     * </ul>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   434
     * <p>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   435
     * For a high resolution display where there is a scaling transform,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   436
     * the {@code MultiResolutionImage} will have two image variants:
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   437
     * <ul>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   438
     * <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
   439
     * <li> Native device resolution image with device size pixels.
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   440
     * </ul>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   441
     * <p>
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   442
     * Example:
44134
0821d790675b 8175066: Compilation error due to tag in JDK-8162959
pkbalakr
parents: 44130
diff changeset
   443
     * <pre>{@code
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   444
     *      Image nativeResImage;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   445
     *      MultiResolutionImage mrImage = robot.createMultiResolutionScreenCapture(frame.getBounds());
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   446
     *      List<Image> resolutionVariants = mrImage.getResolutionVariants();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   447
     *      if (resolutionVariants.size() > 1) {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   448
     *          nativeResImage = resolutionVariants.get(1);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   449
     *      } else {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   450
     *          nativeResImage = resolutionVariants.get(0);
44134
0821d790675b 8175066: Compilation error due to tag in JDK-8162959
pkbalakr
parents: 44130
diff changeset
   451
     *      }
0821d790675b 8175066: Compilation error due to tag in JDK-8162959
pkbalakr
parents: 44130
diff changeset
   452
     * }</pre>
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   453
     * @param   screenRect     Rect to capture in screen coordinates
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   454
     * @return  The captured image
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   455
     * @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
   456
     * @throws  SecurityException if {@code readDisplayPixels} permission is not granted
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   457
     * @see     SecurityManager#checkPermission
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   458
     * @see     AWTPermission
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   459
     *
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   460
     * @since 9
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   461
     */
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   462
    public synchronized MultiResolutionImage
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   463
            createMultiResolutionScreenCapture(Rectangle screenRect) {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   464
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   465
        return new BaseMultiResolutionImage(
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   466
                createCompatibleImage(screenRect, true));
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   467
    }
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
    private synchronized BufferedImage[]
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   470
            createCompatibleImage(Rectangle screenRect, boolean isHiDPI) {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   471
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        checkScreenCaptureAllowed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
23274
3b6993da89df 8013116: Robot moves mouse to point which differs from set in mouseMove on Unity shell
bagiras
parents: 5506
diff changeset
   474
        checkValidRect(screenRect);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   476
        BufferedImage lowResolutionImage;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   477
        BufferedImage highResolutionImage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        DataBufferInt buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        WritableRaster raster;
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   480
        BufferedImage[] imageArray;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (screenCapCM == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
             * Fix for 4285201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
             * Create a DirectColorModel equivalent to the default RGB ColorModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
             * except with no Alpha component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            screenCapCM = new DirectColorModel(24,
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   490
                    /* red mask */ 0x00FF0000,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   491
                    /* green mask */ 0x0000FF00,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   492
                    /* blue mask */ 0x000000FF);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   495
        int[] bandmasks = new int[3];
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   496
        bandmasks[0] = screenCapCM.getRedMask();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   497
        bandmasks[1] = screenCapCM.getGreenMask();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   498
        bandmasks[2] = screenCapCM.getBlueMask();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   499
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   500
        // need to sync the toolkit prior to grabbing the pixels since in some
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   501
        // cases rendering to the screen may be delayed
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   502
        Toolkit.getDefaultToolkit().sync();
44152
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   503
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   504
        GraphicsConfiguration gc = GraphicsEnvironment
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   505
                .getLocalGraphicsEnvironment()
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   506
                .getDefaultScreenDevice().
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   507
                getDefaultConfiguration();
50482
18f8e3b6f3b7 8196030: AWT Robot mouseMove fails on Windows 10 1709 with HiDPI
serb
parents: 47216
diff changeset
   508
        gc = SunGraphicsEnvironment.getGraphicsConfigurationAtPoint(
44152
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   509
                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
   510
af74e15ef830 8176097: Window set location to a display with different DPI does not properly work
alexsch
parents: 44145
diff changeset
   511
        AffineTransform tx = gc.getDefaultTransform();
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   512
        double uiScaleX = tx.getScaleX();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   513
        double uiScaleY = tx.getScaleY();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        int pixels[];
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   515
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   516
        if (uiScaleX == 1 && uiScaleY == 1) {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   517
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   518
            pixels = peer.getRGBPixels(screenRect);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   519
            buffer = new DataBufferInt(pixels, pixels.length);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   520
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   521
            bandmasks[0] = screenCapCM.getRedMask();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   522
            bandmasks[1] = screenCapCM.getGreenMask();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   523
            bandmasks[2] = screenCapCM.getBlueMask();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   524
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   525
            raster = Raster.createPackedRaster(buffer, screenRect.width,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   526
                    screenRect.height, screenRect.width, bandmasks, null);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   527
            SunWritableRaster.makeTrackable(buffer);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   529
            highResolutionImage = new BufferedImage(screenCapCM, raster,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   530
                    false, null);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   531
            imageArray = new BufferedImage[1];
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   532
            imageArray[0] = highResolutionImage;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   533
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   534
        } else {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   535
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   536
            int sX = (int) Math.floor(screenRect.x * uiScaleX);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   537
            int sY = (int) Math.floor(screenRect.y * uiScaleY);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   538
            int sWidth = (int) Math.ceil(screenRect.width * uiScaleX);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   539
            int sHeight = (int) Math.ceil(screenRect.height * uiScaleY);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   540
            int temppixels[];
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   541
            Rectangle scaledRect = new Rectangle(sX, sY, sWidth, sHeight);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   542
            temppixels = peer.getRGBPixels(scaledRect);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   544
            // HighResolutionImage
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   545
            pixels = temppixels;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   546
            buffer = new DataBufferInt(pixels, pixels.length);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   547
            raster = Raster.createPackedRaster(buffer, scaledRect.width,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   548
                    scaledRect.height, scaledRect.width, bandmasks, null);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   549
            SunWritableRaster.makeTrackable(buffer);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   550
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   551
            highResolutionImage = new BufferedImage(screenCapCM, raster,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   552
                    false, null);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   553
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   555
            // LowResolutionImage
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   556
            lowResolutionImage = new BufferedImage(screenRect.width,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   557
                    screenRect.height, highResolutionImage.getType());
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   558
            Graphics2D g = lowResolutionImage.createGraphics();
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   559
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   560
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   561
            g.setRenderingHint(RenderingHints.KEY_RENDERING,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   562
                    RenderingHints.VALUE_RENDER_QUALITY);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   563
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   564
                    RenderingHints.VALUE_ANTIALIAS_ON);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   565
            g.drawImage(highResolutionImage, 0, 0,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   566
                    screenRect.width, screenRect.height,
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   567
                    0, 0, scaledRect.width, scaledRect.height, null);
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   568
            g.dispose();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   570
            if(!isHiDPI) {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   571
                imageArray = new BufferedImage[1];
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   572
                imageArray[0] = lowResolutionImage;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   573
            } else {
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   574
                imageArray = new BufferedImage[2];
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   575
                imageArray[0] = lowResolutionImage;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   576
                imageArray[1] = highResolutionImage;
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   577
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
44130
f3516efccffa 8162959: [HiDPI] screenshot artifacts using AWT Robot
pkbalakr
parents: 43722
diff changeset
   579
        }
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
        return imageArray;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    private static void checkValidRect(Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (rect.width <= 0 || rect.height <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            throw new IllegalArgumentException("Rectangle width and height must be > 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    private static void checkScreenCaptureAllowed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        if (security != null) {
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 5506
diff changeset
   593
            security.checkPermission(AWTPermissions.READ_DISPLAY_PIXELS_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Called after an event is generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    private void afterEvent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        autoWaitForIdle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        autoDelay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   606
     * Returns whether this Robot automatically invokes {@code waitForIdle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * after generating an event.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   608
     * @return Whether {@code waitForIdle} is automatically called
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public synchronized boolean isAutoWaitForIdle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return isAutoWaitForIdle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   615
     * Sets whether this Robot automatically invokes {@code waitForIdle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * after generating an event.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   617
     * @param   isOn    Whether {@code waitForIdle} is automatically invoked
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    public synchronized void setAutoWaitForIdle(boolean isOn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        isAutoWaitForIdle = isOn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Calls waitForIdle after every event if so desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    private void autoWaitForIdle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        if (isAutoWaitForIdle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            waitForIdle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * Returns the number of milliseconds this Robot sleeps after generating an event.
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   634
     *
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   635
     * @return the delay duration in milliseconds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    public synchronized int getAutoDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return autoDelay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Sets the number of milliseconds this Robot sleeps after generating an event.
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   643
     *
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   644
     * @param  ms the delay duration in milliseconds
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   645
     * @throws IllegalArgumentException If {@code ms}
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   646
     *         is not between 0 and 60,000 milliseconds inclusive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public synchronized void setAutoDelay(int ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        checkDelayArgument(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        autoDelay = ms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Automatically sleeps for the specified interval after event generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    private void autoDelay() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        delay(autoDelay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Sleeps for the specified time.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   662
     * To catch any {@code InterruptedException}s that occur,
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32493
diff changeset
   663
     * {@code Thread.sleep()} may be used instead.
25162
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   664
     *
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   665
     * @param  ms time to sleep in milliseconds
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   666
     * @throws IllegalArgumentException if {@code ms}
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   667
     *         is not between 0 and 60,000 milliseconds inclusive
c388078278d4 8043967: Fix doclint warnings for java.awt
yan
parents: 23717
diff changeset
   668
     * @see java.lang.Thread#sleep
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public synchronized void delay(int ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        checkDelayArgument(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            Thread.sleep(ms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        } catch(InterruptedException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            ite.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    private void checkDelayArgument(int ms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        if (ms < 0 || ms > MAX_DELAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Waits until all events currently on the event queue have been processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @throws  IllegalThreadStateException if called on the AWT event dispatching thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
    public synchronized void waitForIdle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        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
   691
        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
   692
        ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    private void checkNotDispatchThread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Returns a string representation of this Robot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * @return  the string representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     */
32493
31bd2a308840 8076178: [macosx] Few open swing and awt reg-tests fail after their update to avoid SunToolkit.realSync
serb
parents: 27509
diff changeset
   706
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    public synchronized String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        String params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return getClass().getName() + "[ " + params + " ]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
}