Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Image get desktop image and create macro

Well I'm playing a online flash game and you have to click on white boxes as fast as possible. How could I automate this with Java? The location of boxes is randomized.

I tried using Robot class and use getPixelColor, but that is way too slow.

So what I need to do:

  1. my game windows is 500x500 window, so get pixels from it
  2. find 5x5 white boxes
  3. click on them

Any suggestions?

like image 430
Jaanus Avatar asked Apr 19 '26 20:04

Jaanus


1 Answers

You could use the Rectangle class, using the "contains" method.

Or you could create your own, which essentially works like this:

  1. Keep track of your boxes, their current (x, y) position, and their (width, height)
  2. Use the mouseClicked() event to get the (x, y) position of the click
  3. Inside of mouseClicked(), loop through all your boxes, and check to see if the mouse click (x, y) is inside the box

For example if you have two boxes on the screen:

boxA: (x, y, width, height) = (0, 0, 10, 10)
boxB: (x, y, width, height) = (20, 20, 10, 10)

This gives you two boxes, each of which is 10x10 pixels in size. "boxA" has it's top-left corner at (0, 0), and "boxB" has its top-left corner at (20, 20).

If the "mouseClicked" event's (x, y) coordinate is (7, 7), then that is within the bounds of "boxA" (because the point (7, 7) is between (0, 0) and (10, 10)

If the "mouseClicked" event's (x, y) coordinate is (23, 25), then it's within "boxB", because (23, 25) is between (20, 20) and (30, 30)

You're right that getPixelColor is too slow for what you're trying to do. Looping over the list of the boxes will be much faster.


If you want to dig into a much deeper example, here's an article on the concept of collision detection.

like image 77
jefflunt Avatar answered Apr 22 '26 11:04

jefflunt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!