/* * 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.Raster; 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; static Robot bot; static boolean freezeImage = false; private static void pressMouse() throws InterruptedException { bot.mousePress(InputEvent.BUTTON1_MASK); bot.mouseRelease(InputEvent.BUTTON1_MASK); } 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(); if (c.getName() == "redButton") { c.getToolkit().beep(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } freezeImage = !freezeImage; } } 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.setColor(pixel); g.fillRect(300,100,20,20); g.drawImage(bi,20, 50, this); } public static void main(String[] args) throws AWTException, InterruptedException { //bot.mouseMove(100, 100); bot = new Robot(); ScreenRecognition foo = new ScreenRecognition(); while (true) { PointerInfo pointer = MouseInfo.getPointerInfo(); mouse = pointer.getLocation(); pixel = bot.getPixelColor(mouse.x, mouse.y); try { if (!freezeImage) { bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround, mouse.y + mouseSurround)); bi = bi.getSubimage(mouse.x - mouseSurround, mouse.y - mouseSurround, mouseSurround * 2, mouseSurround * 2); /* See is enabled */ Color target = new Color(255, 170, 0); int pixelTreshold = 10; search: for (int x = 0; x < bi.getWidth(); x++) { for (int y = 0; y < bi.getHeight(); y++){ if (bi.getRGB(x, y) == target.getRGB()) { pixelTreshold--; if (pixelTreshold == 0) { break search; } } } } if (pixelTreshold == 0) { pressMouse(); System.out.println("Button Click"); } } foo.repaint(); } catch (IllegalArgumentException e) { continue; } catch (RasterFormatException e) { continue; } // Allow time to focus Firefox window Thread.sleep(100); } } }