home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap31 / ThreadApplet4.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-21  |  1.5 KB  |  54 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5.  
  6. public class ThreadApplet4 extends Applet implements Runnable {
  7.    Thread thread;
  8.    int count;
  9.    String displayStr;
  10.    Font font;
  11.  
  12.    public void init() {
  13.       this.font = new Font("TimesRoman", 0, 72);
  14.       ((Component)this).setFont(this.font);
  15.       this.count = 0;
  16.       this.displayStr = "";
  17.       this.thread = new Thread(this);
  18.    }
  19.  
  20.    public void start() {
  21.       if (this.thread.isAlive()) {
  22.          this.thread.resume();
  23.       } else {
  24.          this.thread.start();
  25.       }
  26.    }
  27.  
  28.    public void stop() {
  29.       this.thread.suspend();
  30.    }
  31.  
  32.    public void destroy() {
  33.       this.thread.stop();
  34.    }
  35.  
  36.    public void run() {
  37.       while(this.count < 1000) {
  38.          ++this.count;
  39.          this.displayStr = String.valueOf(this.count);
  40.          ((Component)this).repaint();
  41.  
  42.          try {
  43.             Thread.sleep(100L);
  44.          } catch (InterruptedException var1) {
  45.          }
  46.       }
  47.  
  48.    }
  49.  
  50.    public void paint(Graphics var1) {
  51.       var1.drawString(this.displayStr, 50, 130);
  52.    }
  53. }
  54.