import awt.*;
import java.lang.*;
import java.util.*;
import browser.*;
import net.www.html.*;
import Object;
import java.io.*;


class TickerTape extends Applet implements Runnable {
  String fonttype="TimesRoman", messages;     // String that will hold message.
  int palsiz = 90;
  int red=0, green=0, blue=0;  // text color
  Color defcol;                // text color
  int size=20;
  int xdraw;        // Possition to start drawing string pixel wise.
  int pxls;         // The width of the text in pixels.

  Thread back = null;
  Color pal[] = new Color[256];  // Color palette for drawing..
  int q; // current palette index..
  int usepal;  // use rotating palette

  Color HSItoRGB(double h, double s, double i) {
    double rv, gv, bv;
    double PI, t;
    Color col;
    int r, g, b;
 
    PI=3.1415926;

    t = 2 * PI * h;
    rv=1.0 + s * Math.sin(t - 2.0 * PI / 3.0);
    gv=1.0 + s * Math.sin(t);
    bv=1.0 + s * Math.sin(t + 2.0 * PI / 3.0);
 
    t=255.9999999 * i / 2;
 
    r = (int) (rv * t);
    g = (int) (gv * t);
    b = (int) (bv * t);
 
    col = new Color(awt.WServer.theServer, r, g, b);
 
    return col;
  }


  public void init () {
    int i; // loop vairable
    double hue;  // temp color variable
 
        if(getAttribute("usepal")!=null) {
          usepal=1;
        }
	if(getAttribute("red")!=null)
	{
		Integer red2=new Integer(getAttribute("red"));
		red=red2.intValue();
	}
	if(getAttribute("green")!=null)
	{
		Integer green2=new Integer(getAttribute("green"));
		green=green2.intValue();
	}
	if(getAttribute("blue")!=null)
	{
		Integer blue2=new Integer(getAttribute("blue"));
		blue=blue2.intValue();
	}
	if(getAttribute("size")!=null)
	{
		Integer size2=new Integer(getAttribute("size"));
		size=size2.intValue();
	}
	if(getAttribute("fnt")!=null)
		fonttype=getAttribute("fnt");


// setup color palette;
       for (i=1; i<(palsiz + 1); i++) {
         hue=(i/(palsiz * 1.0));
         pal[i] = HSItoRGB(hue, 0.95, 1.0);
       }
       pal[0]=Color.lightGray;
       defcol = new Color(awt.WServer.theServer, red, green, blue);

       q=0;

       Font f = getFont("TimesRoman",Font.BOLD,size);
       font = f;
	
       xdraw = width;
       resize(width,2*f.actualHeight);

      messages = getAttribute("msgs");
      // Hopefully one will be able to load messages from a file but
      // until that happend. . . 
      messages = (messages == null) ?
	"TickerTape 1.0 . . .                    \
	. . . By Sven Heinicke  . . .         \
        . . . To use your own message put the following in your HTML: \
      . . .\
        . . . Known bugs: it flickers a bit."
	  : messages;
    }
  public void run () {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while (true) {
       xdraw -= 10;

       // If the string is not being drawn on the displayed portion
       // of the graphics any more start over again.

      if (xdraw < - (pxls + 20)) {
        xdraw = width;
        Thread.sleep (2000);
      }

      repaint ();

      Thread.sleep (100);

      // debuggin info below..
      //  System.out.println ("width:" + width + " xdraw:" + xdraw);

    }
  }

  public void paint (Graphics g) {
    q=(q+1) % palsiz;  // rotate palette...
    if (usepal==1) 
      g.setForeground(pal[(q % palsiz) + 1]);
    else 
      g.setForeground(defcol);

    pxls = g.drawStringWidth (messages, xdraw, size);
  }

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