/*
Applet      : APPLET1.class
Author      : Rob Schluter
Description : Example Java applet, used in HTML tag list.
*/

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

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

  Thread runner;
  Font f = new Font("TimesRoman", Font.BOLD, 25);
  FontMetrics fm = getFontMetrics(f);
  String text;
  int pos;

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

  public void stop() {
    if (runner != null); {
       runner.stop();
       runner = null;
    }
  }

  public void init() {
    text = getParameter("text");
    if (text == null) {
       text = "A Java example"; }
  }

  public void run() {
    setBackground(new Color(0,0x80,0x80));
    while (true) {
      for (pos = 0;pos < text.length();pos++) {
          repaint();
          try { runner.sleep(100); }
          catch (InterruptedException e) {}
      }
    }
  }

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

  public void paint(Graphics g) {
    String t1,t2,t3;
    g.setFont(f);

    if (pos == 0) {
       t1 = "";
       t2 = text.substring(0,1);
       t3 = text.substring(pos + 1);
    } else {
       t1 = text.substring(0,pos);
       t2 = text.substring(pos,pos + 1);
       t3 = text.substring(pos + 1);
    }
    g.setColor(Color.white);
    g.drawString(t1, 15, 30);
    g.setColor(Color.red);
    g.drawString(t2, 15 + fm.stringWidth(t1), 30);
    g.setColor(Color.white);
    g.drawString(t3, 15 + fm.stringWidth(t1 + t2), 30);
  }
}

Back to the APPLET element.
Statistics

  Copyright © 1996 - 2000 Rob Schlüter,   schluter@knoware.nl   (last updated 1998/01/13)