/* RandomName applet by James Inge.
 *
 * 31/10/97
 *
 * This is passed a string of names, seperated by commas, in the parameter "names"
 * ie.
 *   <APPLET CODE="RandomName.class" WIDTH=60 HEIGHT=17>
 *      <PARAM NAME="names" VALUE="Tom, Dick, Harry">
 *   </APPLET>
 */
 
import java.awt.Graphics;
import java.util.Random;
import java.awt.Event;

public class RandomName extends java.applet.Applet implements Runnable {

// Runnable lets me multithread, which makes the refresh work better.

	Thread runner;
	static String[] names;
	static int howManyNames;

	public void start() {
		if (runner == null) {
			runner = new Thread(this);
			runner.start();
		}
	}

	public void stop() {
		if (runner != null) {
			runner.stop();
			runner = null;
		}
	}

	public void run() {
//	While running, refresh ever 2 secs.
		
		while (true) {
			repaint();
			try{ Thread.sleep(2000); }
			catch (InterruptedException e) { }
		}
	}

	public void init() {
//	Set up an array, and separate out the names into it.
		
		int i = 0;
		int pos = 0;
		String list = getParameter("names");
		String[] names = new String[Math.round(list.length() / 2)];
		
		names[0] = "";
		for( i=1; i < list.length() +1; i ++) {
			if( list.substring(i-1, i).equals(",")) {
				pos ++;
				names[pos] = "";
			}
			else {
				if( list.substring(i-1, i).equals(" ")) {
				}
				else {
					names[pos] += list.substring(i-1, i);
				}
			}
		}
		RandomName.names = names;
		RandomName.howManyNames = pos + 1;
	}
	
	public void paint(Graphics g) {
//	Refresh the screen, painting a random name from our array.
		
		Random rd = new Random();

		g.drawString(names[(int) (rd.nextFloat() * (float) RandomName.howManyNames)], 1,15);
	}

	public boolean mouseExit(Event evt, int x, int y) {
//	Change the display if the mouse goes nearby.		
		repaint();
		return true;
	}
}

