source: java-automator/src/nl/rickvanderzwet/farmville/Automator.java@ 109

Last change on this file since 109 was 107, checked in by Rick van der Zwet, 15 years ago
  • Failsave switch for mouse moving
  • Clicking optional
File size: 3.7 KB
Line 
1/*
2 * Rick van der Zwet <info@rickvanderzwet.nl>
3 * License BSDLike http://rickvanderzwet.nl/LICENSE
4 */
5
6package nl.rickvanderzwet.farmville;
7
8import java.awt.AWTException;
9import java.awt.Color;
10import java.awt.Point;
11import java.awt.PointerInfo;
12import java.awt.Robot;
13import java.awt.event.InputEvent;
14import java.util.Random;
15import java.awt.MouseInfo;
16import java.io.IOException;
17
18
19
20public class Automator {
21
22 public enum Direction {
23 UP, DOWN, LEFT, RIGHT
24 }
25
26 private static Robot bot = null;
27 private static Random randomGenerator = null;
28 static boolean mouseClickEnabled = false;
29
30
31 private static void pressMouse() throws InterruptedException {
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 }
38
39 }
40
41 private static void moveMouse(Direction direction) throws InterruptedException {
42 moveMouse(direction, 100);
43 }
44
45 private static void moveMouse(Direction direction, Integer sleep_msec) throws InterruptedException {
46 /* Relocate to next field */
47 Point mouse = MouseInfo.getPointerInfo().getLocation();
48 Integer XMOVE = 22;
49 Integer YMOVE = 11;
50
51 Point newLocation = mouse;
52 switch (direction) {
53 case UP:
54 newLocation.x -= XMOVE;
55 newLocation.y -= YMOVE;
56 break;
57 case DOWN:
58 newLocation.x += XMOVE;
59 newLocation.y += YMOVE;
60 break;
61 case LEFT:
62 newLocation.x -= XMOVE;
63 newLocation.y += YMOVE;
64 break;
65 case RIGHT:
66 newLocation.x += XMOVE;
67 newLocation.y -= YMOVE;
68 break;
69 }
70 bot.mouseMove(newLocation.x, newLocation.y);
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
79 }
80
81 /**
82 * @param args
83 * @throws InterruptedException
84 * @throws IOException
85 */
86 public static void main(String[] args) throws InterruptedException, IOException {
87 try {
88 //bot.mouseMove(100, 100);
89 bot = new Robot();
90 PointerInfo pointer = null;
91 Point mouse = null;
92 randomGenerator = new Random();
93 // Allow time to focus Firefox window
94 Thread.sleep(3000);
95
96 /*
97 bot.mouseMove(500, 500);
98 bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
99 bot.mouseMove(700, 700);
100 bot.mouseRelease(InputEvent.BUTTON1_MASK);
101 */
102
103 /* Constants */
104 Integer farm_size_y = 1;
105 Integer farm_size_x = 22;
106 /* 0 = up, 1 = down, 2 = left, 3 = right */
107
108 pressMouse();
109
110 Direction direction = Direction.UP;
111
112 // Save the mouse button :-)
113 for (int i = 0; i < farm_size_y; i++) {
114 for (int j = 0; j < farm_size_x; j++) {
115 pointer = MouseInfo.getPointerInfo();
116 mouse = pointer.getLocation();
117 System.out.println("x: " + mouse.x + ", y: " + mouse.y);
118
119
120 /* Too difficult, try larger parts */
121 Color p = bot.getPixelColor(mouse.x, mouse.y);
122 System.out.println("Pixel color R:" + p.getRed() + ",G:" +
123 p.getGreen() + ",B:" + p.getBlue());
124
125 pressMouse();
126 moveMouse(direction);
127 }
128
129 moveMouse(Direction.LEFT);
130 pressMouse();
131
132 if (direction == Direction.UP) {
133 direction = Direction.DOWN;
134 } else {
135 direction = Direction.UP;
136 }
137 }
138 } catch (AWTException e) {
139 // TODO Auto-generated catch block
140 e.printStackTrace();
141 }
142
143 // TODO Auto-generated method stub
144
145 }
146}
147
148
149
150
Note: See TracBrowser for help on using the repository browser.