home *** CD-ROM | disk | FTP | other *** search
/ Java by Example / jbecd.bin / JBE-CD / NTUsers / JBECODE.ZIP / JavaByExample / chap31 / ThreadApplet.java < prev    next >
Encoding:
Java Source  |  1996-03-20  |  931 b   |  52 lines

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