home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / code / chap18 / Ex2.java < prev    next >
Encoding:
Java Source  |  1997-04-20  |  1.0 KB  |  47 lines

  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. public class Ex2 extends Applet { 
  5.    Color         color = Color.red;
  6.    int           candidate = 3;
  7.  
  8.    public void init() {
  9.       new PrimeThread().start();
  10.    }
  11.  
  12.    public void paint(Graphics g) {
  13.       g.setColor(color);
  14.       g.drawString(new Integer(candidate).toString(), 30, 40);
  15.    }
  16.  
  17.    class PrimeThread extends Thread {
  18.  
  19.       public void run() {
  20.          for ( ; ; candidate++) {
  21.             if (isPrime(candidate)) 
  22.                color = Color.red;
  23.             else
  24.                color = Color.blue;
  25.  
  26.             repaint();
  27.  
  28.             try {
  29.                sleep(1000);
  30.             } catch (InterruptedException ie) {
  31.             }         
  32.          }
  33.       }
  34.  
  35.       public boolean isPrime(int number) {
  36.          boolean isPrime = true;
  37.     
  38.          for (int i = 2; i < number - 1 && isPrime; i++) {
  39.             if ( (number % i ) == 0)
  40.                isPrime = false;
  41.          }
  42.          return isPrime;
  43.       }
  44.    }
  45.  
  46. }
  47.