< prev index next >

src/java.desktop/share/classes/java/awt/Robot.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.event.InputEvent;
  29 import java.awt.event.KeyEvent;
  30 import java.awt.geom.AffineTransform;
  31 import java.awt.image.BaseMultiResolutionImage;
  32 import java.awt.image.MultiResolutionImage;
  33 import java.awt.image.BufferedImage;
  34 import java.awt.image.DataBufferInt;
  35 import java.awt.image.DirectColorModel;

  36 import java.awt.image.Raster;
  37 import java.awt.image.WritableRaster;
  38 import java.awt.peer.RobotPeer;
  39 
  40 import sun.awt.AWTPermissions;
  41 import sun.awt.ComponentFactory;
  42 import sun.awt.SunToolkit;
  43 import sun.awt.image.SunWritableRaster;
  44 import sun.swing.SwingUtilities2;
  45 
  46 /**
  47  * This class is used to generate native system input events
  48  * for the purposes of test automation, self-running demos, and
  49  * other applications where control of the mouse and keyboard
  50  * is needed. The primary purpose of Robot is to facilitate
  51  * automated testing of Java platform implementations.
  52  * <p>
  53  * Using the class to generate input events differs from posting
  54  * events to the AWT event queue or AWT components in that the
  55  * events are generated in the platform's native input
  56  * queue. For example, {@code Robot.mouseMove} will actually move
  57  * the mouse cursor instead of just generating mouse move events.
  58  * <p>
  59  * Note that some platforms require special privileges or extensions
  60  * to access low-level input control. If the current platform configuration
  61  * does not allow input control, an {@code AWTException} will be thrown
  62  * when trying to construct Robot objects. For example, X-Window systems
  63  * will throw the exception if the XTEST 2.2 standard extension is not supported
  64  * (or not enabled) by the X server.


 488 
 489             screenCapCM = new DirectColorModel(24,
 490                     /* red mask */ 0x00FF0000,
 491                     /* green mask */ 0x0000FF00,
 492                     /* blue mask */ 0x000000FF);
 493         }
 494 
 495         int[] bandmasks = new int[3];
 496         bandmasks[0] = screenCapCM.getRedMask();
 497         bandmasks[1] = screenCapCM.getGreenMask();
 498         bandmasks[2] = screenCapCM.getBlueMask();
 499 
 500         // need to sync the toolkit prior to grabbing the pixels since in some
 501         // cases rendering to the screen may be delayed
 502         Toolkit.getDefaultToolkit().sync();
 503 
 504         GraphicsConfiguration gc = GraphicsEnvironment
 505                 .getLocalGraphicsEnvironment()
 506                 .getDefaultScreenDevice().
 507                 getDefaultConfiguration();
 508         gc = SwingUtilities2.getGraphicsConfigurationAtPoint(
 509                 gc, screenRect.getCenterX(), screenRect.getCenterY());
 510 
 511         AffineTransform tx = gc.getDefaultTransform();
 512         double uiScaleX = tx.getScaleX();
 513         double uiScaleY = tx.getScaleY();
 514         int pixels[];
 515 
 516         if (uiScaleX == 1 && uiScaleY == 1) {
 517 
 518             pixels = peer.getRGBPixels(screenRect);
 519             buffer = new DataBufferInt(pixels, pixels.length);
 520 
 521             bandmasks[0] = screenCapCM.getRedMask();
 522             bandmasks[1] = screenCapCM.getGreenMask();
 523             bandmasks[2] = screenCapCM.getBlueMask();
 524 
 525             raster = Raster.createPackedRaster(buffer, screenRect.width,
 526                     screenRect.height, screenRect.width, bandmasks, null);
 527             SunWritableRaster.makeTrackable(buffer);
 528 


   1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.event.InputEvent;
  29 import java.awt.event.KeyEvent;
  30 import java.awt.geom.AffineTransform;
  31 import java.awt.image.BaseMultiResolutionImage;

  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.DataBufferInt;
  34 import java.awt.image.DirectColorModel;
  35 import java.awt.image.MultiResolutionImage;
  36 import java.awt.image.Raster;
  37 import java.awt.image.WritableRaster;
  38 import java.awt.peer.RobotPeer;
  39 
  40 import sun.awt.AWTPermissions;
  41 import sun.awt.ComponentFactory;
  42 import sun.awt.SunToolkit;
  43 import sun.awt.image.SunWritableRaster;
  44 import sun.java2d.SunGraphicsEnvironment;
  45 
  46 /**
  47  * This class is used to generate native system input events
  48  * for the purposes of test automation, self-running demos, and
  49  * other applications where control of the mouse and keyboard
  50  * is needed. The primary purpose of Robot is to facilitate
  51  * automated testing of Java platform implementations.
  52  * <p>
  53  * Using the class to generate input events differs from posting
  54  * events to the AWT event queue or AWT components in that the
  55  * events are generated in the platform's native input
  56  * queue. For example, {@code Robot.mouseMove} will actually move
  57  * the mouse cursor instead of just generating mouse move events.
  58  * <p>
  59  * Note that some platforms require special privileges or extensions
  60  * to access low-level input control. If the current platform configuration
  61  * does not allow input control, an {@code AWTException} will be thrown
  62  * when trying to construct Robot objects. For example, X-Window systems
  63  * will throw the exception if the XTEST 2.2 standard extension is not supported
  64  * (or not enabled) by the X server.


 488 
 489             screenCapCM = new DirectColorModel(24,
 490                     /* red mask */ 0x00FF0000,
 491                     /* green mask */ 0x0000FF00,
 492                     /* blue mask */ 0x000000FF);
 493         }
 494 
 495         int[] bandmasks = new int[3];
 496         bandmasks[0] = screenCapCM.getRedMask();
 497         bandmasks[1] = screenCapCM.getGreenMask();
 498         bandmasks[2] = screenCapCM.getBlueMask();
 499 
 500         // need to sync the toolkit prior to grabbing the pixels since in some
 501         // cases rendering to the screen may be delayed
 502         Toolkit.getDefaultToolkit().sync();
 503 
 504         GraphicsConfiguration gc = GraphicsEnvironment
 505                 .getLocalGraphicsEnvironment()
 506                 .getDefaultScreenDevice().
 507                 getDefaultConfiguration();
 508         gc = SunGraphicsEnvironment.getGraphicsConfigurationAtPoint(
 509                 gc, screenRect.getCenterX(), screenRect.getCenterY());
 510 
 511         AffineTransform tx = gc.getDefaultTransform();
 512         double uiScaleX = tx.getScaleX();
 513         double uiScaleY = tx.getScaleY();
 514         int pixels[];
 515 
 516         if (uiScaleX == 1 && uiScaleY == 1) {
 517 
 518             pixels = peer.getRGBPixels(screenRect);
 519             buffer = new DataBufferInt(pixels, pixels.length);
 520 
 521             bandmasks[0] = screenCapCM.getRedMask();
 522             bandmasks[1] = screenCapCM.getGreenMask();
 523             bandmasks[2] = screenCapCM.getBlueMask();
 524 
 525             raster = Raster.createPackedRaster(buffer, screenRect.width,
 526                     screenRect.height, screenRect.width, bandmasks, null);
 527             SunWritableRaster.makeTrackable(buffer);
 528 


< prev index next >