// File $CISC370HOME/example-progs/threads/animation/ThermApplet.java /******************************************************************** * This is an applet that creates a thermometer. It then * creates two canvases, attaching each * as observers to the single thermometer. This is * example of having two observers of a single object. The * canvases are attached to the east and west parts of the applet panel. * 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; import javax.swing.*; public class ThermApplet extends JApplet 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 for applet ********************************************************************/ public void init() // The init method for the Applet { System.out.println("Enter ThermApplet: init()"); Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add("West", thermoCanvas1); // Attach 1st thermo canvas to applet panel cp.add("East", thermoCanvas2); // Attach 2nd thermo canvas to applet panel } public void start(){ // The start method for Applet System.out.println("Enter ThermApplet: start()"); if (animatorThread == null) { animatorThread = new Thread(this);// For 1st call to Applet.start() animatorThread.start(); // Creates system resources for } // the thread & schedules it to run else animatorThread.resume(); // For 2nd & subsequent calls } // to Applet.start(). Return thread // to runnable state. public void stop() // The stop method for the Applet { System.out.println("Enter ThermApplet: stop()"); if (animatorThread != null && animatorThread.isAlive()) animatorThread.suspend(); // Put thread in not runnable state } /******************************************************************** * Set up thread for animation ********************************************************************/ private Thread animatorThread = null; // Another instance variable // Required to implement Runnable. Called by Thread.start() public void run(){ System.out.println("Enter ThermApplet: run()"); for (int i = 91; i >= 0; i--) { System.out.println("ThermApplet.run():i = " + i); try{ Thread.sleep(50); // A static method that effects the } catch(InterruptedException e){ // currently running thread. 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: } } } // Thread dies a natural death on termination of the loop }