jeffheaton's picture
in

So far we have only produced programs that display "still" graphic images. Nothing is moving. To get motion, you must use multithreading.

Multithreading allows your program to do more than one thing at once. This allows a function to remain running in the background. In this case, the background function will drive the animation.

However, before we create an animated multithreading application we will see how to create a thread. We will create a simple application that displays a counter. You can see this program in Figure 3.2.

 Multithreaded counter

Figure 3.2: Multithreaded counter

This program works by creating a thread. This thread displays a counter that runs in the background. The source code for this program is shown in Listing 3.2.

Listing 3.2: Using threads in Java (ThreadApplet.java)

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ThreadApplet extends Applet implements Runnable
{
  /**
   * When this class is ran as an application, how wide should it be.
   */
  public static final int IDEAL_WIDTH = 200;

  /**
   * When this class is ran as an application, how tall should it be.
   */
  public static final int IDEAL_HEIGHT = 200;

  /**
   * The thread that is used to drive the counter.
   */
  private Thread thread;


  /**
   * Setup the applet, and create the thread.
   *
   */
  public void init()
  {
    thread = new Thread(this);
    thread.start();
  }

  /**
   * Run the background thread. This thread will display a
   * counter in the applet.
   *
   */
  public void run()
  {
    Graphics g = getGraphics();

    for (int i = 0; i <= 1000; i++)
    {
      g.setColor(Color.BLACK);
      g.fillRect(0, 0, getWidth(), getHeight());
      g.setColor(Color.WHITE);
      g.drawString("Counting: " + i, 10, 10);
      try
      {
        Thread.sleep(1000);
      } catch (InterruptedException e)
      {
        return;
      }
    }
  }

  /**
   * The main method is called when the class is to be ran
   * as a Java application. This method creates a frame and
   * attaches the applet to the frame.
   *
   * @param args Not used.
   */
  public static void main(String args[])
  {
    Applet applet = new ThreadApplet();
    Frame frame = new Frame();
    frame.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });

    frame.add(applet);
    frame.setSize(IDEAL_WIDTH, IDEAL_HEIGHT);
    frame.show();
  }
}

First the background thread must be created in started. This is done inside of the "init" method. The thread is created with the following lines of code.

thread = new Thread(this);
thread.start();

A new "Thread" object is created. The current object (this) is passed in as the object that contains the background function to be run. Any class that implements the "Runnable" interface can be passed to the "Thread" object. The "Runnable" object must contain a method named "run". The "run" method should then begin a loop and continue until the program is done.

The "run" method for this simple example begins a loop that will display a counter that will count up to 1,000. The program delays by one second for each iteration of the loop. This allows the counter to progress slow enough to be read.


Copyright 2005 - 2012 by Heaton Research, Inc.. Heaton Research™ and Encog™ are trademarks of Heaton Research. Click here for copyright, license and trademark information.