Index: java-automator/src/nl/rickvanderzwet/farmville/ScreenRecognition.java
===================================================================
--- java-automator/src/nl/rickvanderzwet/farmville/ScreenRecognition.java	(revision 111)
+++ java-automator/src/nl/rickvanderzwet/farmville/ScreenRecognition.java	(revision 112)
@@ -3,4 +3,5 @@
  * License BSDLike http://rickvanderzwet.nl/LICENSE
  * XXX: Do not look at this code, it's dirty and _NOT_ the way Java should be written.
+ * XXX: Dragging sucks needs to be be around
  */
 
@@ -10,8 +11,7 @@
 import java.awt.event.*;
 import java.awt.image.BufferedImage;
+import java.awt.image.IndexColorModel;
 import java.awt.image.RasterFormatException;
 
-import javax.swing.BorderFactory;
-import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
@@ -40,4 +40,6 @@
 	static Point mouse;
 	static BufferedImage bi;
+	static BufferedImage rawPattern;
+	static BufferedImage pattern;
 	static Robot bot;
 
@@ -49,6 +51,9 @@
 	static Button redButton;
 	static Button greenButton;
-
-	Point boxLeftTop, boxRightBottom = new Point (0,0);
+	static Button sampleShotButton;
+
+
+	static Point boxLeftTop = new Point (0,0);
+	static Point boxRightBottom = new Point (0,0);
 	Boolean drawBox = false;
 	static Boolean draggingBox = false;
@@ -102,7 +107,13 @@
 		blueButton.setLabel("Stop clicking");
 		blueButton.addActionListener(this);
+		
+		sampleShotButton = new Button("sampleShot");
+		sampleShotButton.setName("sampleShot");
+		sampleShotButton.setLabel("Sample shot");
+		sampleShotButton.addActionListener(this);
 		buttons.add(redButton);
 		buttons.add(blueButton);
 		buttons.add(greenButton);
+		buttons.add(sampleShotButton);
 		
 
@@ -111,5 +122,5 @@
 
 			
-		setSize(400,300);
+		setSize(600,300);
 		addWindowListener(new WindowAdapter() {
 			public void windowClosing(WindowEvent e){ 
@@ -124,6 +135,6 @@
 
 	public void actionPerformed(ActionEvent event) {
-		System.out.println("ActionPerformed");
 		Component c = (Component)event.getSource();
+		System.out.println("ActionPerformed: " + c.getName());
 		if (c.getName() == "redButton") {
 			if (freezeImage) {
@@ -141,5 +152,5 @@
 					}
 				};
-				Timer foo = new Timer(3000, freezeScreen);
+				Timer foo = new Timer(500, freezeScreen);
 				foo.setRepeats(false);
 				foo.start();
@@ -153,7 +164,23 @@
 				((Button) c).setLabel("Start clicking");	
 			}
+		} else if (c.getName() == "sampleShot") {
+			System.out.println("Point to use: " + boxLeftTop + boxRightBottom);
+			
+			/* Make sure we can select both ways */
+			Integer xStart = (boxLeftTop.x < boxRightBottom.x) ? boxLeftTop.x : boxRightBottom.x;
+			Integer yStart = (boxLeftTop.y < boxRightBottom.y) ? boxLeftTop.y : boxRightBottom.y;
+			
+			Integer xWidth = Math.abs(boxRightBottom.x - boxLeftTop.x);
+			Integer yWidth = Math.abs(boxRightBottom.y - boxLeftTop.y);
+			//pattern = bi.getSubimage(xStart - 20 , yStart - 50, xWidth, yWidth);
+			rawPattern = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB);		
+			bi.copyData(rawPattern.getRaster());
+			xStart -= 20;
+			yStart -= 50;
+			System.out.println("Values used: " + xStart + " ," + yStart + ", " + xWidth + "," +yWidth);
+			pattern = rawPattern.getSubimage(xStart , yStart, xWidth, yWidth);
 		}
 	}
-
+	
 	public void paint(Graphics g) {
 		super.paint(g);
@@ -166,12 +193,19 @@
 		
 		g.drawImage(bi,20, 50, this);
+		g.drawImage(pattern, 320, 50, this);
 
 		if (drawBox){
 			g.setColor(Color.RED);
-			g.drawRect(boxLeftTop.x, boxLeftTop.y, Math.abs(boxRightBottom.x - boxLeftTop.x), Math.abs(boxRightBottom.y - boxLeftTop.y));
+			
+			/* Make sure we can select both ways */
+			Integer xStart = (boxLeftTop.x < boxRightBottom.x) ? boxLeftTop.x : boxRightBottom.x;
+			Integer yStart = (boxLeftTop.y < boxRightBottom.y) ? boxLeftTop.y : boxRightBottom.y;
+			
+			Integer xWidth = Math.abs(boxRightBottom.x - boxLeftTop.x);
+			Integer yWidth = Math.abs(boxRightBottom.y - boxLeftTop.y);
+			g.drawRect(xStart, yStart, xWidth, yWidth);
 		}
 		
 		greenButton.setEnabled(foundPlowLocation);
-		
 		bi.createGraphics();
 		
@@ -192,9 +226,13 @@
 					System.out.println("Location mouse changed " + mouse + "(was: " + prevLocation + ")");
 					prevLocation = mouse;
+					
+					/* XXX: Make sure we do not cross the boundaries */
+					
 					bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround,
 							mouse.y + mouseSurround));
-					bi = bi.getSubimage(mouse.x - mouseSurround, 
-							mouse.y - mouseSurround, 
-							mouseSurround * 2, mouseSurround * 2);
+
+					Integer xBase = ((mouse.x - mouseSurround) < 0) ? mouseSurround : (mouse.x - mouseSurround);
+					Integer yBase = ((mouse.y - mouseSurround) < 0) ? mouseSurround : (mouse.y - mouseSurround);
+					bi = bi.getSubimage(xBase, yBase, mouseSurround * 2, mouseSurround * 2);
 
 					/* See is enabled */
@@ -242,7 +280,35 @@
 					}
 
-					/* Do basic pattern recognition */
+					Boolean patternMatch = false;
+					Point patLoc = new Point(0,0);
+
+					if (pattern != null) {
+						/* Do basic pattern recognition 
+						 * XXX: Does not scale, does not support multiple objects
+						 * XXX: Maybe colour will help us over here as well
+						 */
+						nextx: for (int x = 0; x < bi.getWidth() - pattern.getWidth(); x++) {
+							nexty: for (int y = 0; y < bi.getHeight() - pattern.getHeight(); y++) {
+								for (int i = 0; i < pattern.getWidth(); i++) {
+									for (int j = 0; j < pattern.getHeight(); j++) {
+										if (bi.getRGB(x + i, y + j) != pattern.getRGB(i, j)) {
+											//System.out.println("Fail at (" + x + "," + y + ") [" + 
+											//		patLoc.x + "," + patLoc.y + "] " + 
+											//		bi.getRGB(x + i, y + j) + "/" + pattern.getRGB(i, j));
+											continue nexty;
+										}
+									}
+								}
+								/* We are successful */
+								patternMatch = true;
+								break nextx;
+							}
+						}
+					}
 					
-
+					if(patternMatch) {
+						System.out.println("Match found at location: " + patLoc);
+					}
+					
 					if (pixelTreshold == 0) {
 						pressMouse();
