home *** CD-ROM | disk | FTP | other *** search
/ Sky at Night 2007 June / SAN CD 6-2007 CD-ROM 25.iso / pc / Software / AstroGrav_Win / Java / jre1.6.0 / lib / rt.jar / java / awt / Robot.class (.txt) < prev    next >
Encoding:
Java Class File  |  2006-11-29  |  5.8 KB  |  223 lines

  1. package java.awt;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.awt.image.DataBufferInt;
  5. import java.awt.image.DirectColorModel;
  6. import java.awt.image.Raster;
  7. import java.awt.image.WritableRaster;
  8. import java.awt.peer.RobotPeer;
  9. import java.lang.reflect.InvocationTargetException;
  10. import java.util.Hashtable;
  11. import sun.awt.ComponentFactory;
  12. import sun.awt.SunToolkit;
  13. import sun.security.util.SecurityConstants;
  14.  
  15. public class Robot {
  16.    private static final int MAX_DELAY = 60000;
  17.    private RobotPeer peer;
  18.    private boolean isAutoWaitForIdle = false;
  19.    private int autoDelay = 0;
  20.    private static final int LEGAL_BUTTON_MASK = 28;
  21.    private Point gdLoc;
  22.    private DirectColorModel screenCapCM = null;
  23.  
  24.    public Robot() throws AWTException {
  25.       if (GraphicsEnvironment.isHeadless()) {
  26.          throw new AWTException("headless environment");
  27.       } else {
  28.          this.init(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
  29.       }
  30.    }
  31.  
  32.    public Robot(GraphicsDevice var1) throws AWTException {
  33.       this.checkIsScreenDevice(var1);
  34.       this.init(var1);
  35.    }
  36.  
  37.    private void init(GraphicsDevice var1) throws AWTException {
  38.       this.checkRobotAllowed();
  39.       this.gdLoc = var1.getDefaultConfiguration().getBounds().getLocation();
  40.       Toolkit var2 = Toolkit.getDefaultToolkit();
  41.       if (var2 instanceof ComponentFactory) {
  42.          this.peer = ((ComponentFactory)var2).createRobot(this, var1);
  43.       }
  44.  
  45.    }
  46.  
  47.    private void checkRobotAllowed() {
  48.       SecurityManager var1 = System.getSecurityManager();
  49.       if (var1 != null) {
  50.          var1.checkPermission(SecurityConstants.CREATE_ROBOT_PERMISSION);
  51.       }
  52.  
  53.    }
  54.  
  55.    private void checkIsScreenDevice(GraphicsDevice var1) {
  56.       if (var1 == null || var1.getType() != 0) {
  57.          throw new IllegalArgumentException("not a valid screen device");
  58.       }
  59.    }
  60.  
  61.    public synchronized void mouseMove(int var1, int var2) {
  62.       this.peer.mouseMove(this.gdLoc.x + var1, this.gdLoc.y + var2);
  63.       this.afterEvent();
  64.    }
  65.  
  66.    public synchronized void mousePress(int var1) {
  67.       this.checkButtonsArgument(var1);
  68.       this.peer.mousePress(var1);
  69.       this.afterEvent();
  70.    }
  71.  
  72.    public synchronized void mouseRelease(int var1) {
  73.       this.checkButtonsArgument(var1);
  74.       this.peer.mouseRelease(var1);
  75.       this.afterEvent();
  76.    }
  77.  
  78.    private void checkButtonsArgument(int var1) {
  79.       if ((var1 | 28) != 28) {
  80.          throw new IllegalArgumentException("Invalid combination of button flags");
  81.       }
  82.    }
  83.  
  84.    public synchronized void mouseWheel(int var1) {
  85.       this.peer.mouseWheel(var1);
  86.       this.afterEvent();
  87.    }
  88.  
  89.    public synchronized void keyPress(int var1) {
  90.       this.checkKeycodeArgument(var1);
  91.       this.peer.keyPress(var1);
  92.       this.afterEvent();
  93.    }
  94.  
  95.    public synchronized void keyRelease(int var1) {
  96.       this.checkKeycodeArgument(var1);
  97.       this.peer.keyRelease(var1);
  98.       this.afterEvent();
  99.    }
  100.  
  101.    private void checkKeycodeArgument(int var1) {
  102.       if (var1 == 0) {
  103.          throw new IllegalArgumentException("Invalid key code");
  104.       }
  105.    }
  106.  
  107.    public synchronized Color getPixelColor(int var1, int var2) {
  108.       Color var3 = new Color(this.peer.getRGBPixel(this.gdLoc.x + var1, this.gdLoc.y + var2));
  109.       return var3;
  110.    }
  111.  
  112.    public synchronized BufferedImage createScreenCapture(Rectangle var1) {
  113.       checkScreenCaptureAllowed();
  114.       Rectangle var2 = new Rectangle(var1);
  115.       var2.translate(this.gdLoc.x, this.gdLoc.y);
  116.       checkValidRect(var2);
  117.       if (this.screenCapCM == null) {
  118.          this.screenCapCM = new DirectColorModel(24, 16711680, 65280, 255);
  119.       }
  120.  
  121.       int[] var7 = new int[3];
  122.       int[] var6 = this.peer.getRGBPixels(var2);
  123.       DataBufferInt var4 = new DataBufferInt(var6, var6.length);
  124.       var7[0] = this.screenCapCM.getRedMask();
  125.       var7[1] = this.screenCapCM.getGreenMask();
  126.       var7[2] = this.screenCapCM.getBlueMask();
  127.       WritableRaster var5 = Raster.createPackedRaster(var4, var2.width, var2.height, var2.width, var7, (Point)null);
  128.       BufferedImage var3 = new BufferedImage(this.screenCapCM, var5, false, (Hashtable)null);
  129.       return var3;
  130.    }
  131.  
  132.    private static void checkValidRect(Rectangle var0) {
  133.       if (var0.width <= 0 || var0.height <= 0) {
  134.          throw new IllegalArgumentException("Rectangle width and height must be > 0");
  135.       }
  136.    }
  137.  
  138.    private static void checkScreenCaptureAllowed() {
  139.       SecurityManager var0 = System.getSecurityManager();
  140.       if (var0 != null) {
  141.          var0.checkPermission(SecurityConstants.READ_DISPLAY_PIXELS_PERMISSION);
  142.       }
  143.  
  144.    }
  145.  
  146.    private void afterEvent() {
  147.       this.autoWaitForIdle();
  148.       this.autoDelay();
  149.    }
  150.  
  151.    public synchronized boolean isAutoWaitForIdle() {
  152.       return this.isAutoWaitForIdle;
  153.    }
  154.  
  155.    public synchronized void setAutoWaitForIdle(boolean var1) {
  156.       this.isAutoWaitForIdle = var1;
  157.    }
  158.  
  159.    private void autoWaitForIdle() {
  160.       if (this.isAutoWaitForIdle) {
  161.          this.waitForIdle();
  162.       }
  163.  
  164.    }
  165.  
  166.    public synchronized int getAutoDelay() {
  167.       return this.autoDelay;
  168.    }
  169.  
  170.    public synchronized void setAutoDelay(int var1) {
  171.       this.checkDelayArgument(var1);
  172.       this.autoDelay = var1;
  173.    }
  174.  
  175.    private void autoDelay() {
  176.       this.delay(this.autoDelay);
  177.    }
  178.  
  179.    public synchronized void delay(int var1) {
  180.       this.checkDelayArgument(var1);
  181.  
  182.       try {
  183.          Thread.sleep((long)var1);
  184.       } catch (InterruptedException var3) {
  185.          var3.printStackTrace();
  186.       }
  187.  
  188.    }
  189.  
  190.    private void checkDelayArgument(int var1) {
  191.       if (var1 < 0 || var1 > 60000) {
  192.          throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
  193.       }
  194.    }
  195.  
  196.    public synchronized void waitForIdle() {
  197.       this.checkNotDispatchThread();
  198.  
  199.       try {
  200.          SunToolkit.flushPendingEvents();
  201.          EventQueue.invokeAndWait(new 1(this));
  202.       } catch (InterruptedException var2) {
  203.          System.err.println("Robot.waitForIdle, non-fatal exception caught:");
  204.          var2.printStackTrace();
  205.       } catch (InvocationTargetException var3) {
  206.          System.err.println("Robot.waitForIdle, non-fatal exception caught:");
  207.          var3.printStackTrace();
  208.       }
  209.  
  210.    }
  211.  
  212.    private void checkNotDispatchThread() {
  213.       if (EventQueue.isDispatchThread()) {
  214.          throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
  215.       }
  216.    }
  217.  
  218.    public synchronized String toString() {
  219.       String var1 = "autoDelay = " + this.getAutoDelay() + ", " + "autoWaitForIdle = " + this.isAutoWaitForIdle();
  220.       return this.getClass().getName() + "[ " + var1 + " ]";
  221.    }
  222. }
  223.