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.geom.Point2D;
|
---|
12 | import java.awt.image.BufferedImage;
|
---|
13 | import java.awt.image.Raster;
|
---|
14 | import java.awt.image.RasterFormatException;
|
---|
15 | import java.io.IOException;
|
---|
16 |
|
---|
17 |
|
---|
18 | public class ScreenRecognition extends Frame implements ActionListener {
|
---|
19 | static int mouseSurround = 120;
|
---|
20 | Image image;
|
---|
21 | static Color pixel;
|
---|
22 | static Point mouse;
|
---|
23 | static BufferedImage bi;
|
---|
24 | static Robot bot;
|
---|
25 |
|
---|
26 | static boolean freezeImage = false;
|
---|
27 |
|
---|
28 | private static void pressMouse() throws InterruptedException {
|
---|
29 | bot.mousePress(InputEvent.BUTTON1_MASK);
|
---|
30 | bot.mouseRelease(InputEvent.BUTTON1_MASK);
|
---|
31 |
|
---|
32 | }
|
---|
33 |
|
---|
34 | public ScreenRecognition() {
|
---|
35 | setTitle("Image demo Example");
|
---|
36 |
|
---|
37 | Panel buttons = new Panel();
|
---|
38 | Button redButton = new Button("Red");
|
---|
39 | Button blueButton = new Button("Blue");
|
---|
40 | Button greenButton = new Button("Green");
|
---|
41 | redButton.setName("redButton");
|
---|
42 | redButton.addActionListener(this);
|
---|
43 | blueButton.setName("blueButton");
|
---|
44 | blueButton.addActionListener(this);
|
---|
45 | buttons.add(redButton);
|
---|
46 | buttons.add(blueButton);
|
---|
47 | buttons.add(greenButton);
|
---|
48 | add(buttons,"South");
|
---|
49 |
|
---|
50 |
|
---|
51 | setSize(400,300);
|
---|
52 | addWindowListener(new WindowAdapter() {
|
---|
53 | public void windowClosing(WindowEvent e){
|
---|
54 | System.exit(0);
|
---|
55 | }
|
---|
56 | });
|
---|
57 | setVisible(true);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void actionPerformed(ActionEvent event) {
|
---|
61 | Component c = (Component)event.getSource();
|
---|
62 | if (c.getName() == "redButton") {
|
---|
63 | c.getToolkit().beep();
|
---|
64 | try {
|
---|
65 | Thread.sleep(3000);
|
---|
66 | } catch (InterruptedException e) {
|
---|
67 | // TODO Auto-generated catch block
|
---|
68 | e.printStackTrace();
|
---|
69 | }
|
---|
70 | freezeImage = !freezeImage;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void paint(Graphics g) {
|
---|
75 | g.drawString("Mouse:" + mouse.x + "," + mouse.y ,20,40);
|
---|
76 | g.drawString("PixelRGB:" + pixel.toString() ,150,40);
|
---|
77 |
|
---|
78 | g.drawRect(18, 48, mouseSurround * 2 + 4, mouseSurround * 2 + 4);
|
---|
79 | g.setColor(pixel);
|
---|
80 | g.fillRect(300,100,20,20);
|
---|
81 | g.drawImage(bi,20, 50, this);
|
---|
82 | }
|
---|
83 |
|
---|
84 | public static void main(String[] args) throws AWTException, InterruptedException {
|
---|
85 | //bot.mouseMove(100, 100);
|
---|
86 | bot = new Robot();
|
---|
87 | ScreenRecognition foo = new ScreenRecognition();
|
---|
88 |
|
---|
89 | while (true) {
|
---|
90 | PointerInfo pointer = MouseInfo.getPointerInfo();
|
---|
91 | mouse = pointer.getLocation();
|
---|
92 | pixel = bot.getPixelColor(mouse.x, mouse.y);
|
---|
93 | try {
|
---|
94 | if (!freezeImage) {
|
---|
95 | bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround,
|
---|
96 | mouse.y + mouseSurround));
|
---|
97 | bi = bi.getSubimage(mouse.x - mouseSurround,
|
---|
98 | mouse.y - mouseSurround,
|
---|
99 | mouseSurround * 2, mouseSurround * 2);
|
---|
100 |
|
---|
101 | /* See is enabled */
|
---|
102 | Color target = new Color(255, 170, 0);
|
---|
103 | int pixelTreshold = 10;
|
---|
104 |
|
---|
105 | search: for (int x = 0; x < bi.getWidth(); x++) {
|
---|
106 | for (int y = 0; y < bi.getHeight(); y++){
|
---|
107 | if (bi.getRGB(x, y) == target.getRGB()) {
|
---|
108 | pixelTreshold--;
|
---|
109 | if (pixelTreshold == 0) {
|
---|
110 | break search;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | if (pixelTreshold == 0) {
|
---|
116 | pressMouse();
|
---|
117 | System.out.println("Button Click");
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|
121 | foo.repaint();
|
---|
122 | } catch (IllegalArgumentException e) {
|
---|
123 | continue;
|
---|
124 | } catch (RasterFormatException e) {
|
---|
125 | continue;
|
---|
126 | }
|
---|
127 | // Allow time to focus Firefox window
|
---|
128 | Thread.sleep(100);
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|
132 | }
|
---|