/*
    Test of text animation.


    Original Code: Daniel Wyszynski 
                   Center for Applied Large-Scale Computing (CALC) 
                   04-12-95 

    kwalrath: Changed string; added thread suspension. 5-9-95

    Damien Miller Added:  27 December 95
                          - adjustable speed
                          - adjustable spacing
                          - adjustable font size
                          - adjustable 'jiggle distance'
                          - adjustable foreground color
                          - adjustable background color

   Parameters:

    text       - Text to be displayed (string, default = "Freakout!")
    speed      - Milliseconds between 'jiggles' (int, default = 35)
    fontsize   - Size of font in points (int, default = 36)
    xsep       - Num of pixels between chars (int, default = 15)
    jiggledist - Max num of pixels to move chars (int, default = 10)
    bgcolor    - Hex value of background color (RRGGBB, default = grey)
    fgcolor    - Hex value of foreground color (RRGGBB, default = white) 

 */

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

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

	char separated[];
	Thread killme = null;
	boolean threadSuspended = false; // added by kwalrath
	String s = null, param = null;
	int col, i, x_coord = 0, y_coord = 0, speed = 35;
	int fontsize = 36, jiggle_dist = 10, sep_dist = 15;

	public void init() {
		s = getParameter("text");
		if (s == null) {
			s = "Freakout!";
		}

		param = getParameter("speed");
		if (param != null) {
			speed = Integer.parseInt(param);
		}

		param = getParameter("xsep");
		if (param != null) {
			sep_dist = Integer.parseInt(param);
		}

		param = getParameter("fontsize");
		if (param != null) {
			fontsize = Integer.parseInt(param);
		}

		param = getParameter("jiggledist");
		if (param != null) {
			jiggle_dist = Integer.parseInt(param);
		}

		param = getParameter("bgcolor");
		if (param != null) {
			col = Integer.parseInt(param, 16);
			setBackground(new Color(col));
		}

		param = getParameter("fgcolor");
		if (param != null) {
			col = Integer.parseInt(param, 16);
			setForeground(new Color(col));
		}

		setFont(new Font("TimesRoman", Font.BOLD, fontsize));

		separated = new char[s.length()];
		s.getChars(0, s.length(), separated, 0);
	}

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

	public void stop() {
		killme = null;
	}

	public void run() {
		while (killme != null) {
			try {
				Thread.sleep(speed);
			} catch (InterruptedException e) {
			}
			repaint();
		}
		killme = null;
	}

	public void paint(Graphics g) {
		for (i = 0; i < s.length(); i++) {
			x_coord = (int) (Math.random() * jiggle_dist + (sep_dist * i));
			y_coord = (int) (Math.random() * jiggle_dist + fontsize);
			g.drawChars(separated, i, 1, x_coord, y_coord);
		}
	}

	/* Added by kwalrath. */
	public boolean mouseDown(java.awt.Event evt, int x, int y) {
		if (threadSuspended) {
			killme.resume();
		} else {
			killme.suspend();
		}
		threadSuspended = !threadSuspended;
		return true;
	}
}


