Changeset 111 for java-automator/src/nl
- Timestamp:
- Apr 15, 2010, 12:19:42 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java-automator/src/nl/rickvanderzwet/farmville/ScreenRecognition.java
r108 r111 2 2 * Rick van der Zwet <info@rickvanderzwet.nl> 3 3 * License BSDLike http://rickvanderzwet.nl/LICENSE 4 * XXX: Do not look at this code, it's dirty and _NOT_ the way Java should be written. 4 5 */ 5 6 6 /* Based after http://ironbark.bendigo.latrobe.edu.au/~mary/DS/lectures/tutorials/ACSWorkshop98.html */7 7 package nl.rickvanderzwet.farmville; 8 8 … … 12 12 import java.awt.image.RasterFormatException; 13 13 14 15 public class ScreenRecognition extends Frame implements ActionListener { 14 import javax.swing.BorderFactory; 15 import javax.swing.JButton; 16 import javax.swing.JFrame; 17 import javax.swing.JLabel; 18 import javax.swing.JLayeredPane; 19 import javax.swing.JPanel; 20 import javax.swing.Timer; 21 22 //class ScreenPainter extends Panel { 23 // private static final long serialVersionUID = -3073797542841857452L; 24 // BufferedImage foo = null; 25 // 26 // public void paint(Graphics g) { 27 // // System.out.println("FOO"); 28 // // g.drawImage(foo,20, 50, this); 29 // } 30 //} 31 32 public class ScreenRecognition extends JFrame implements ActionListener,MouseMotionListener,MouseListener { 16 33 /** 17 34 * … … 24 41 static BufferedImage bi; 25 42 static Robot bot; 26 43 27 44 static boolean freezeImage = false; 28 static boolean mouseClickEnabled = true; 45 static boolean mouseClickEnabled = false; 46 47 static boolean foundPlowLocation = false; 48 49 static Button redButton; 50 static Button greenButton; 51 52 Point boxLeftTop, boxRightBottom = new Point (0,0); 53 Boolean drawBox = false; 54 static Boolean draggingBox = false; 29 55 30 56 private static void pressMouse() throws InterruptedException { … … 38 64 public ScreenRecognition() { 39 65 setTitle("Screen Recognition Example"); 66 JLayeredPane layeredPane = new JLayeredPane(); 67 68 //layeredPane.setBorder(BorderFactory.createTitledBorder("Foo")); 69 //Panel foo = new ScreenPainter(); 70 //foo.setBackground(Color.RED); 71 //foo.setBounds(70, 70, 50, 50); 72 73 JLabel bar = new JLabel(); 74 bar.setText("FooBAR"); 75 bar.setBounds(0, 0, 200, 20); 76 // Create 3 buttons 77 JPanel top = new JPanel(); 78 top.setBackground(Color.white); 79 top.setBounds(20, 20, 50, 50); 80 JPanel middle = new JPanel(); 81 middle.setBackground(Color.gray); 82 middle.setBounds(40, 40, 50, 50); 83 JPanel bottom = new JPanel(); 84 bottom.setBackground(Color.black); 85 bottom.setBounds(60, 60, 50, 50); 86 87 // Place the buttons in different layers 88 // layeredPane.add(foo, new Integer(5)); 89 // layeredPane.add(bar, new Integer(5)); 90 // layeredPane.add(middle,new Integer(0)); 91 // layeredPane.add(top, new Integer(4)); 92 // layeredPane.add(bottom, new Integer(0)); 40 93 41 94 Panel buttons = new Panel(); 42 ButtonredButton = new Button("Red");95 redButton = new Button("Red"); 43 96 Button blueButton = new Button("Blue"); 44 ButtongreenButton = new Button("Green");97 greenButton = new Button("Green"); 45 98 redButton.setName("redButton"); 46 99 redButton.setLabel("Freeze Screen"); … … 52 105 buttons.add(blueButton); 53 106 buttons.add(greenButton); 107 108 109 // add(layeredPane); 54 110 add(buttons,"South"); 55 111 56 112 57 113 setSize(400,300); 58 114 addWindowListener(new WindowAdapter() { … … 62 118 }); 63 119 setVisible(true); 120 addMouseMotionListener( this ); 121 addMouseListener( this ); 122 64 123 } 65 124 66 125 public void actionPerformed(ActionEvent event) { 126 System.out.println("ActionPerformed"); 67 127 Component c = (Component)event.getSource(); 68 128 if (c.getName() == "redButton") { 69 c.getToolkit().beep(); 70 try { 71 Thread.sleep(3000); 72 } catch (InterruptedException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 129 if (freezeImage) { 130 freezeImage = false; 131 redButton.setLabel("Freeze image"); 132 } else { 133 c.getToolkit().beep(); 134 135 /* Freeze screen after number of seconds seconds */ 136 ActionListener freezeScreen = new ActionListener() { 137 public void actionPerformed(ActionEvent evt) { 138 System.out.println(freezeImage); 139 freezeImage = true; 140 redButton.setLabel("Release image"); 141 } 142 }; 143 Timer foo = new Timer(3000, freezeScreen); 144 foo.setRepeats(false); 145 foo.start(); 146 redButton.setLabel("Freezing image..."); 75 147 } 76 freezeImage = !freezeImage;77 148 } else if (c.getName() == "blueButton") { 78 149 mouseClickEnabled = !mouseClickEnabled; … … 84 155 } 85 156 } 86 157 87 158 public void paint(Graphics g) { 159 super.paint(g); 88 160 g.drawString("Mouse:" + mouse.x + "," + mouse.y ,20,40); 89 161 g.drawString("PixelRGB:" + pixel.toString() ,150,40); … … 92 164 g.setColor(pixel); 93 165 g.fillRect(300,100,20,20); 166 94 167 g.drawImage(bi,20, 50, this); 168 169 if (drawBox){ 170 g.setColor(Color.RED); 171 g.drawRect(boxLeftTop.x, boxLeftTop.y, Math.abs(boxRightBottom.x - boxLeftTop.x), Math.abs(boxRightBottom.y - boxLeftTop.y)); 172 } 173 174 greenButton.setEnabled(foundPlowLocation); 175 176 bi.createGraphics(); 177 95 178 } 96 179 … … 100 183 ScreenRecognition foo = new ScreenRecognition(); 101 184 185 Point prevLocation = new Point(0,0); 102 186 while (true) { 103 187 PointerInfo pointer = MouseInfo.getPointerInfo(); … … 105 189 pixel = bot.getPixelColor(mouse.x, mouse.y); 106 190 try { 107 if (!freezeImage) { 108 bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround, 109 mouse.y + mouseSurround)); 110 bi = bi.getSubimage(mouse.x - mouseSurround, 111 mouse.y - mouseSurround, 112 mouseSurround * 2, mouseSurround * 2); 113 114 /* See is enabled */ 115 Color target = new Color(255, 170, 0); 116 int pixelTreshold = 10; 117 118 search: for (int x = 0; x < bi.getWidth(); x++) { 119 for (int y = 0; y < bi.getHeight(); y++){ 120 if (bi.getRGB(x, y) == target.getRGB()) { 121 pixelTreshold--; 122 if (pixelTreshold == 0) { 123 break search; 191 if (!freezeImage && !mouse.equals(prevLocation)) { 192 System.out.println("Location mouse changed " + mouse + "(was: " + prevLocation + ")"); 193 prevLocation = mouse; 194 bi=bot.createScreenCapture(new Rectangle(mouse.x + mouseSurround, 195 mouse.y + mouseSurround)); 196 bi = bi.getSubimage(mouse.x - mouseSurround, 197 mouse.y - mouseSurround, 198 mouseSurround * 2, mouseSurround * 2); 199 200 /* See is enabled */ 201 final Color target = new Color(255, 170, 0); 202 int pixelTreshold = 10; 203 204 search: for (int x = 0; x < bi.getWidth(); x++) { 205 for (int y = 0; y < bi.getHeight(); y++){ 206 if (bi.getRGB(x, y) == target.getRGB()) { 207 pixelTreshold--; 208 if (pixelTreshold == 0) { 209 break search; 210 } 124 211 } 125 212 } 126 213 } 127 } 128 if (pixelTreshold == 0) { 129 pressMouse(); 130 System.out.println("Button Click at [" + mouse.x + "," + mouse.y + "]"); 131 } 214 215 /* Select text only */ 216 final Color white = new Color(255, 255, 255); 217 final Color black = new Color(0, 0, 0); 218 for (int x = 0; x < bi.getWidth(); x++) { 219 for (int y = 0; y < bi.getHeight(); y++){ 220 if (bi.getRGB(x, y) != white.getRGB()) { 221 bi.setRGB(x,y, black.getRGB()); 222 } 223 } 224 } 225 226 /* Determine text borders */ 227 Point leftTop = new Point(bi.getWidth(),bi.getHeight()); 228 Point rightBottom = new Point(0,0); 229 for (int x = 0; x < bi.getWidth(); x++) { 230 for (int y = 0; y < bi.getHeight(); y++){ 231 if (bi.getRGB(x, y) == white.getRGB()) { 232 if (x < leftTop.x && y < leftTop.y) { 233 leftTop.x = x; 234 leftTop.y = y; 235 } 236 if (x > rightBottom.x && y > rightBottom.y) { 237 rightBottom.x = x; 238 rightBottom.y = y; 239 } 240 } 241 } 242 } 243 244 /* Do basic pattern recognition */ 245 246 247 if (pixelTreshold == 0) { 248 pressMouse(); 249 System.out.println("Button Click at [" + mouse.x + "," + mouse.y + "]"); 250 } 132 251 133 252 } … … 143 262 144 263 } 264 265 266 @Override 267 public void mouseDragged(MouseEvent e) { 268 boxRightBottom = e.getPoint(); 269 //System.out.println("Dragged"); 270 271 } 272 273 @Override 274 public void mouseMoved(MouseEvent e) { 275 //System.out.println("Moved"); 276 277 // TODO Auto-generated method stub 278 279 } 280 281 public void mouseClicked (MouseEvent e) { 282 //System.out.println("Clicked"); 283 } 284 285 public void mouseReleased (MouseEvent e) { 286 //System.out.println("Released"); 287 draggingBox = false; 288 } 289 290 @Override 291 public void mouseEntered(MouseEvent e) { 292 //System.out.println("Entered"); 293 294 // TODO Auto-generated method stub 295 296 } 297 298 @Override 299 public void mouseExited(MouseEvent e) { 300 //System.out.println("Exited"); 301 302 // TODO Auto-generated method stub 303 304 } 305 306 @Override 307 public void mousePressed(MouseEvent e) { 308 //System.out.println("Pressed"); 309 drawBox = true; 310 boxLeftTop = e.getPoint(); 311 draggingBox = true; 312 // TODO Auto-generated method stub 313 314 } 145 315 }
Note:
See TracChangeset
for help on using the changeset viewer.