import java.awt.*;

public class RLine extends java.applet.Applet
    implements Runnable {
   
   int strlen, bgcolor, speed;
   Thread th;
   Color colors[];
   int phase = 0;
   Image img;
   Graphics gr;
   float h;
   int s;

  public void init() {
    bgcolor = Integer.valueOf(getParameter("bgcolor")).intValue();
    speed = Integer.valueOf(getParameter("speed")).intValue();
    
    setBackground(new Color(bgcolor));
    strlen = size().width;
    colors = new Color[strlen];
    for (int i = 0; i < strlen; i++) {
      h = ((float)i)/((float)strlen);
      colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f));
      }
    img = createImage(this.size().width,this.size().height);
    gr = img.getGraphics();
    gr.setColor(getBackground());
    gr.fillRect(0, 0, size().width, size().height);
    
  }

  public void start() {
    if(th == null) {
      th = new Thread(this);
      th.start();
    }
  }

  public void stop() {
    if (th != null)
      th.stop();
    th = null;
  }

  public void run() {
    
    while (th != null) {
      super.repaint();
    try {
	Thread.sleep(speed);
	}
	  catch (InterruptedException e) {}	  
    }
  }

  public void update(Graphics g) {
    phase--;
    if (phase < 0)
      phase=strlen-1;
    for(int i=0;i<strlen;i+=16)
     {
      gr.setColor(colors[(phase+i)%strlen]); // värvit liikkeelle
      gr.fillRect(s, 0, 10, size().height);
      s += 15;
      if (s >= size().width) s = 0;
     }
    paint(g);
  }

  public void paint(Graphics g) {
     g.drawImage(img, 0, 0, size().width, size().height, this);
  }

  
}

