Tag Archives: Robot class

Hometrainer 2.0 – Source code

I’ve recently had a request to publish the source code of the hometrainer application I made a while back. The application was a continuation of the research I did on connecting a hometrainer to a computer using the sound card and calculating the bike’s speed by processing the audio signal.

The source code can be found at the GitHub repository.

I’ve rewritten most of the code to make it easier to use (notable by the use of tons of constants to tweak the application to your needs), but I also removed the part that controlled my media player. I couldn’t find a nice cross-platform method of doing this without resorting to gigantic APIs or messy hacks. The previous code used the Robot class in Java to simulate pressing the spacebar, but this is a rather messy method that can even complicate using your computer.

The code consists of three classes: Hometrainer, View and Main. The Hometrainer class does the work of monitoring the audio port and making sense of the incoming data. It extends the Observable class so it is easy to subscribe to updates by making your own class implement the Observer class (it this is unclear, you can read up on the Observer pattern). This is exactly what the View class does, it receives updates from the Hometrainer class and display the current speed and distance in a nice JFrame using JLabels. You can use this class as an example of how to grab data from the Hometrainer class. This leaves us with the Main class, which is a small class that initializes both classes.

Feel free to use the code however you want. If you have any questions, don’t hesitate to use the comments box below.

Automated game of Doeo using Java Robot class

Now that pong is fully automated, it is time to optimize.

First I switched to the game Doeo. The goal of the game is to move your mouse as fast over Doeos as possible. A fun game to use as an example for automation.

I also made a small performance enhancement (10 to 15 percent) by not using getRGB () from BufferdImage, but reading directly from an array. It does take some time to get everything into an integer array, but if that’s done, the for-loop is a lot faster.

Demo video:

The optimized code:

Continue reading

Automated game of pong using Java Robot class

Using the Java Robot class, I have written a small utility that plays an automated game of pong against the computer.

It constantly monitors the left side of the game, and when a ball enters the field, it moves the mouse cursor to the position of the ball.

Demo video:


This is a simple introduction to image-recognition and mouse-movement in Java. Feel free to use the code, optimize or improve.

Usage of the RobotPong object::

  • Create an object using the coordinate of the part of the screen that needs to be monitored.
  • Start the object using the start() method, this will start the thread containing the object.
  • It is possible to run multiple threads, each playing their own game of pong.

The game itself can be found at the MegaFunGames webpage.

The code:

Continue reading