/********************************************************************
 * This is an applet that creates a movie and a thermometer.  It then
 * creates three canvases, attaching one as an observer to the movie
 * and the other two as observers to the single thermometer.  This is
 * another example of having two observers of a single object. The
 * canvases are attached to the applet panel from left to right with
 * a thermomter display followed by the movie display followed by a
 * second display of the thermometer.
 * Then the temperature on the thermometer is changed in a for loop
 * with appropriate changes to the thermometer title.  This is a test
 * of the setters in the class Thermometer and of some simple
 * animation.
 ********************************************************************/
 
import java.applet.*;
import java.awt.*;
import java.lang.Thread;
 
public class ThermApplet extends Applet implements Runnable
{
/********************************************************************
 * Instance variables
 ********************************************************************/

 // Create thermometer to display
 Thermometer T = new Thermometer("Thermometer Demo",0,100,33);

 ThermoCanvas thermoCanvas1 = new ThermoCanvas( T );// Create canvas
 ThermoCanvas thermoCanvas2 = new ThermoCanvas( T );// observers

/********************************************************************
 * Instance methods
 ********************************************************************/

 public void init()
 {
  System.out.println("Enter ThermApplet: init()");
  setLayout(new BorderLayout());
  add("West", thermoCanvas1);	// Attach 1st thermo canvas to applet panel
  add("East", thermoCanvas2);	// Attach 2nd thermo canvas to applet panel
 }

/********************************************************************
 * Set up the animation
 ********************************************************************/
 private Thread animatorThread = null;

 public void start()
 {
   System.out.println("Enter ThermApplet: start()");
   if (animatorThread == null)
   {
     animatorThread = new Thread(this);		// For 1st call to start()
     animatorThread.start();
   }
   else
     animatorThread.resume();			// For 2nd & subsequent calls
 }

 public void stop()
 {
   System.out.println("Enter ThermApplet: stop()");
   if (animatorThread != null && animatorThread.isAlive())
     animatorThread.suspend();
 }

 public void run()
 {
  System.out.println("Enter ThermApplet: run()");
  for (int i = 91; i >= 0; i--)
  {
    try{ Thread.sleep(50); }
    catch(InterruptedException e)
     {System.out.println(e);
      System.exit(1);
     }
    T.setTemp(i);
    switch (i)
    {
     case 10:  T.setTitle("Baby, its cold outside!");
               break;
     case 32:  T.setTitle("It's a cool day.");
               break;
     case 65:  T.setTitle("It's a nice day.");
               break;
     case 80:  T.setTitle("It's cooling off!");
               break;
     case 90:  T.setTitle("Boy, it's sweltering!");
               break;
     default:
    }
  }
 }
}
