This example animates the HelloWorld applet by drawing the string in different locations, as if it were scrolling to the left.

Let's look at the code, which can be found in HelloAnim.java:

Line 1 and 2 specify what classes to import. These are the, by now familiar:

        import java.awt.*;
        import java.applet.Applet;
The applet also uses the Thread class, which is defined in the java.lang package. However, when you compile your applet, the Java compiler includes the java.lang package automatically.

Line 4 specifies the HelloAnim class as extending the Applet class and implementing the Runnable interface:

	public class HelloAnim extends Applet implements Runnable {
The applet needs the Runnable interface to support threads. The Runnable interface tells Java that your class contains a run() function. Unlike C++, Java does not support multiple inheritance, but Java provides a way to use interfaces to define multpile classes which implement the same functions. Remember that an interface is an abstract class definition, which means that you cannot declare a variable of this class. In other words, when you define an interface, you give a name to a set of functions which are defined in the interface. In this apple, we will implement the Runnable interface. The Runnable interface tells Java the class has a run() function. So, to implement the Runnable interface, the applet must define the run() function.

The HelloAnim class declares three variables to specify the text's (x,y) location and also the window's width. In addition, the class declares the Thread object, called my_thread:

    		int x = 0;
    		int y = 0;
    		int width = 0;
    		Thread my_thread = null;

Initializing

The init() is used to perform any one-time initialization that is necessary when the applet is first created. The init() function uses the size function (inherited from the Applet class) to get window's width and height. It initializes the x variable to the width of the window, and the y variable to half the height of the window, so that the string gets drawn centered vertically in the window. It sets the width variable to be equal to the window's width:
    	public void init() {
        	x = size().width;
        	y = size().height / 2;
        	width = x;
    	}

The Thread

The applet's start function gets called after the init() function ends. The new operator is used to create a Thread object. By passing the this value, you ensure that this applet's run function gets called by the thread's start function:
    public void start()
      {
        my_thread = new Thread(this);
        my_thread.start();
      }
Once the thread is started, the real work gets done in the run function. An endless loop is started, which continuously moves the message across the screen. It calls the repaint() function, which ensures that the applet window's contents are up to date (it calls paint). After painting the string, the x value is decreased by 10, which causes the string to move left on the screen the next time it gets drawn. Then the thread sleeps for 100 milliseconds and loops again. If the sleep is interrupted, it throws an exception of type InterruptedException:
    public void run()
      {
        while(true)
          {
            repaint();
            x -= 10;
            if(x < 0)
              x = width;

            try
              {
                Thread.sleep(100);
              }
            catch(InterruptedException e)
              {
              }
          }
      }
In Java, the applet must catch all exceptions. Therefore, you must place the code that contains exceptions inside a try block, which is followed by catch statements.

Painting

The paint function simply displays the message "HelloWorld!" on the screen at location (x,y):
    public void paint(Graphics g)
      {
        g.drawString("Hello World!", x, y);
      }


Back to Lecture2