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

Last change on this file since 257 was 110, checked in by Rick van der Zwet, 15 years ago

Move around more pretty, still very hacky

File size: 3.9 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 /* Constants */
27 static Integer FARMSIZE_Y = 7;
28 static Integer FARMSIZE_X = 22;
29 static boolean mouseClickEnabled = false;
30
31
32 private static Robot bot = null;
33 private static Random randomGenerator = null;
34
35 /* Parcel size in pixels */
36 static Integer XMOVE = 22;
37 static Integer YMOVE = 11;
38
39 private static void pressMouse() throws InterruptedException {
40 if (mouseClickEnabled) {
41 bot.mousePress(InputEvent.BUTTON1_MASK);
42 // Constant values are tricky, computers can only do that
43 Thread.sleep(100 + randomGenerator.nextInt(250));
44 bot.mouseRelease(InputEvent.BUTTON1_MASK);
45 }
46
47 }
48
49 private static void moveMouse(Direction direction) throws InterruptedException {
50 moveMouse(direction, 100);
51 }
52
53 private static void moveMouse(Direction direction, Integer sleep_msec) throws InterruptedException {
54 /* Relocate to next field */
55 Point mouse = MouseInfo.getPointerInfo().getLocation();
56
57 /* Grumble half pixels moves are not possible, this will hack around it */
58 XMOVE = (XMOVE == 22) ? XMOVE-- : XMOVE++;
59 YMOVE = (YMOVE == 11) ? YMOVE-- : YMOVE++;
60
61
62
63 Point newLocation = mouse;
64 switch (direction) {
65 case UP:
66 newLocation.x -= XMOVE;
67 newLocation.y -= YMOVE;
68 break;
69 case DOWN:
70 newLocation.x += XMOVE;
71 newLocation.y += YMOVE;
72 break;
73 case LEFT:
74 newLocation.x -= XMOVE;
75 newLocation.y += YMOVE;
76 break;
77 case RIGHT:
78 newLocation.x += XMOVE;
79 newLocation.y -= YMOVE;
80 break;
81 }
82 bot.mouseMove(newLocation.x, newLocation.y);
83 Thread.sleep(sleep_msec);
84
85 mouse = MouseInfo.getPointerInfo().getLocation();
86 if (newLocation.x != mouse.x || newLocation.y != mouse.y) {
87 System.out.println("Forced exit, mouse moved externally");
88 System.exit(1);
89 }
90
91 }
92
93 /**
94 * @param args
95 * @throws InterruptedException
96 * @throws IOException
97 */
98 public static void main(String[] args) throws InterruptedException, IOException {
99 try {
100 //bot.mouseMove(100, 100);
101 bot = new Robot();
102 PointerInfo pointer = null;
103 Point mouse = null;
104 randomGenerator = new Random();
105 // Allow time to focus Firefox window
106 Thread.sleep(3000);
107
108 /*
109 bot.mouseMove(500, 500);
110 bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
111 bot.mouseMove(700, 700);
112 bot.mouseRelease(InputEvent.BUTTON1_MASK);
113 */
114
115 /* 0 = up, 1 = down, 2 = left, 3 = right */
116
117 pressMouse();
118
119 Direction direction = Direction.RIGHT;
120
121 /* Move around naturally, from top left, to bottom right */
122 for (int i = 0; i < FARMSIZE_Y; i++) {
123 for (int j = 0; j < FARMSIZE_X; j++) {
124 pointer = MouseInfo.getPointerInfo();
125 mouse = pointer.getLocation();
126 System.out.println("x: " + mouse.x + ", y: " + mouse.y);
127
128
129 /* Too difficult, try larger parts */
130 Color p = bot.getPixelColor(mouse.x, mouse.y);
131 System.out.println("Pixel color R:" + p.getRed() + ",G:" +
132 p.getGreen() + ",B:" + p.getBlue());
133
134 pressMouse();
135 moveMouse(direction);
136 }
137
138 moveMouse(Direction.DOWN);
139 pressMouse();
140
141 /* Flip direction around */
142 direction = (direction == Direction.LEFT) ? Direction.RIGHT : Direction.LEFT;
143
144 }
145 } catch (AWTException e) {
146 // TODO Auto-generated catch block
147 e.printStackTrace();
148 }
149
150 // TODO Auto-generated method stub
151
152 }
153}
154
155
156
157
Note: See TracBrowser for help on using the repository browser.