1 | /*
|
---|
2 | * Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
3 | * License BSDLike http://rickvanderzwet.nl/LICENSE
|
---|
4 | */
|
---|
5 |
|
---|
6 | /* Based after http://ironbark.bendigo.latrobe.edu.au/~mary/DS/lectures/tutorials/ACSWorkshop98.html */
|
---|
7 | package nl.rickvanderzwet.farmville;
|
---|
8 |
|
---|
9 | import java.awt.*;
|
---|
10 | import java.awt.event.*;
|
---|
11 | import java.awt.image.BufferedImage;
|
---|
12 | import java.awt.image.RasterFormatException;
|
---|
13 |
|
---|
14 |
|
---|
15 | public class ScreenRecognition extends Frame implements ActionListener {
|
---|
16 | /**
|
---|
17 | *
|
---|
18 | */
|
---|
19 | private static final long serialVersionUID = -6742715532913330783L;
|
---|
20 | static int mouseSurround = 120;
|
---|
21 | Image image;
|
---|
22 | static Color pixel;
|
---|
23 | static Point mouse;
|
---|
24 | static BufferedImage bi;
|
---|
25 | static Robot bot;
|
---|
26 |
|
---|
27 | static boolean freezeImage = false;
|
---|
28 | static boolean mouseClickEnabled = true;
|
---|
29 |
|
---|
30 | private static void pressMouse() throws InterruptedException {
|
---|
31 | if (mouseClickEnabled) {
|
---|
32 | bot.mousePress(InputEvent.BUTTON1_MASK);
|
---|
33 | bot.mouseRelease(InputEvent.BUTTON1_MASK);
|
---|
34 | }
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 | public ScreenRecognition() {
|
---|
39 | setTitle("Screen Recognition Example");
|
---|
40 |
|
---|
41 | Panel buttons = new Panel();
|
---|
42 | Button redButton = new Button("Red");
|
---|
43 | Button blueButton = new Button("Blue");
|
---|
44 | Button greenButton = new Button("Green");
|
---|
45 | redButton.setName("redButton");
|
---|
46 | redButton.setLabel("Freeze Screen");
|
---|
47 | redButton.addActionListener(this);
|
---|
48 | blueButton.setName("blueButton");
|
---|
49 | blueButton.setLabel("Stop clicking");
|
---|
50 | blueButton.addActionListener(this);
|
---|
51 | buttons.add(redButton);
|
---|
52 | buttons.add(blueButton);
|
---|
53 | buttons.add(greenButton);
|
---|
54 | add(buttons,"South");
|
---|
55 |
|
---|
56 |
|
---|
57 | setSize(400,300);
|
---|
58 | addWindowListener(new WindowAdapter() {
|
---|
59 | public void windowClosing(WindowEvent e){
|
---|
60 | System.exit(0);
|
---|
61 | }
|
---|
62 | });
|
---|
63 | setVisible(true);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void actionPerformed(ActionEvent event) {
|
---|
67 | Component c = (Component)event.getSource();
|
---|
68 | if (c.getName() == "redButton") {
|
---|
69 | c.getToolkit().beep();
|
---|
70 | try {
|
---|
71 | Thread.sleep(3000);
|
---|
72 | } catch (InterruptedException e) {
|
---|
73 | // TODO Auto-generated catch block
|
---|
74 | e.printStackTrace();
|
---|
75 | }
|
---|
76 | freezeImage = !freezeImage;
|
---|
77 | } else if (c.getName() == "blueButton") {
|
---|
78 | mouseClickEnabled = !mouseClickEnabled;
|
---|
79 | if (mouseClickEnabled) {
|
---|
80 | ((Button) c).setLabel("Stop clicking");
|
---|
81 | } else {
|
---|
82 | ((Button) c).setLabel("Start clicking");
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void paint(Graphics g) {
|
---|
88 | g.drawString("Mouse:" + mouse.x + "," + mouse.y ,20,40);
|
---|
89 | g.drawString("PixelRGB:" + pixel.toString() ,150,40);
|
---|
90 |
|
---|
91 | g.drawRect(18, 48, mouseSurround * 2 + 4, mouseSurround * 2 + 4);
|
---|
92 | g.setColor(pixel);
|
---|
93 | g.fillRect(300,100,20,20);
|
---|
94 | g.drawImage(bi,20, 50, this);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public static void main(String[] args) throws AWTException, InterruptedException {
|
---|
98 | //bot.mouseMove(100, 100);
|
---|
99 | bot = new Robot();
|
---|
100 | ScreenRecognition foo = new ScreenRecognition();
|
---|
101 |
|
---|
102 | while (true) {
|
---|
103 | PointerInfo pointer = MouseInfo.getPointerInfo();
|
---|
104 | mouse = pointer.getLocation();
|
---|
105 | pixel = bot.getPixelColor(mouse.x, mouse.y);
|
---|
106 | try {
|
---|
107 | if (!freezeImage) {
|
---|
108 | bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround,
|
---|
109 | mouse.y + mouseSurround));
|
---|
110 | bi = bi.getSubimage(mouse.x - mouseSurround,
|
---|
111 | mouse.y - mouseSurround,
|
---|
112 | mouseSurround * 2, mouseSurround * 2);
|
---|
113 |
|
---|
114 | /* See is enabled */
|
---|
115 | Color target = new Color(255, 170, 0);
|
---|
116 | int pixelTreshold = 10;
|
---|
117 |
|
---|
118 | search: for (int x = 0; x < bi.getWidth(); x++) {
|
---|
119 | for (int y = 0; y < bi.getHeight(); y++){
|
---|
120 | if (bi.getRGB(x, y) == target.getRGB()) {
|
---|
121 | pixelTreshold--;
|
---|
122 | if (pixelTreshold == 0) {
|
---|
123 | break search;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 | if (pixelTreshold == 0) {
|
---|
129 | pressMouse();
|
---|
130 | System.out.println("Button Click at [" + mouse.x + "," + mouse.y + "]");
|
---|
131 | }
|
---|
132 |
|
---|
133 | }
|
---|
134 | foo.repaint();
|
---|
135 | } catch (IllegalArgumentException e) {
|
---|
136 | continue;
|
---|
137 | } catch (RasterFormatException e) {
|
---|
138 | continue;
|
---|
139 | }
|
---|
140 | // Allow time to focus Firefox window
|
---|
141 | Thread.sleep(100);
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 | }
|
---|