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

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

Some random hacks

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