import java.awt.*;
import java.applet.*;

public class textanim extends java.applet.Applet implements Runnable {

String message = null;                // animated text
int delay = 100;                      // delay between successive redrawing
int i;                                // index
Dimension size;                       // size of display area
Image oIm;                            // off-screen image for double-buffering
Graphics oGC;                         // graphics context to draw off-screen
Font font;                            // font used to display text
FontMetrics fm;                       // various dimensions of the font
int textHeight;                       // height of the characters
int baseline;                         // baseline for the characters
Color colors[] = new Color[10];       // colors for characters
Thread animThread = null;             // thread to animate text 

public void init() {
size = getSize();    
message = getParameter("text");
if (message == null) message = "Mathematics";

String p = getParameter("delay");
if(p != null) delay = Integer.valueOf(p).intValue();
if(delay < 10) delay = 100;

Graphics g = getGraphics();
int textSize = size.height-8; 
int maxWidth = size.width - (message.length() +1)*4 - 8;
int width = maxWidth+1;
while(width > maxWidth) {
    font = new Font("Helvetica", Font.BOLD, 16);
    g.setFont(font);
    fm = g.getFontMetrics();
    width = fm.stringWidth(message);
    if(width > maxWidth) { textHeight--; }
}
baseline = size.height - fm.getMaxDescent();
for (i=0; i<10; i++) { colors[i] = new Color(15*i,80,255-15*i); }
oIm = createImage(size.width, size.height);
oGC = oIm.getGraphics();
}

public void start() {
  if(animThread == null) { 
     animThread = new Thread(this); 
     animThread.start(); 
  }
}

public void stop() {
if(animThread != null) {
    animThread.stop();
    animThread = null;
}
}

public void update(Graphics g) { paint(g); }

public void paint(Graphics g) {
Color bgColor = new Color(0,0,0); 

oGC.setColor(bgColor);
oGC.setFont(font);
oGC.fillRect(0,0,size.width,size.height);

int x=0, y;
for(int i=0; i<message.length(); i++) {
    x += (int)(Math.random()*3);
    y = baseline - (int)(Math.random()*3);
    Color color;
    int j = (int)((Math.random()+0.1)*9);
    color = colors[j];
    oGC.setColor(color);
    String s = message.substring(i,i+1);
    oGC.drawString(s,x,y);
    x += fm.stringWidth(s);
 }
 g.drawImage(oIm,0,0,this);
 }

public void run() {
  while(true) {
     try { repaint(); Thread.sleep(delay); } 
     catch(InterruptedException e) {stop();}
  }
}

}

