Internet Systems Design:
Technologies for Modern Information Systems

Example Applets II

Note: If you find that Java Applets aren't working correctly (you will usually just see a gray rectangle) on your browser, download the JRE.

/* print the date */
import java.awt.Graphics;

import java.awt.Font;

import java.util.Date;
public class DigitalThreads extends java.applet.Applet 

  implements Runnable {
  Font theFont = new Font("TimesRoman",Font.BOLD,24);

  Date theDate;

  Thread runner;
  public void start() {

    if (runner == null); {

      runner = new Thread(this);

      runner.start();

    }

  }
  public void stop() {

	runner = null;

	}
  public void run() {

    while (true) {

      theDate = new Date();

      repaint();

      try { Thread.sleep(1000); }

      catch (InterruptedException e) { }

    }

  }
  public void paint(Graphics g) {

    g.setFont(theFont);

    g.drawString(theDate.toString(),10,50);

  }

}

Your browser does not support Java

/* swirly colors */ 

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;

public class ColorSwirl2 extends java.applet.Applet implements Runnable {

    Font f = new Font("TimesRoman",Font.BOLD,48);
    Color colors[] = new Color[50];
    Thread runThread;

    public void start() {
        if (runThread == null) {
        runThread = new Thread(this);
        runThread.start();
        }
    }

  public void stop() {
        runThread = null;
   } 

    public void run() {
        // initialize the color arry
        float c = 0;
        for (int i = 0; i < colors.length; i++) {
            colors[i] = Color.getHSBColor(c, (float)1.0,(float)1.0);
            c += .02;
            }

       // cycle through the colors
        int i = 0;
        while (true) {
        setForeground(colors[i]);
        repaint();
        i++;
        try { Thread.currentThread().sleep(50); }
        catch (InterruptedException e) { }
        if (i == (colors.length - 1)) i = 0;
            }
    }

    public void update(Graphics g) {
        paint(g);
        }

    public void paint(Graphics g) {
        g.setFont(f);
        g.drawString("Internet Systems Design", 15,50);
        }
}