Changeset 112 for java-automator/src
- Timestamp:
- Apr 15, 2010, 9:52:08 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java-automator/src/nl/rickvanderzwet/farmville/ScreenRecognition.java
r111 r112 3 3 * License BSDLike http://rickvanderzwet.nl/LICENSE 4 4 * XXX: Do not look at this code, it's dirty and _NOT_ the way Java should be written. 5 * XXX: Dragging sucks needs to be be around 5 6 */ 6 7 … … 10 11 import java.awt.event.*; 11 12 import java.awt.image.BufferedImage; 13 import java.awt.image.IndexColorModel; 12 14 import java.awt.image.RasterFormatException; 13 15 14 import javax.swing.BorderFactory;15 import javax.swing.JButton;16 16 import javax.swing.JFrame; 17 17 import javax.swing.JLabel; … … 40 40 static Point mouse; 41 41 static BufferedImage bi; 42 static BufferedImage rawPattern; 43 static BufferedImage pattern; 42 44 static Robot bot; 43 45 … … 49 51 static Button redButton; 50 52 static Button greenButton; 51 52 Point boxLeftTop, boxRightBottom = new Point (0,0); 53 static Button sampleShotButton; 54 55 56 static Point boxLeftTop = new Point (0,0); 57 static Point boxRightBottom = new Point (0,0); 53 58 Boolean drawBox = false; 54 59 static Boolean draggingBox = false; … … 102 107 blueButton.setLabel("Stop clicking"); 103 108 blueButton.addActionListener(this); 109 110 sampleShotButton = new Button("sampleShot"); 111 sampleShotButton.setName("sampleShot"); 112 sampleShotButton.setLabel("Sample shot"); 113 sampleShotButton.addActionListener(this); 104 114 buttons.add(redButton); 105 115 buttons.add(blueButton); 106 116 buttons.add(greenButton); 117 buttons.add(sampleShotButton); 107 118 108 119 … … 111 122 112 123 113 setSize( 400,300);124 setSize(600,300); 114 125 addWindowListener(new WindowAdapter() { 115 126 public void windowClosing(WindowEvent e){ … … 124 135 125 136 public void actionPerformed(ActionEvent event) { 126 System.out.println("ActionPerformed");127 137 Component c = (Component)event.getSource(); 138 System.out.println("ActionPerformed: " + c.getName()); 128 139 if (c.getName() == "redButton") { 129 140 if (freezeImage) { … … 141 152 } 142 153 }; 143 Timer foo = new Timer( 3000, freezeScreen);154 Timer foo = new Timer(500, freezeScreen); 144 155 foo.setRepeats(false); 145 156 foo.start(); … … 153 164 ((Button) c).setLabel("Start clicking"); 154 165 } 166 } else if (c.getName() == "sampleShot") { 167 System.out.println("Point to use: " + boxLeftTop + boxRightBottom); 168 169 /* Make sure we can select both ways */ 170 Integer xStart = (boxLeftTop.x < boxRightBottom.x) ? boxLeftTop.x : boxRightBottom.x; 171 Integer yStart = (boxLeftTop.y < boxRightBottom.y) ? boxLeftTop.y : boxRightBottom.y; 172 173 Integer xWidth = Math.abs(boxRightBottom.x - boxLeftTop.x); 174 Integer yWidth = Math.abs(boxRightBottom.y - boxLeftTop.y); 175 //pattern = bi.getSubimage(xStart - 20 , yStart - 50, xWidth, yWidth); 176 rawPattern = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); 177 bi.copyData(rawPattern.getRaster()); 178 xStart -= 20; 179 yStart -= 50; 180 System.out.println("Values used: " + xStart + " ," + yStart + ", " + xWidth + "," +yWidth); 181 pattern = rawPattern.getSubimage(xStart , yStart, xWidth, yWidth); 155 182 } 156 183 } 157 184 158 185 public void paint(Graphics g) { 159 186 super.paint(g); … … 166 193 167 194 g.drawImage(bi,20, 50, this); 195 g.drawImage(pattern, 320, 50, this); 168 196 169 197 if (drawBox){ 170 198 g.setColor(Color.RED); 171 g.drawRect(boxLeftTop.x, boxLeftTop.y, Math.abs(boxRightBottom.x - boxLeftTop.x), Math.abs(boxRightBottom.y - boxLeftTop.y)); 199 200 /* Make sure we can select both ways */ 201 Integer xStart = (boxLeftTop.x < boxRightBottom.x) ? boxLeftTop.x : boxRightBottom.x; 202 Integer yStart = (boxLeftTop.y < boxRightBottom.y) ? boxLeftTop.y : boxRightBottom.y; 203 204 Integer xWidth = Math.abs(boxRightBottom.x - boxLeftTop.x); 205 Integer yWidth = Math.abs(boxRightBottom.y - boxLeftTop.y); 206 g.drawRect(xStart, yStart, xWidth, yWidth); 172 207 } 173 208 174 209 greenButton.setEnabled(foundPlowLocation); 175 176 210 bi.createGraphics(); 177 211 … … 192 226 System.out.println("Location mouse changed " + mouse + "(was: " + prevLocation + ")"); 193 227 prevLocation = mouse; 228 229 /* XXX: Make sure we do not cross the boundaries */ 230 194 231 bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround, 195 232 mouse.y + mouseSurround)); 196 bi = bi.getSubimage(mouse.x - mouseSurround, 197 mouse.y - mouseSurround, 198 mouseSurround * 2, mouseSurround * 2); 233 234 Integer xBase = ((mouse.x - mouseSurround) < 0) ? mouseSurround : (mouse.x - mouseSurround); 235 Integer yBase = ((mouse.y - mouseSurround) < 0) ? mouseSurround : (mouse.y - mouseSurround); 236 bi = bi.getSubimage(xBase, yBase, mouseSurround * 2, mouseSurround * 2); 199 237 200 238 /* See is enabled */ … … 242 280 } 243 281 244 /* Do basic pattern recognition */ 282 Boolean patternMatch = false; 283 Point patLoc = new Point(0,0); 284 285 if (pattern != null) { 286 /* Do basic pattern recognition 287 * XXX: Does not scale, does not support multiple objects 288 * XXX: Maybe colour will help us over here as well 289 */ 290 nextx: for (int x = 0; x < bi.getWidth() - pattern.getWidth(); x++) { 291 nexty: for (int y = 0; y < bi.getHeight() - pattern.getHeight(); y++) { 292 for (int i = 0; i < pattern.getWidth(); i++) { 293 for (int j = 0; j < pattern.getHeight(); j++) { 294 if (bi.getRGB(x + i, y + j) != pattern.getRGB(i, j)) { 295 //System.out.println("Fail at (" + x + "," + y + ") [" + 296 // patLoc.x + "," + patLoc.y + "] " + 297 // bi.getRGB(x + i, y + j) + "/" + pattern.getRGB(i, j)); 298 continue nexty; 299 } 300 } 301 } 302 /* We are successful */ 303 patternMatch = true; 304 break nextx; 305 } 306 } 307 } 245 308 246 309 if(patternMatch) { 310 System.out.println("Match found at location: " + patLoc); 311 } 312 247 313 if (pixelTreshold == 0) { 248 314 pressMouse();
Note:
See TracChangeset
for help on using the changeset viewer.