WalkingTextpublic 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 | napTimeHow long to nap for each move |
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 void | init()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 void | paint(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 void | run()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 void | start()create the thread and start it.
if (t == null)
t = new Thread(this);
done = false;
t.start();
| public void | stop()This is important: we create a thread, so we must kill it
done = true;
t = null;
|
|