|                                                                                                                                                                1    7    8   package java.awt; 9    10  import java.awt.peer.*; 11  import java.awt.image.*; 12  import java.awt.event.*; 13  import java.lang.reflect.InvocationTargetException   ; 14  import sun.awt.ComponentFactory; 15  import sun.awt.SunToolkit; 16  import sun.security.util.SecurityConstants; 17   18   45  public class Robot { 46      private static final int MAX_DELAY = 60000; 47      private RobotPeer peer; 48      private boolean isAutoWaitForIdle = false; 49      private int autoDelay = 0; 50      private static final int LEGAL_BUTTON_MASK =  51                          InputEvent.BUTTON1_MASK| 52                          InputEvent.BUTTON2_MASK| 53                          InputEvent.BUTTON3_MASK; 54   55      private DirectColorModel screenCapCM = null; 56       57       69      public Robot() throws AWTException    { 70          if (GraphicsEnvironment.isHeadless()) { 71              throw new AWTException   ("headless environment"); 72          } 73          init(GraphicsEnvironment.getLocalGraphicsEnvironment() 74              .getDefaultScreenDevice()); 75      } 76   77       105     public Robot(GraphicsDevice    screen) throws AWTException    { 106     checkIsScreenDevice(screen); 107         init(screen); 108     } 109  110     private void init(GraphicsDevice    screen) throws AWTException    { 111         checkRobotAllowed(); 112         Toolkit    toolkit = Toolkit.getDefaultToolkit(); 113         if (toolkit instanceof ComponentFactory) { 114             peer = ((ComponentFactory)toolkit).createRobot(this, screen); 115         } 116     } 117  118      119     private void checkRobotAllowed() { 120     SecurityManager    security = System.getSecurityManager(); 121     if (security != null) { 122         security.checkPermission(SecurityConstants.CREATE_ROBOT_PERMISSION); 123     } 124     } 125  126      127     private void checkIsScreenDevice(GraphicsDevice    device) { 128         if (device == null || device.getType() != GraphicsDevice.TYPE_RASTER_SCREEN) { 129             throw new IllegalArgumentException   ("not a valid screen device"); 130         } 131     } 132  133      138     public synchronized void mouseMove(int x, int y) { 139     peer.mouseMove(x,y); 140     afterEvent(); 141     } 142  143      158     public synchronized void mousePress(int buttons) { 159     checkButtonsArgument(buttons); 160     peer.mousePress(buttons); 161     afterEvent(); 162     } 163      164      178     public synchronized void mouseRelease(int buttons) { 179     checkButtonsArgument(buttons); 180     peer.mouseRelease(buttons); 181     afterEvent(); 182     } 183  184     private void checkButtonsArgument(int buttons) { 185     if ( (buttons|LEGAL_BUTTON_MASK) != LEGAL_BUTTON_MASK ) { 186         throw new IllegalArgumentException   ("Invalid combination of button flags"); 187     } 188     } 189  190      199     public synchronized void mouseWheel(int wheelAmt) { 200         peer.mouseWheel(wheelAmt); 201         afterEvent(); 202     } 203  204      218     public synchronized void keyPress(int keycode) { 219     checkKeycodeArgument(keycode); 220     peer.keyPress(keycode); 221     afterEvent(); 222     } 223      224      237     public synchronized void keyRelease(int keycode) { 238     checkKeycodeArgument(keycode); 239     peer.keyRelease(keycode); 240     afterEvent(); 241     } 242  243     private void checkKeycodeArgument(int keycode) { 244                     if (keycode == KeyEvent.VK_UNDEFINED) { 249         throw new IllegalArgumentException   ("Invalid key code"); 250     } 251     } 252  253      259     public synchronized Color    getPixelColor(int x, int y) { 260     Color    color = new Color   (peer.getRGBPixel(x,y)); 261     return color; 262     } 263  264      274     public synchronized BufferedImage createScreenCapture(Rectangle    screenRect) { 275     checkScreenCaptureAllowed(); 276     checkValidRect(screenRect); 277  278     BufferedImage image; 279     DataBufferInt buffer; 280     WritableRaster raster; 281  282     if (screenCapCM == null) { 283          288  289         screenCapCM = new DirectColorModel(24, 290                              0x00FF0000, 291                            0x0000FF00, 292                             0x000000FF); 293     } 294  295     int pixels[]; 296     int[] bandmasks = new int[3]; 297  298     pixels = peer.getRGBPixels(screenRect); 299     buffer = new DataBufferInt(pixels, pixels.length); 300  301     bandmasks[0] = screenCapCM.getRedMask(); 302     bandmasks[1] = screenCapCM.getGreenMask(); 303     bandmasks[2] = screenCapCM.getBlueMask(); 304  305     raster = Raster.createPackedRaster(buffer, screenRect.width, screenRect.height, screenRect.width, bandmasks, null); 306  307     image = new BufferedImage(screenCapCM, raster, false, null); 308  309     return image; 310     } 311  312     private static void checkValidRect(Rectangle    rect) { 313     if (rect.width <= 0 || rect.height <= 0) { 314         throw new IllegalArgumentException   ("Rectangle width and height must be > 0"); 315     } 316     } 317  318     private static void checkScreenCaptureAllowed() { 319     SecurityManager    security = System.getSecurityManager(); 320     if (security != null) { 321         security.checkPermission( 322         SecurityConstants.READ_DISPLAY_PIXELS_PERMISSION); 323     } 324     } 325  326      329     private void afterEvent() { 330     autoWaitForIdle(); 331     autoDelay(); 332     } 333  334      339     public synchronized boolean isAutoWaitForIdle() { 340     return isAutoWaitForIdle; 341     } 342  343      348     public synchronized void setAutoWaitForIdle(boolean isOn) { 349     isAutoWaitForIdle = isOn; 350     } 351      352      355     private void autoWaitForIdle() { 356     if (isAutoWaitForIdle) { 357         waitForIdle(); 358     } 359     } 360  361      364     public synchronized int getAutoDelay() { 365     return autoDelay; 366     } 367  368      372     public synchronized void setAutoDelay(int ms) { 373     checkDelayArgument(ms); 374     autoDelay = ms; 375     } 376  377      380     private void autoDelay() { 381     delay(autoDelay); 382     } 383  384      392     public synchronized void delay(int ms) { 393     checkDelayArgument(ms); 394     try { 395         Thread.sleep(ms); 396     } catch(InterruptedException    ite) { 397         ite.printStackTrace(); 398     } 399     } 400  401     private void checkDelayArgument(int ms) { 402     if (ms < 0 || ms > MAX_DELAY) { 403         throw new IllegalArgumentException   ("Delay must be to 0 to 60,000ms"); 404     } 405     } 406  407      411     public synchronized void waitForIdle() { 412     checkNotDispatchThread(); 413             try { 416             SunToolkit.flushPendingEvents(); 417         EventQueue.invokeAndWait( new Runnable   () {  418                         public void run() { 419                                                 } 421                     } ); 422     } catch(InterruptedException    ite) { 423         System.err.println("Robot.waitForIdle, non-fatal exception caught:"); 424         ite.printStackTrace(); 425     } catch(InvocationTargetException    ine) { 426         System.err.println("Robot.waitForIdle, non-fatal exception caught:"); 427         ine.printStackTrace(); 428     } 429     } 430  431     private void checkNotDispatchThread() {      432     if (EventQueue.isDispatchThread()) { 433         throw new IllegalThreadStateException   ("Cannot call method from the event dispatcher thread"); 434     } 435     } 436  437      442     public synchronized String    toString() { 443     String    params = "autoDelay = "+getAutoDelay()+", "+"autoWaitForIdle = "+isAutoWaitForIdle(); 444     return getClass().getName() + "[ " + params + " ]"; 445     } 446 } 447                                                                                                                                                                                                                                                                                                                           |                                                                                                                                                                                                                                                                                                                                                                                                            Popular Tags                                                                                                                                                                                                                                                                     |