Changeset 107 for java-automator/src/nl
- Timestamp:
- Apr 13, 2010, 8:02:46 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java-automator/src/nl/rickvanderzwet/farmville/Automator.java
r101 r107 8 8 import java.awt.AWTException; 9 9 import java.awt.Color; 10 import java.awt.Point; 10 11 import java.awt.PointerInfo; 11 12 import java.awt.Robot; … … 25 26 private static Robot bot = null; 26 27 private static Random randomGenerator = null; 28 static boolean mouseClickEnabled = false; 29 27 30 28 31 private static void pressMouse() throws InterruptedException { 29 bot.mousePress(InputEvent.BUTTON1_MASK); 30 // Constant values are tricky, computers can only do that 31 Thread.sleep(100 + randomGenerator.nextInt(250)); 32 bot.mouseRelease(InputEvent.BUTTON1_MASK); 32 if (mouseClickEnabled) { 33 bot.mousePress(InputEvent.BUTTON1_MASK); 34 // Constant values are tricky, computers can only do that 35 Thread.sleep(100 + randomGenerator.nextInt(250)); 36 bot.mouseRelease(InputEvent.BUTTON1_MASK); 37 } 33 38 34 39 } … … 40 45 private static void moveMouse(Direction direction, Integer sleep_msec) throws InterruptedException { 41 46 /* Relocate to next field */ 42 PointerInfo pointer = MouseInfo.getPointerInfo(); 43 44 Integer XMOVE = 24; 47 Point mouse = MouseInfo.getPointerInfo().getLocation(); 48 Integer XMOVE = 22; 45 49 Integer YMOVE = 11; 46 50 47 Integer x = pointer.getLocation().x; 48 Integer y = pointer.getLocation().y; 49 51 Point newLocation = mouse; 50 52 switch (direction) { 51 53 case UP: 52 bot.mouseMove(x - XMOVE, y - YMOVE); 54 newLocation.x -= XMOVE; 55 newLocation.y -= YMOVE; 53 56 break; 54 57 case DOWN: 55 bot.mouseMove(x + XMOVE, y + YMOVE); 58 newLocation.x += XMOVE; 59 newLocation.y += YMOVE; 56 60 break; 57 61 case LEFT: 58 bot.mouseMove(x - XMOVE, y + YMOVE); 62 newLocation.x -= XMOVE; 63 newLocation.y += YMOVE; 59 64 break; 60 65 case RIGHT: 61 bot.mouseMove(x + XMOVE, y - YMOVE); 66 newLocation.x += XMOVE; 67 newLocation.y -= YMOVE; 62 68 break; 63 69 } 70 bot.mouseMove(newLocation.x, newLocation.y); 64 71 Thread.sleep(sleep_msec); 72 73 mouse = MouseInfo.getPointerInfo().getLocation(); 74 if (newLocation.x != mouse.x || newLocation.y != mouse.y) { 75 System.out.println("Forced exit, mouse moved externally"); 76 System.exit(1); 77 } 78 65 79 } 66 80 … … 75 89 bot = new Robot(); 76 90 PointerInfo pointer = null; 91 Point mouse = null; 77 92 randomGenerator = new Random(); 78 93 // Allow time to focus Firefox window 79 94 Thread.sleep(3000); 80 95 81 int x = 0 ,y = 0;82 96 /* 83 97 bot.mouseMove(500, 500); … … 88 102 89 103 /* Constants */ 90 Boolean press_mouse = true; 91 Integer farm_size_y = 9; 104 Integer farm_size_y = 1; 92 105 Integer farm_size_x = 22; 93 106 /* 0 = up, 1 = down, 2 = left, 3 = right */ 94 107 95 if (press_mouse) { 96 pressMouse(); 97 } 108 pressMouse(); 98 109 99 110 Direction direction = Direction.UP; … … 103 114 for (int j = 0; j < farm_size_x; j++) { 104 115 pointer = MouseInfo.getPointerInfo(); 105 106 x = pointer.getLocation().x; 107 y = pointer.getLocation().y; 108 System.out.println("x: " + x + ", y: " + y); 116 mouse = pointer.getLocation(); 117 System.out.println("x: " + mouse.x + ", y: " + mouse.y); 109 118 110 119 111 120 /* Too difficult, try larger parts */ 112 Color p = bot.getPixelColor( x,y);121 Color p = bot.getPixelColor(mouse.x, mouse.y); 113 122 System.out.println("Pixel color R:" + p.getRed() + ",G:" + 114 123 p.getGreen() + ",B:" + p.getBlue()); 115 124 116 if (press_mouse) { 117 pressMouse(); 118 } 125 pressMouse(); 119 126 moveMouse(direction); 120 127 } … … 137 144 138 145 } 139 140 141 142 146 } 143 147
Note:
See TracChangeset
for help on using the changeset viewer.