import java.awt.*;
import java.applet.*;
import java.util.*;

public class D3Text extends Applet implements Runnable
{
 String text = new String();
 Color colors[] = new Color[14];
 int index, a, z, x, y;
 long speed_delay, sleep_delay, start, sleep, speed;
 int fontstyle, fontsize;
 Color bgcolor;
 Font font;
 Thread th;
 String fontname, about;
 float ftr, ftg;
 Image image;
 Graphics gr, g;
 Dimension d = new Dimension();
 
 public void init()
{
 fontsize = Integer.parseInt(getParameter("fontsize"));
 fontstyle = Integer.parseInt(getParameter("fontstyle"));
 fontname = getParameter("fontname");

 sleep_delay = Integer.parseInt(getParameter("sleep_delay"));
 speed_delay = Integer.parseInt(getParameter("speed_delay"));

 font = new Font(fontname, fontstyle, fontsize);

 about = getParameter("author Rain Ausmaa|rainex@sgic.fi"); //pieni suojaus
 if (about == null) th.start();
 
 int[] ints;
 ints = parseInt(getParameter("bgcolor"), ",");
 bgcolor = new Color(ints[0], ints[1], ints[2]);

 setBackground(bgcolor);

  d = size();
  image = createImage(d.width, d.height);
  gr = image.getGraphics();

  start = System.currentTimeMillis();
  sleep = sleep_delay - (System.currentTimeMillis() - start);
  speed = speed_delay - (System.currentTimeMillis() - start);

        
}

int[] parseInt(String s, String sep) 
    {
	StringTokenizer st = new StringTokenizer(s, sep);
        int[] result = new int[st.countTokens()];

	for (int i=0; i<result.length; i++) {
            result[i] = Integer.parseInt(st.nextToken());
	}
        return result;
    }

public void start()
 {
  if (th == null) {
  th = new Thread(this);
  th.start(); }
 }

public void stop()
 {
  th = null;
 }

  public void run()
  {
   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
   
   while (true)
   {
    gr.setColor(getBackground());          //tyhjä kuva
    gr.fillRect(0, 0, d.width, d.height);
    while ((text = getParameter("text"+a)) == null) a = 0;

 
  x = (d.width - getFontMetrics(font).stringWidth(text))/2;
       //keskitä tekstiä
  y = (int)(d.height/1.5);
  
  gr.setFont(font);
  
  for (z = 0; z < 7; z++)
   {
      
    colors[0] = new Color(R(), 0.0f, 0.0f); //red
    colors[1] = new Color(0.0f, R(), 0.0f); //green
    colors[2] = new Color(0.0f, 0.0f, R()); //blue
    colors[3] = new Color(R(), 0.0f, R());  //violet
    colors[4] = new Color(R(), R(), 0.0f);  //yellow
    colors[5] = new Color(0.0f, R(), R());  //lightblue
    colors[6] = new Color(R(), R(), R());   //white
    colors[7] = new Color(R(), G(), 0.0f);  //orange
    colors[8] = new Color(G(), G(), 0.0f);  //808000
    colors[9] = new Color(R(), G(), G());   //FF8080
    colors[10] = new Color(G(), G(), R());  //8080FF
    colors[11] = new Color(G(), R(), G());  //80FF80
    colors[12] = new Color(0.0f, G(), G()); //008000
    colors[13] = new Color(0.0f, G(), 0.0f);//0080FF
     
    gr.setColor(colors[index]);
  
    gr.drawString(text, x++, y++);
    	try{		// nopeus
	Thread.sleep(speed);
	   } catch(InterruptedException e){}
    repaint();
   }
    
    index++;		// vaihtaa väri
    if (index == colors.length) index = 0;

    a++;		// vaihtaa text
    
    repaint();
    
    try{		
       Thread.sleep(sleep);
       } catch(InterruptedException e) {}
    	
    }
   }

public void update(Graphics g){
  g.drawImage(image, 0, 0, d.width, d.height, this);
	                      }

public boolean imageUpdate(Image image, int info, int x,
	 int y, int width, int height)  // värinätön kuva
  		{ return true; }

public float R()
{
 ftr = ((float)z)/((float)6);
 
 return ftr;
}

public float G()
{
 ftg = (((float)z)*(float)0.5)/((float)6);
 	
 return ftg;
}

}
