home *** CD-ROM | disk | FTP | other *** search
- package java.awt;
-
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import java.awt.image.DirectColorModel;
- import java.awt.image.Raster;
- import java.awt.image.WritableRaster;
- import java.awt.peer.RobotPeer;
- import java.lang.reflect.InvocationTargetException;
- import java.util.Hashtable;
- import sun.awt.ComponentFactory;
- import sun.awt.SunToolkit;
- import sun.security.util.SecurityConstants;
-
- public class Robot {
- private static final int MAX_DELAY = 60000;
- private RobotPeer peer;
- private boolean isAutoWaitForIdle = false;
- private int autoDelay = 0;
- private static final int LEGAL_BUTTON_MASK = 28;
- private Point gdLoc;
- private DirectColorModel screenCapCM = null;
-
- public Robot() throws AWTException {
- if (GraphicsEnvironment.isHeadless()) {
- throw new AWTException("headless environment");
- } else {
- this.init(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice());
- }
- }
-
- public Robot(GraphicsDevice var1) throws AWTException {
- this.checkIsScreenDevice(var1);
- this.init(var1);
- }
-
- private void init(GraphicsDevice var1) throws AWTException {
- this.checkRobotAllowed();
- this.gdLoc = var1.getDefaultConfiguration().getBounds().getLocation();
- Toolkit var2 = Toolkit.getDefaultToolkit();
- if (var2 instanceof ComponentFactory) {
- this.peer = ((ComponentFactory)var2).createRobot(this, var1);
- }
-
- }
-
- private void checkRobotAllowed() {
- SecurityManager var1 = System.getSecurityManager();
- if (var1 != null) {
- var1.checkPermission(SecurityConstants.CREATE_ROBOT_PERMISSION);
- }
-
- }
-
- private void checkIsScreenDevice(GraphicsDevice var1) {
- if (var1 == null || var1.getType() != 0) {
- throw new IllegalArgumentException("not a valid screen device");
- }
- }
-
- public synchronized void mouseMove(int var1, int var2) {
- this.peer.mouseMove(this.gdLoc.x + var1, this.gdLoc.y + var2);
- this.afterEvent();
- }
-
- public synchronized void mousePress(int var1) {
- this.checkButtonsArgument(var1);
- this.peer.mousePress(var1);
- this.afterEvent();
- }
-
- public synchronized void mouseRelease(int var1) {
- this.checkButtonsArgument(var1);
- this.peer.mouseRelease(var1);
- this.afterEvent();
- }
-
- private void checkButtonsArgument(int var1) {
- if ((var1 | 28) != 28) {
- throw new IllegalArgumentException("Invalid combination of button flags");
- }
- }
-
- public synchronized void mouseWheel(int var1) {
- this.peer.mouseWheel(var1);
- this.afterEvent();
- }
-
- public synchronized void keyPress(int var1) {
- this.checkKeycodeArgument(var1);
- this.peer.keyPress(var1);
- this.afterEvent();
- }
-
- public synchronized void keyRelease(int var1) {
- this.checkKeycodeArgument(var1);
- this.peer.keyRelease(var1);
- this.afterEvent();
- }
-
- private void checkKeycodeArgument(int var1) {
- if (var1 == 0) {
- throw new IllegalArgumentException("Invalid key code");
- }
- }
-
- public synchronized Color getPixelColor(int var1, int var2) {
- Color var3 = new Color(this.peer.getRGBPixel(this.gdLoc.x + var1, this.gdLoc.y + var2));
- return var3;
- }
-
- public synchronized BufferedImage createScreenCapture(Rectangle var1) {
- checkScreenCaptureAllowed();
- Rectangle var2 = new Rectangle(var1);
- var2.translate(this.gdLoc.x, this.gdLoc.y);
- checkValidRect(var2);
- if (this.screenCapCM == null) {
- this.screenCapCM = new DirectColorModel(24, 16711680, 65280, 255);
- }
-
- int[] var7 = new int[3];
- int[] var6 = this.peer.getRGBPixels(var2);
- DataBufferInt var4 = new DataBufferInt(var6, var6.length);
- var7[0] = this.screenCapCM.getRedMask();
- var7[1] = this.screenCapCM.getGreenMask();
- var7[2] = this.screenCapCM.getBlueMask();
- WritableRaster var5 = Raster.createPackedRaster(var4, var2.width, var2.height, var2.width, var7, (Point)null);
- BufferedImage var3 = new BufferedImage(this.screenCapCM, var5, false, (Hashtable)null);
- return var3;
- }
-
- private static void checkValidRect(Rectangle var0) {
- if (var0.width <= 0 || var0.height <= 0) {
- throw new IllegalArgumentException("Rectangle width and height must be > 0");
- }
- }
-
- private static void checkScreenCaptureAllowed() {
- SecurityManager var0 = System.getSecurityManager();
- if (var0 != null) {
- var0.checkPermission(SecurityConstants.READ_DISPLAY_PIXELS_PERMISSION);
- }
-
- }
-
- private void afterEvent() {
- this.autoWaitForIdle();
- this.autoDelay();
- }
-
- public synchronized boolean isAutoWaitForIdle() {
- return this.isAutoWaitForIdle;
- }
-
- public synchronized void setAutoWaitForIdle(boolean var1) {
- this.isAutoWaitForIdle = var1;
- }
-
- private void autoWaitForIdle() {
- if (this.isAutoWaitForIdle) {
- this.waitForIdle();
- }
-
- }
-
- public synchronized int getAutoDelay() {
- return this.autoDelay;
- }
-
- public synchronized void setAutoDelay(int var1) {
- this.checkDelayArgument(var1);
- this.autoDelay = var1;
- }
-
- private void autoDelay() {
- this.delay(this.autoDelay);
- }
-
- public synchronized void delay(int var1) {
- this.checkDelayArgument(var1);
-
- try {
- Thread.sleep((long)var1);
- } catch (InterruptedException var3) {
- var3.printStackTrace();
- }
-
- }
-
- private void checkDelayArgument(int var1) {
- if (var1 < 0 || var1 > 60000) {
- throw new IllegalArgumentException("Delay must be to 0 to 60,000ms");
- }
- }
-
- public synchronized void waitForIdle() {
- this.checkNotDispatchThread();
-
- try {
- SunToolkit.flushPendingEvents();
- EventQueue.invokeAndWait(new 1(this));
- } catch (InterruptedException var2) {
- System.err.println("Robot.waitForIdle, non-fatal exception caught:");
- var2.printStackTrace();
- } catch (InvocationTargetException var3) {
- System.err.println("Robot.waitForIdle, non-fatal exception caught:");
- var3.printStackTrace();
- }
-
- }
-
- private void checkNotDispatchThread() {
- if (EventQueue.isDispatchThread()) {
- throw new IllegalThreadStateException("Cannot call method from the event dispatcher thread");
- }
- }
-
- public synchronized String toString() {
- String var1 = "autoDelay = " + this.getAutoDelay() + ", " + "autoWaitForIdle = " + this.isAutoWaitForIdle();
- return this.getClass().getName() + "[ " + var1 + " ]";
- }
- }
-