source: java-automator/src/nl/rickvanderzwet/farmville/ScreenRecognition.java@ 105

Last change on this file since 105 was 104, checked in by Rick van der Zwet, 15 years ago
  • First pixel recognition tester
  • Action based on colour state change
File size: 3.5 KB
RevLine 
[103]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 */
7package nl.rickvanderzwet.farmville;
8
9import java.awt.*;
10import java.awt.event.*;
11import java.awt.geom.Point2D;
12import java.awt.image.BufferedImage;
[104]13import java.awt.image.Raster;
[103]14import java.awt.image.RasterFormatException;
15import java.io.IOException;
16
17
18public 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;
[104]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
[103]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();
[104]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 }
[103]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);
[104]79 g.setColor(pixel);
80 g.fillRect(300,100,20,20);
[103]81 g.drawImage(bi,20, 50, this);
82 }
83
84 public static void main(String[] args) throws AWTException, InterruptedException {
85 //bot.mouseMove(100, 100);
[104]86 bot = new Robot();
[103]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 {
[104]94 if (!freezeImage) {
[103]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);
[104]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 }
[103]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}
Note: See TracBrowser for help on using the repository browser.