// simple marquee applet
import java.applet.Applet;
import java.awt.*;

public class simpleMarquee extends Applet implements Runnable {
        String theMessage;
        Font textFont;
        FontMetrics fontMetrics;
        int stepPixels;
        int oldX, oldY, newX, newY;
        int speed; // in milliseconds
        Thread scroller=null;

        // standard applet methods
        public void init()
        {
                theMessage = getParameter("message");
                if (theMessage == null)
                        theMessage = new
                                String("A simple Marquee by Michael Daconta.");

                String speedStr = getParameter("speed");
                if (speedStr == null)
                        speed = 15;
                else
                        speed = Integer.parseInt(speedStr);        

                textFont = new Font("TimesRoman",Font.BOLD, 18);                
                stepPixels = 6;
        }

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

        public void stop()
        {
                if (scroller != null)
                {
                        scroller.stop();
                        scroller = null;
                }
        }

        public void run()
        {
                Thread.currentThread().setPriority(Thread.NORM_PRIORITY-1);
                paint(this.getGraphics());
                while (true)
                        repaint(speed);
        }

        public void paint(Graphics g)
        {
                // initial background
                g.setColor(Color.black);
                g.drawRect(0,0, size().width,
                           size().height); 
                g.setColor(Color.white);
                g.fillRect(1,1, size().width - 1,
                           size().height - 1);
                
                // set the font
                g.setFont(textFont);
                fontMetrics = g.getFontMetrics();

                // set initial X and Y to drawString
                oldX = newX = size().width -
                        fontMetrics.charWidth(theMessage.charAt(0));
                oldY = newY = size().height/2 +
                        (fontMetrics.getHeight()/2);
        }

        public void update(Graphics g)
        {
                g.setFont(textFont);

                // Erase the old
                g.setColor(Color.white);
                g.drawString(theMessage, oldX, oldY);

                // draw the New
                g.setColor(Color.black);
                g.drawString(theMessage, newX, newY);

                // step
                oldX = newX;
                newX -= stepPixels;

                // restart?
                if (newX <= -(fontMetrics.stringWidth(theMessage)
                              - fontMetrics.charWidth(
                                theMessage.charAt(
                                   theMessage.length() - 1))))
                        paint(g);  // start at the beginning
        }

}
