home *** CD-ROM | disk | FTP | other *** search
/ Web Programming with Visual J++ / Web_Programming_with_Visual_J_SAMS.NET_Version_1.0_1997.iso / source / chap12 / ex12b.java < prev    next >
Encoding:
Java Source  |  1996-09-22  |  1.8 KB  |  94 lines

  1. import java.applet.*;
  2. import java.awt.*;
  3.  
  4. public class EX12B extends Applet implements Runnable
  5. {
  6.     protected Thread CounterThread;
  7.     protected Button SuspendButton = new Button("Suspend");
  8.     protected Button ResumeButton = new Button("Resume");
  9.     protected Label CountLabel;
  10.     protected int count = 10;
  11.  
  12.     public void init()
  13.     {
  14.         // gives us some control over where the controls are
  15.         //   to be placed
  16.         setLayout(new BorderLayout());
  17.  
  18.         // provide some control over the count
  19.         Panel p = new Panel();
  20.         p.add(SuspendButton);
  21.         p.add(ResumeButton);
  22.         add("North", p);
  23.  
  24.         // provide a visual of the count
  25.         p = new Panel();
  26.         p.add(new Label("Count:"));
  27.         CountLabel = new Label(Integer.toString(count));
  28.         p.add(CountLabel);
  29.         add("Center", p);
  30.     }
  31.  
  32.     public void start()
  33.     {
  34.         if (CounterThread == null) {
  35.             // allocate the thread, using the local run method
  36.             CounterThread = new Thread(this);
  37.             CounterThread.start();
  38.         }
  39.     }
  40.     
  41.     public void stop()
  42.     {
  43.         if (CounterThread != null) {
  44.             // stop the thread
  45.             CounterThread.stop();
  46.             CounterThread = null;
  47.         }
  48.     }
  49.  
  50.     public boolean action(Event evt, Object arg)
  51.     {
  52.         boolean retval = false;            // assume event not processed
  53.  
  54.         if (evt.target == SuspendButton) {
  55.             if (CounterThread != null)
  56.                 CounterThread.suspend();
  57.  
  58.             retval = true;
  59.         }
  60.         else if (evt.target == ResumeButton) {
  61.             if (CounterThread != null)
  62.                 CounterThread.resume();
  63.  
  64.             retval = true;
  65.         }
  66.  
  67.         return retval;
  68.     }
  69.  
  70.     public void run()
  71.     {
  72.         while (true) {
  73.             try {
  74.                 Thread.sleep(1000);
  75.             }
  76.             catch (InterruptedException e) {}
  77.  
  78.             CountDown();
  79.         }
  80.     }
  81.  
  82.     protected void CountDown()
  83.     {
  84.         count--;
  85.  
  86.         CountLabel.setText(Integer.toString(count));
  87.  
  88.         if (count == 0)
  89.             // reset to 11 since the first call will move it back
  90.             //   down to 10 and then display
  91.             count = 11;
  92.     }
  93. }
  94.