/* * 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.image.BufferedImage; import java.awt.image.RasterFormatException; public class ScreenRecognition extends Frame implements ActionListener { /** * */ private static final long serialVersionUID = -6742715532913330783L; static int mouseSurround = 120; Image image; static Color pixel; static Point mouse; static BufferedImage bi; static Robot bot; static boolean freezeImage = false; static boolean mouseClickEnabled = true; private static void pressMouse() throws InterruptedException { if (mouseClickEnabled) { bot.mousePress(InputEvent.BUTTON1_MASK); bot.mouseRelease(InputEvent.BUTTON1_MASK); } } public ScreenRecognition() { setTitle("Screen Recognition Example"); Panel buttons = new Panel(); Button redButton = new Button("Red"); Button blueButton = new Button("Blue"); Button greenButton = new Button("Green"); redButton.setName("redButton"); redButton.setLabel("Freeze Screen"); redButton.addActionListener(this); blueButton.setName("blueButton"); blueButton.setLabel("Stop clicking"); 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; } else if (c.getName() == "blueButton") { mouseClickEnabled = !mouseClickEnabled; if (mouseClickEnabled) { ((Button) c).setLabel("Stop clicking"); } else { ((Button) c).setLabel("Start clicking"); } } } 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 at [" + mouse.x + "," + mouse.y + "]"); } } foo.repaint(); } catch (IllegalArgumentException e) { continue; } catch (RasterFormatException e) { continue; } // Allow time to focus Firefox window Thread.sleep(100); } } }