Search code examples
javaawtrobot

Using Java Robot to capture a region of a window


I'm attempting to create a program which plays a mobile game automatically, using the Robot class.

As I've currently planned it, the Robot will make decisions by looking at parts of the game window (e.g. what round is it, what kind of enemy has appeared, how many stamina do you have, etc).

Is it possible to make the Robot's createScreenCapture() reference its coordinates from a window, rather than the entire screen?


Solution

  • Unfortunately this would require you to look into OS specific functions, and to access them you need native calls through JNA or JNI.

    There's one simple way to do what you want though. Get the user to click to on a specific part of the window, or have them position it in the top left corner! You could also scan the screenshot for a specific pattern such as the title of the window or an image that's always present in the window.