/* * Rick van der Zwet * License BSDLike http://rickvanderzwet.nl/LICENSE */ /* Based after http://ironbark.bendigo.latrobe.edu.au/~mary/DS/lectures/tutorials/ACSWorkshop98.html */ package nl.rickvanderzwet.farmville; import java.awt.*; import java.awt.event.*; import java.awt.geom.Point2D; import java.awt.image.BufferedImage; import java.awt.image.RasterFormatException; import java.io.IOException; public class ScreenRecognition extends Frame implements ActionListener { static int mouseSurround = 120; Image image; static Color pixel; static Point mouse; static BufferedImage bi; public ScreenRecognition() { setTitle("Image demo Example"); Panel buttons = new Panel(); Button redButton = new Button("Red"); Button blueButton = new Button("Blue"); Button greenButton = new Button("Green"); redButton.setName("redButton"); redButton.addActionListener(this); blueButton.setName("blueButton"); blueButton.addActionListener(this); buttons.add(redButton); buttons.add(blueButton); buttons.add(greenButton); add(buttons,"South"); setSize(400,300); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } }); setVisible(true); } public void actionPerformed(ActionEvent event) { Component c = (Component)event.getSource(); System.out.println(c.getName()); c.getToolkit().beep(); } public void paint(Graphics g) { g.drawString("Mouse:" + mouse.x + "," + mouse.y ,20,40); g.drawString("PixelRGB:" + pixel.toString() ,150,40); g.drawRect(18, 48, mouseSurround * 2 + 4, mouseSurround * 2 + 4); g.drawImage(bi,20, 50, this); } public static void main(String[] args) throws AWTException, InterruptedException { //bot.mouseMove(100, 100); Robot bot = new Robot(); ScreenRecognition foo = new ScreenRecognition(); while (true) { PointerInfo pointer = MouseInfo.getPointerInfo(); mouse = pointer.getLocation(); pixel = bot.getPixelColor(mouse.x, mouse.y); try { bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround, mouse.y + mouseSurround)); bi = bi.getSubimage(mouse.x - mouseSurround, mouse.y - mouseSurround, mouseSurround * 2, mouseSurround * 2); foo.repaint(); } catch (IllegalArgumentException e) { continue; } catch (RasterFormatException e) { continue; } // Allow time to focus Firefox window Thread.sleep(100); } } }