FileDocCategorySizeDatePackage
WalkingText.javaAPI DocExample2356Sun Dec 27 15:20:36 GMT 1998None

WalkingText

public class WalkingText extends Applet implements Runnable

Fields Summary
protected String
mesg
protected int
xloc
protected int
yloc
protected int
width
protected int
height
protected int
textWidth
protected int
textHeight
protected Thread
t
protected boolean
done
protected int
napTime
How long to nap for each move
Constructors Summary
Methods Summary
public java.lang.String[][]getParameterInfo()

		String[][] params = {
			{ "text", "text", "Text to walk across the screen" },
			{ "fontName", "text", "Name of font to display in" },
			{ "fontsize", "int", "How big to make the text" },
		};

		return params;
	
public voidinit()
Applet Initializer


	   
	   
		xloc = 0;
		width = getSize().width;
		height = getSize().height;

		if ((mesg = getParameter("text")) == null)
			mesg = "Hello World of Java";

		String pSize = getParameter("fontsize"); 
		if (pSize == null)
			pSize = "12";

		String fontName = getParameter("fontName"); 
		if (fontName == null)
			fontName = "Helvetica";

		// System.out.println("Font is " + pSize + " point " + fontName);
		Font f = new Font(fontName, Font.PLAIN, Integer.parseInt(pSize));
		setFont(f);

		FontMetrics fm = getToolkit().getFontMetrics(f);
		textWidth = fm.stringWidth(mesg);
		textHeight = fm.getHeight();
		// System.out.println("TextWidth " + textWidth + ", ht " + textHeight);

		// use textHeight in y coordinate calculation
		yloc = height - ((height-textHeight) / 2);
	
public voidpaint(java.awt.Graphics g)
Paint is called by Applet to redraw the screen

		g.drawString(mesg, xloc, yloc);
		// if ((xloc + textWidth) > getSize().width) {
		// 	int negLoc = textWidth-(width-xloc);
		// 	System.out.println("xloc, textWidth, negLoc: " + xloc + "," +
		// 			textWidth + ", " + negLoc);
		// 	g.drawString(mesg, negLoc, yloc);
		// }
	
public voidrun()
Run is called by the Thread class when there is work to do

		while (!done) {
			if ((xloc+=5) > getSize().width)
				xloc = 0;
			repaint();
			try {
				Thread.sleep(napTime);
			} catch (Exception e) {
				System.out.println("Who dares to interrupt my Sleep()? " + e);
			};
		}
	
public voidstart()
create the thread and start it.

		if (t == null)
			t = new Thread(this);
		done = false;
		t.start();
	
public voidstop()
This is important: we create a thread, so we must kill it

		done = true;
		t = null;