import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;

/**
 * A simple applet that prints a string, piece by piece..
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of November 1998
 */
public class AnimatedWelcome 
  extends Applet 
  implements Runnable 
{
  /** The message to print. */
  String message;
  
  /** The color of the message. */
  Color color;

  /** The portion of the message to print. */
  int amt = 1;
  
  /** The thread for the applet. */
  Thread welcomeThread;
  
  /**
   * Set up the message to print.
   */
  public void init() {
    message = getParameter("Message");
    if (message == null) { message = "A Sample Message"; }
    color = Color.blue;
  } // init()
  
  /*
   * Stop the thread.
   */
  public void stop() {
    welcomeThread = null;
  }
  
  /**
   * Start up.
   */
  public void start() {
    if ((amt < message.length()) && (welcomeThread == null)) {
      welcomeThread = new Thread(this);
    }
    welcomeThread.start();
  } // start
  
  /*
   * Run the program (or the thread).
   */
  public void run() {
    //Remember which thread we are.
    Thread currentThread = Thread.currentThread();

    //This is the display loop.
    while ( (currentThread == welcomeThread) &&
            (amt <= message.length()) ) {
      //Display it.
      repaint();
      // Pause
      try {
        Thread.sleep(200); // 1/5 of a second
      } catch (InterruptedException e) { break; }
    } // while
  } // run
  
  /*
   * Paint part of the message.
   */
  public void paint(Graphics g) {
    g.setColor(color);
    g.setFont(new Font("Times", Font.BOLD, 16));
    g.drawString(message.substring(0,amt), 10, 16);
    if (amt < message.length()) 
      ++amt;
    else {
      // Change the color.
      if (color.equals(Color.blue))
        color = Color.red;
      else
        color = Color.blue;
      // Reset the amount
      amt = 0;
    }
  } // paint(Graphics)
} // class AnimatedWelcome

