Please send questions to st10@humboldt.edu .
/**
 * Train1
 * 
 * a fifth demo for Threads, a first for animation using threads;
 * this one modifies the old Spring 2000 CIS 235 Train1.java,
 * making it avoid deprecated Thread methods.
 * It uses an AWT Applet and the Runnable interface, also,
 * and animates an image of a train moving left to right across
 * the applet area.
 *
 * GIF file required: train.gif
 *
 * RECOMMENDED SIZE: height 200, width 600
 *
 * modified by: Sharon M. Tuttle
 * last modified: 3-26-01
 **/

import java.applet.Applet;
import java.awt.*;

public class Train1 extends Applet implements Runnable
{
        int     x, y;
        Image   myTrain;
        Thread  myRunner;

        public void init()
        {
                setBackground(Color.lightGray);
                System.out.println("Train1, Version 1.1");
                Label yourName = new Label("CIS 480 - Spring 2001");
                add(yourName);
                Label describe = new Label("Train1: Version 1.1");
                add(describe);

                // currently in same directory as my .class file
                myTrain = getImage(getCodeBase(),"train.gif");

                // hard code the y pixel location
                y = 100;

		// let's start x at 0...
		x = 0;
        }

        public void start()
        {
                if (myRunner == null)
                { 
                        // not bothering to name the thread, this time;
                        myRunner = new Thread(this);
                        myRunner.start();
                }
        }

        public void run()
        {
                Thread  executingThread;
                                
                executingThread = Thread.currentThread();

                while (myRunner == executingThread)
                {
                        x = 0;
                        repaint();
                        try
                        {
                                // sleep for 1 second (1000 msec = 1 sec)
                                Thread.sleep(1000);
                        }
                        catch(InterruptedException e) 
                        {
                        } 

                        for (int k = 0; k < 120; k++)
                        {
                                x = k * 5;
                                repaint();
                                try
                                {
                                        // pause for .1 second between
                                        // repaintings
                                        Thread.sleep(100);  // .1 second pause
                                }
                                catch (InterruptedException e) 
                                {
                                }
                        }  //end for
                }  //end while
        }

        //suspends execution when user leaves page
        public void stop()
        { 
                if (myRunner != null)
                {
                        myRunner = null;
                }
        }

        public void paint(Graphics g)
        {
                g.drawImage(myTrain, x, y, this);
        }
}