import java.applet.Applet;
import java.awt.*;

/*
*Application/applet class
*/
public class CrazyTxt extends Applet{

String text ="Java"; // string to be bounced
int delta = 5;  //craziness factor max pixel offset
String fontName = "Comic Sans Script";
int fontSize = 36;

char chars[];    //individual chars in 'text'
int positions[]; // base horizontal position for start of each char
FontMetrics fm;

/*
* called when applet is loaded
*/
public void init() {

int fontStyle = Font.ITALIC + Font.BOLD;
setFont(new Font(fontName, fontStyle, fontSize));
fm = getFontMetrics(getFont());

chars = new char[text.length()];
text.getChars(0, text.length(), chars, 0);

positions = new int[text.length()];
for (int i = 0; i < text.length(); i++){
     positions[i] = fm.charsWidth(chars, 0, i) + 20;
    }
}

/*
* draws the characters and forces a repaint 100ms later
*/

public void paint (Graphics g) {
  int x, y;

  setBackground(Color.red);
  g.setColor (new Color((float) Math.random(),
             (float) Math.random(),
             (float) Math.random()));

  for (int i = 0; i < text.length(); i++ ){
        x = (int)(Math.random() * delta * 2) + positions[i];
        y = (int)(Math.random() * delta * 2) + fm.getAscent() -1;
         g.drawChars (chars, i, 1, x, y);
        }
     repaint (800);
}

/*
* eliminate default update method to get smearing effect
*/

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

/*
* application entry point, create new window with applet inside it
*/

public static void main (String args[]) {

  Frame f = new Frame ("Crazy");
  CrazyTxt crazy = new CrazyTxt ();

  f.add ("Center", crazy);
  f.show ();
  crazy.init ();
}
}

