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

Last change on this file since 91 was 87, checked in by Rick van der Zwet, 15 years ago
File size: 2.8 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
29 private static void pressMouse() throws InterruptedException {
30 bot.mousePress(InputEvent.BUTTON1_MASK);
31 // Constant values are tricky, computers can only do that
32 Thread.sleep(500 + randomGenerator.nextInt(250));
33 bot.mouseRelease(InputEvent.BUTTON1_MASK);
34
35 }
36
37 /**
38 * @param args
39 * @throws InterruptedException
40 * @throws IOException
41 */
42 public static void main(String[] args) throws InterruptedException, IOException {
43 try {
44 //bot.mouseMove(100, 100);
45 bot = new Robot();
46 PointerInfo pointer = null;
47 randomGenerator = new Random();
48 // Allow time to focus firefox window
49 Thread.sleep(2000);
50
51 int x = 0 ,y = 0;
52 /*
53 bot.mouseMove(500, 500);
54 bot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
55 bot.mouseMove(700, 700);
56 bot.mouseRelease(InputEvent.BUTTON1_MASK);
57 */
58
59 Boolean press_mouse = false;
60 Integer farm_size_y = 4;
61 Integer farm_size_x = 19;
62 /* 0 = up, 1 = down, 2 = left, 3 = right */
63 Direction direction = Direction.UP;
64
65 if (press_mouse) {
66 pressMouse();
67 }
68 // Save the mouse button :-)
69 for (int i = 0; i < farm_size_y; i++) {
70 for (int j = 0; j < farm_size_x; j++) {
71 pointer = MouseInfo.getPointerInfo();
72
73 x = pointer.getLocation().x;
74 y = pointer.getLocation().y;
75 System.out.println("x: " + x + ", y: " + y);
76
77
78 /* Too difficult to diffentiate, try larger parts */
79 Color p = bot.getPixelColor(x, y);
80 System.out.println("Pixel color R:" + p.getRed() + ",G:" +
81 p.getGreen() + ",B:" + p.getBlue());
82
83 /* Relocate to next field */
84 switch (direction) {
85 case UP:
86 bot.mouseMove(x - 24, y - 12);
87 break;
88 case DOWN:
89 bot.mouseMove(x + 24, y + 12);
90 break;
91 case LEFT:
92 bot.mouseMove(x - 24, y + 12);
93 break;
94 case RIGHT:
95 bot.mouseMove(x + 24, y - 12);
96 break;
97 }
98
99 Thread.sleep(100);
100
101 if (press_mouse) {
102 pressMouse();
103 }
104 }
105
106 }
107 } catch (AWTException e) {
108 // TODO Auto-generated catch block
109 e.printStackTrace();
110 }
111
112 // TODO Auto-generated method stub
113
114 }
115
116
117
118}
Note: See TracBrowser for help on using the repository browser.