Chiddix

DirectRobot

Jun 28th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 KB | None | 0 0
  1. import java.awt.AWTException;
  2. import java.awt.GraphicsDevice;
  3. import java.awt.GraphicsEnvironment;
  4. import java.awt.MouseInfo;
  5. import java.awt.Point;
  6. import java.awt.PointerInfo;
  7. import java.awt.Rectangle;
  8. import java.awt.Toolkit;
  9. import java.awt.peer.MouseInfoPeer;
  10. import java.awt.peer.RobotPeer;
  11. import java.lang.reflect.Field;
  12. import java.lang.reflect.Method;
  13.  
  14. import sun.awt.ComponentFactory;
  15.  
  16. public final class DirectRobot {
  17.    
  18.     public DirectRobot() throws AWTException {
  19.         this(null);
  20.     }
  21.  
  22.     public DirectRobot(GraphicsDevice device) throws AWTException {
  23.         if (device == null) {
  24.             device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  25.         }
  26.  
  27.         this.device = device;
  28.         Toolkit toolkit = Toolkit.getDefaultToolkit();
  29.         peer = ((ComponentFactory) toolkit).createRobot(null, device);
  30.         Class<?> peerClass = peer.getClass();
  31.         Method method = null;
  32.         int methodType = -1;
  33.         Object methodParam = null;
  34.        
  35.         try {
  36.             method = peerClass.getDeclaredMethod("getRGBPixels", new Class<?>[] { Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class });
  37.             methodType = 0;
  38.         } catch (Exception ex) {
  39.             ex.printStackTrace();
  40.         }
  41.        
  42.         if (methodType < 0) {
  43.             try {
  44.                 method = peerClass.getDeclaredMethod("getScreenPixels", new Class<?>[] { Rectangle.class, int[].class });
  45.                 methodType = 1;
  46.             } catch (Exception ex) {
  47.                 ex.printStackTrace();
  48.             }
  49.  
  50.         }
  51.        
  52.         if (methodType < 0) {
  53.             try {
  54.                 method = peerClass.getDeclaredMethod("getScreenPixels", new Class<?>[] { Integer.TYPE, Rectangle.class, int[].class });
  55.                 methodType = 2;
  56.                 GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  57.                 int count = devices.length;
  58.                 for (int i = 0; i != count; ++i) {
  59.                     if (device.equals(devices[i])) {
  60.                         methodParam = Integer.valueOf(i);
  61.                         break;
  62.                     }
  63.                 }
  64.             } catch (Exception ex) {
  65.                 ex.printStackTrace();
  66.             }
  67.         }
  68.        
  69.         if (methodType < 0) {
  70.             try {
  71.                 method = peerClass.getDeclaredMethod("getRGBPixelsImpl", new Class<?>[] { Class.forName("sun.awt.X11GraphicsConfig"), Integer.TYPE, Integer.TYPE, Integer.TYPE, Integer.TYPE, int[].class });
  72.                 methodType = 3;
  73.                 Field field = peerClass.getDeclaredField("xgc");
  74.                
  75.                 try {
  76.                     field.setAccessible(true);
  77.                     methodParam = field.get(peer);
  78.                 } finally {
  79.                     field.setAccessible(false);
  80.                 }
  81.             } catch (Exception ex) {
  82.                 ex.printStackTrace();
  83.             }
  84.         }
  85.  
  86.         if (methodType >= 0 && method != null && (methodType <= 1 || methodParam != null)) {
  87.             getRGBPixelsMethod = method;
  88.             getRGBPixelsMethodType = methodType;
  89.             getRGBPixelsMethodParam = methodParam;
  90.         } else {
  91.             System.out.println("WARNING: Failed to acquire direct method for grabbing pixels, please post this on the main thread!");
  92.             System.out.println();
  93.             System.out.println(peer.getClass().getName());
  94.             System.out.println();
  95.            
  96.             try {
  97.                 Method[] methods = peer.getClass().getDeclaredMethods();
  98.                 for (Method method1 : methods) {
  99.                     System.out.println(method1);
  100.                 }
  101.  
  102.             } catch (Exception ex) {
  103.                 ex.printStackTrace();
  104.             }
  105.         }
  106.     }
  107.  
  108.     public static GraphicsDevice getMouseInfo(Point point) {
  109.         if (!hasMouseInfoPeer) {
  110.             hasMouseInfoPeer = true;
  111.             try {
  112.                 Toolkit toolkit = Toolkit.getDefaultToolkit();
  113.                 Method method = toolkit.getClass().getDeclaredMethod("getMouseInfoPeer", new Class<?>[0]);
  114.                 try {
  115.                     method.setAccessible(true);
  116.                     mouseInfoPeer = (MouseInfoPeer) method.invoke(toolkit, new Object[0]);
  117.                 } finally {
  118.                     method.setAccessible(false);
  119.                 }
  120.             } catch (Exception ex) {
  121.                 ex.printStackTrace();
  122.             }
  123.         }
  124.         if (mouseInfoPeer != null) {
  125.             int device = mouseInfoPeer.fillPointWithCoords(point != null ? point : new Point());
  126.             GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
  127.             return devices[device];
  128.         }
  129.         PointerInfo info = MouseInfo.getPointerInfo();
  130.         if (point != null) {
  131.             Point location = info.getLocation();
  132.             point.x = location.x;
  133.             point.y = location.y;
  134.         }
  135.         return info.getDevice();
  136.     }
  137.  
  138.     public static int getNumberOfMouseButtons() {
  139.         return MouseInfo.getNumberOfButtons();
  140.     }
  141.  
  142.     public static GraphicsDevice getDefaultScreenDevice() {
  143.         return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  144.     }
  145.  
  146.     public static GraphicsDevice getScreenDevice() {
  147.         return getMouseInfo(null);
  148.     }
  149.  
  150.     public void mouseMove(int x, int y) {
  151.         peer.mouseMove(x, y);
  152.     }
  153.  
  154.     public void mousePress(int buttons) {
  155.         peer.mousePress(buttons);
  156.     }
  157.  
  158.     public void mouseRelease(int buttons) {
  159.         peer.mouseRelease(buttons);
  160.     }
  161.  
  162.     public void mouseWheel(int wheelAmt) {
  163.         peer.mouseWheel(wheelAmt);
  164.     }
  165.  
  166.     public void keyPress(int keycode) {
  167.         peer.keyPress(keycode);
  168.     }
  169.  
  170.     public void keyRelease(int keycode) {
  171.         peer.keyRelease(keycode);
  172.     }
  173.  
  174.     public int getRGBPixel(int x, int y) {
  175.         return peer.getRGBPixel(x, y);
  176.     }
  177.  
  178.     public int[] getRGBPixels(Rectangle bounds) {
  179.         return peer.getRGBPixels(bounds);
  180.     }
  181.  
  182.     public boolean getRGBPixels(int x, int y, int width, int height, int[] pixels) {
  183.         if (getRGBPixelsMethod != null) {
  184.             try {
  185.                 if (getRGBPixelsMethodType == 0) {
  186.                     getRGBPixelsMethod.invoke(peer, new Object[] { Integer.valueOf(x), Integer.valueOf(y), Integer.valueOf(width), Integer.valueOf(height), pixels });
  187.                 } else if (getRGBPixelsMethodType == 1) {
  188.                     getRGBPixelsMethod.invoke(peer, new Object[] { new Rectangle(x, y, width, height), pixels });
  189.                 } else if (getRGBPixelsMethodType == 2) {
  190.                     getRGBPixelsMethod.invoke(peer, new Object[] { getRGBPixelsMethodParam, new Rectangle(x, y, width, height), pixels });
  191.                 } else {
  192.                     getRGBPixelsMethod.invoke(peer, new Object[] { getRGBPixelsMethodParam, Integer.valueOf(x), Integer.valueOf(y), Integer.valueOf(width), Integer.valueOf(height), pixels });
  193.                 }
  194.                 return true;
  195.             } catch (Exception ex) {
  196.                 ex.printStackTrace();
  197.             }
  198.         }
  199.        
  200.         int[] tmp = getRGBPixels(new Rectangle(x, y, width, height));
  201.         System.arraycopy(tmp, 0, pixels, 0, width * height);
  202.         return false;
  203.     }
  204.  
  205.     public void dispose() {
  206.         getRGBPixelsMethodParam = null;
  207.         Method method = getRGBPixelsMethod;
  208.        
  209.         if (method != null) {
  210.             getRGBPixelsMethod = null;
  211.             try {
  212.                 method.setAccessible(false);
  213.             } catch (Exception ex) {
  214.             }
  215.         }
  216.        
  217.         try {
  218.             peer.getClass().getDeclaredMethod("dispose", new Class<?>[0]).invoke(peer, new Class<?>[0]);
  219.         } catch (Exception ex) {
  220.             ex.printStackTrace();
  221.         }
  222.     }
  223.  
  224.     @Override
  225.     protected void finalize() throws Throwable {
  226.         try {
  227.             dispose();
  228.         } finally {
  229.             super.finalize();
  230.         }
  231.     }
  232.  
  233.     private Object getRGBPixelsMethodParam;
  234.     private int getRGBPixelsMethodType;
  235.     public final GraphicsDevice device;
  236.     private Method getRGBPixelsMethod;
  237.     private final RobotPeer peer;
  238.     private static boolean hasMouseInfoPeer;
  239.     private static MouseInfoPeer mouseInfoPeer;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment