Bobbie Smulders Freelance Software Developer

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.

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:

import java.awt.*;
import java.awt.image.*;


©2010 Bobbie Smulders

RobotPong plays an automated game of Pong.
Original game can be found here:


public class RobotPong extends Thread {

	public Rectangle rect;

	public RobotPong(int x, int y, int width, int height) {
		rect = new Rectangle(x, y, width, height);

	public void run() {
			Robot robot = new Robot();

			while(true) {
				BufferedImage bufferedImage = robot.createScreenCapture(rect);

				for (int x_scan = 0; x_scan < rect.width; x_scan++) {
					for(int y_scan = 0; y_scan < rect.height; y_scan++) {
						if(bufferedImage.getRGB(x_scan, y_scan) == -1) {
							robot.mouseMove(rect.x + x_scan, y_scan + rect.y);
							break search;
		catch(AWTException e) {

	public static void main(String[] args) {
		new RobotPong(44, 90, 100, 255).start();