// HelloAnim.java, version 1.01, June 11, 1999.
// Applet to introduce the simplest animation, a minimal applet that can be used
// as starting point for more ambitious projects. 
// Copyright 1999 by Rick Wagner, all rights reserved.

import java.applet.*;
import java.awt.*;

public class HelloAnim extends Applet implements Runnable
{
  // Applet instance variables (private where possible)
  private String sVerNum = "1.01";                   // Only constructors can run here
  private Dimension dPanel;                          // The applet panel size
  private Thread AnimThread = null;
  private Image imOffScreen = null;                      // Offscreen image for double buffering
  private Graphics grOffScreen = null;                   // Offscreen graphics for double buffering
  private int x1 = 0;
  private int x2 = 0;
  private int y1 = 0;
  private int y2 = 0;

  // To allow browsers to get information about the applet:
  public String getAppletInfo()
  {
    return "HelloAnim applet, version " + sVerNum +
           ", by Rick Wagner, copyright 1999, all rights reserved.";
  }

  // Initialize the applet (primarily for building the GUI)
  public void init()
  {
    setBackground(Color.white);
    dPanel = this.size();                            // The applet panel size (set in html code)
    x1 = 0;
    y1 = 0;
    x2 = dPanel.width / 20;
    y2 = dPanel.height / 20;
    this.showStatus("Click in the applet frame to animate.");
  }

  // Execute this code after initialization.
  public void start()
  {
    System.out.println("\n" + this.getAppletInfo());        // Identify self to the Java console-aware user
  }

  // Execute this code when the browser leaves the page
  public void stop()
  {
    if (AnimThread != null) AnimThread.stop();
    AnimThread = null;
  }

  // Execute this code when applet is removed from memory (rarely used)
  public void destroy()
  {
  }

  public void run()
  {
    System.out.println("Begin run()");
    int i = 0;

    x1 = 0;
    y1 = 0;
    x2 = dPanel.width / 20;
    y2 = dPanel.height / 20;
    int iFactor = 3;

    for (i = 0 ; i < dPanel.width - dPanel.width / 20; i += iFactor)
    {
      y1 = (x1 * dPanel.height) / dPanel.width;
      repaint();
      try
      {
        AnimThread.sleep(10);
      }
      catch (InterruptedException e)
      {
      }
      x1 += iFactor;
    }
    AnimThread = null;
    System.out.println("End run()");
  }

  // The applet runtime interpreter passes g to this applet frame painting function
  public void paint(Graphics g)
  {
    // Code for displaying images or drawing in the applet frame
    g.clearRect(0, 0, dPanel.width, dPanel.height);
    this.setBackground(Color.white);
    g.fillRect(x1, y1, x2, y2);
  }

  // Implements double buffering
  public void update(Graphics g)
  {
    if (imOffScreen == null)
    {
      // Make sure the offscreen and graphics exist
      imOffScreen = this.createImage(dPanel.width, dPanel.height);
      grOffScreen = imOffScreen.getGraphics();
      grOffScreen.clearRect(0, 0, dPanel.width, dPanel.height);
    }
    this.paint(grOffScreen);
    g.drawImage(imOffScreen, 0, 0, null);
  }

  public boolean mouseDown(Event e, int x, int y)    // For mouse events
  {
    System.out.println("Begin mouseDown()");
    AnimThread = new Thread(this);
    AnimThread.start();
    System.out.println("End mouseDown()");
    return true;
  }

  public boolean keyDown(Event e, int k)             // For keyboard events
  {
    return true;
  }

} // End of applet HelloLine class
