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

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

First steps into proper AWT java. Using screen capture, cropping, colour detections and button action events.

File size: 2.5 KB
Line 
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;
13import java.awt.image.RasterFormatException;
14import java.io.IOException;
15
16
17public class ScreenRecognition extends Frame implements ActionListener {
18 static int mouseSurround = 120;
19 Image image;
20 static Color pixel;
21 static Point mouse;
22 static BufferedImage bi;
23 public ScreenRecognition() {
24 setTitle("Image demo Example");
25
26 Panel buttons = new Panel();
27 Button redButton = new Button("Red");
28 Button blueButton = new Button("Blue");
29 Button greenButton = new Button("Green");
30 redButton.setName("redButton");
31 redButton.addActionListener(this);
32 blueButton.setName("blueButton");
33 blueButton.addActionListener(this);
34 buttons.add(redButton);
35 buttons.add(blueButton);
36 buttons.add(greenButton);
37 add(buttons,"South");
38
39
40 setSize(400,300);
41 addWindowListener(new WindowAdapter() {
42 public void windowClosing(WindowEvent e){
43 System.exit(0);
44 }
45 });
46 setVisible(true);
47 }
48
49 public void actionPerformed(ActionEvent event) {
50 Component c = (Component)event.getSource();
51 System.out.println(c.getName());
52 c.getToolkit().beep();
53 }
54
55 public void paint(Graphics g) {
56 g.drawString("Mouse:" + mouse.x + "," + mouse.y ,20,40);
57 g.drawString("PixelRGB:" + pixel.toString() ,150,40);
58
59 g.drawRect(18, 48, mouseSurround * 2 + 4, mouseSurround * 2 + 4);
60 g.drawImage(bi,20, 50, this);
61 }
62
63 public static void main(String[] args) throws AWTException, InterruptedException {
64 //bot.mouseMove(100, 100);
65 Robot bot = new Robot();
66 ScreenRecognition foo = new ScreenRecognition();
67
68 while (true) {
69 PointerInfo pointer = MouseInfo.getPointerInfo();
70 mouse = pointer.getLocation();
71 pixel = bot.getPixelColor(mouse.x, mouse.y);
72 try {
73 bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround,
74 mouse.y + mouseSurround));
75 bi = bi.getSubimage(mouse.x - mouseSurround,
76 mouse.y - mouseSurround,
77 mouseSurround * 2, mouseSurround * 2);
78 foo.repaint();
79 } catch (IllegalArgumentException e) {
80 continue;
81 } catch (RasterFormatException e) {
82 continue;
83 }
84 // Allow time to focus Firefox window
85 Thread.sleep(100);
86 }
87
88 }
89}
Note: See TracBrowser for help on using the repository browser.