//----------------------------------------------------------------------
//
// TickerLights
// Copyright (c)    Robin P. Dunn, Total Control Software
//                  robind@halcyon.com
//
// A simple applet to help me learn Java and Applet programming.  A
// typical ticker-tape text message, with flickering Christmas lights
// around the border.
//
// Here is an example HTML to load the applet.  Notice how the
// parameters are used to set the various attributes of the applet.
// 
// <applet code=TickerLights.class width=100% height=80>
//     <param name="text"      value="Merry Christmas!">
//     <param name="textPos"   value=54>
//     <param name="bgColor"   value="008800">
//     <param name="fgColor"   value="EE0000">
//     <param name="fontName"  value="Helvetica">
//     <param name="fontSize"  value=34>
// </applet>
//
//
// TODO:
//      Allow multiple text messages, display one on each pass of the text
//      through the ticker.
//
//      Music???
//
//
// License:
//      Anybody can use this applet for whatever they want, You just
//      can't try to claim that it is yours, in original or modified form.
// 
//----------------------------------------------------------------------


import java.awt.Graphics;
import java.applet.Applet;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Color;
import java.lang.System;
import java.lang.Integer;


public class TickerLights extends Applet implements Runnable {
    String  text;
    int     textWidth;
    int     textPos;
    Font    font;
    Color   fgColor;
    Color       colors[] = {
        Color.red, Color.green, Color.white, Color.blue, Color.cyan,
        Color.gray, Color.magenta, Color.orange, Color.pink, Color.yellow
    };
    static int  numColors = 10;
    static int  colorIdx = 0;
    int     xpos;
    Thread  mainThread = null;
    int     delta = -5; 

    public void init() {
        text = getParameter("text");
        textPos = Integer.parseInt(getParameter("textPos"));
        xpos = size().width;
        
        int colorVal = Integer.parseInt(getParameter("bgColor"), 16);
        setBackground(new Color(colorVal));

        colorVal = Integer.parseInt(getParameter("fgColor"), 16);
        fgColor = new Color(colorVal);

        int fontSize = Integer.parseInt(getParameter("fontSize"));
        font = new Font(getParameter("fontName"), 1, fontSize);
        textWidth = this.getFontMetrics(font).stringWidth(text);
    }

    public void paint(Graphics g) {
	int     x, y;
        int     lightSize = 7;
        int     lightSpacing = 18;

        g.setFont(font);
        g.setColor(fgColor);
        g.drawString(text, xpos, textPos);

            // draw the lights on top of the box.
        for (x=5; x<size().width; x+= lightSpacing) {
            g.setColor(nextColor());
            g.fillOval(x,1, lightSize, lightSize);
	}
            // draw the lights on right side of the box.
        for (y=lightSpacing; y<size().height; y+= lightSpacing) {
            g.setColor(nextColor());
            g.fillOval(size().width-lightSize-2,y, lightSize, lightSize);
	}
            // draw the lights on bottom of the box.
        for (x=size().width-lightSpacing; x>0;  x -= lightSpacing) {
            g.setColor(nextColor());
            g.fillOval(x, size().height-lightSize-2, lightSize, lightSize);
	}
            // draw the lights on left side of the box.
        for (y=size().height-lightSpacing; y>0; y -= lightSpacing) {
            g.setColor(nextColor());
            g.fillOval(1,y, lightSize, lightSize);
	}
    }

    
    Color nextColor() {
        if (colorIdx == numColors) {
            colorIdx = 0;
        }
        return colors[colorIdx++];
    }
        

    public void start() {
        if (mainThread == null) {
            mainThread = new Thread(this);
            mainThread.start();
        }
    }

    public void stop() {
        mainThread.stop();
        mainThread = null;
    }

    public void setcoord() {
        xpos += delta;
        if ((delta < 0) && (xpos < -textWidth)) {
	        delta = -delta;
        } 
        else if ((delta > 0) && (xpos > size().width)) {
            delta = -delta;
        }
    }

    public void run() {
        while (mainThread != null) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                break;
            }

            setcoord();
            repaint();
        }
    }

//    public void update(Graphics g) {
//        g.clearRect(0,0, size().width, size().height);
//        paint(g);
//    }
    
}








