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

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

Move around more pretty, still very hacky

File size: 3.9 KB
RevLine 
[87]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;
[107]10import java.awt.Point;
[87]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 }
[110]25
26 /* Constants */
27 static Integer FARMSIZE_Y = 7;
28 static Integer FARMSIZE_X = 22;
29 static boolean mouseClickEnabled = false;
[87]30
[110]31
[87]32 private static Robot bot = null;
33 private static Random randomGenerator = null;
[107]34
[110]35 /* Parcel size in pixels */
36 static Integer XMOVE = 22;
37 static Integer YMOVE = 11;
[87]38
39 private static void pressMouse() throws InterruptedException {
[107]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 }
[87]46
47 }
48
[92]49 private static void moveMouse(Direction direction) throws InterruptedException {
[101]50 moveMouse(direction, 100);
[92]51 }
52
53 private static void moveMouse(Direction direction, Integer sleep_msec) throws InterruptedException {
54 /* Relocate to next field */
[107]55 Point mouse = MouseInfo.getPointerInfo().getLocation();
[92]56
[110]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
[107]63 Point newLocation = mouse;
[92]64 switch (direction) {
65 case UP:
[107]66 newLocation.x -= XMOVE;
67 newLocation.y -= YMOVE;
[92]68 break;
69 case DOWN:
[107]70 newLocation.x += XMOVE;
71 newLocation.y += YMOVE;
[92]72 break;
73 case LEFT:
[107]74 newLocation.x -= XMOVE;
75 newLocation.y += YMOVE;
[92]76 break;
77 case RIGHT:
[107]78 newLocation.x += XMOVE;
79 newLocation.y -= YMOVE;
[92]80 break;
81 }
[107]82 bot.mouseMove(newLocation.x, newLocation.y);
[92]83 Thread.sleep(sleep_msec);
[107]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
[92]91 }
92
[87]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;
[107]103 Point mouse = null;
[87]104 randomGenerator = new Random();
[92]105 // Allow time to focus Firefox window
[101]106 Thread.sleep(3000);
[87]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
[107]117 pressMouse();
[92]118
[110]119 Direction direction = Direction.RIGHT;
[92]120
[110]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++) {
[87]124 pointer = MouseInfo.getPointerInfo();
[107]125 mouse = pointer.getLocation();
126 System.out.println("x: " + mouse.x + ", y: " + mouse.y);
[87]127
128
[92]129 /* Too difficult, try larger parts */
[107]130 Color p = bot.getPixelColor(mouse.x, mouse.y);
[87]131 System.out.println("Pixel color R:" + p.getRed() + ",G:" +
132 p.getGreen() + ",B:" + p.getBlue());
133
[107]134 pressMouse();
[92]135 moveMouse(direction);
[87]136 }
[92]137
[110]138 moveMouse(Direction.DOWN);
[101]139 pressMouse();
[87]140
[110]141 /* Flip direction around */
142 direction = (direction == Direction.LEFT) ? Direction.RIGHT : Direction.LEFT;
143
[87]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}
[101]154
155
156
157
Note: See TracBrowser for help on using the repository browser.