< prev index next >

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

Print this page




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


 482             /*
 483              * Fix for 4285201
 484              * Create a DirectColorModel equivalent to the default RGB ColorModel,
 485              * except with no Alpha component.
 486              */
 487 
 488             screenCapCM = new DirectColorModel(24,
 489                     /* red mask */ 0x00FF0000,
 490                     /* green mask */ 0x0000FF00,
 491                     /* blue mask */ 0x000000FF);
 492         }
 493 
 494         int[] bandmasks = new int[3];
 495         bandmasks[0] = screenCapCM.getRedMask();
 496         bandmasks[1] = screenCapCM.getGreenMask();
 497         bandmasks[2] = screenCapCM.getBlueMask();
 498 
 499         // need to sync the toolkit prior to grabbing the pixels since in some
 500         // cases rendering to the screen may be delayed
 501         Toolkit.getDefaultToolkit().sync();
 502         AffineTransform tx = GraphicsEnvironment.
 503                 getLocalGraphicsEnvironment().getDefaultScreenDevice().
 504                 getDefaultConfiguration().getDefaultTransform();






 505         double uiScaleX = tx.getScaleX();
 506         double uiScaleY = tx.getScaleY();
 507         int pixels[];
 508 
 509         if (uiScaleX == 1 && uiScaleY == 1) {
 510 
 511             pixels = peer.getRGBPixels(screenRect);
 512             buffer = new DataBufferInt(pixels, pixels.length);
 513 
 514             bandmasks[0] = screenCapCM.getRedMask();
 515             bandmasks[1] = screenCapCM.getGreenMask();
 516             bandmasks[2] = screenCapCM.getBlueMask();
 517 
 518             raster = Raster.createPackedRaster(buffer, screenRect.width,
 519                     screenRect.height, screenRect.width, bandmasks, null);
 520             SunWritableRaster.makeTrackable(buffer);
 521 
 522             highResolutionImage = new BufferedImage(screenCapCM, raster,
 523                     false, null);
 524             imageArray = new BufferedImage[1];




  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.


 483             /*
 484              * Fix for 4285201
 485              * Create a DirectColorModel equivalent to the default RGB ColorModel,
 486              * except with no Alpha component.
 487              */
 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 
 529             highResolutionImage = new BufferedImage(screenCapCM, raster,
 530                     false, null);
 531             imageArray = new BufferedImage[1];


< prev index next >