import java.awt.*;
import java.applet.*;

public class RFade extends Applet implements Runnable
{
 String text = new String();
 int a, x, y, line;
 Thread th;
 int ftr = 0, ftg = 0, ftb = 0;
 Font font;
 Graphics gr;
 Dimension d = new Dimension();
 Image image;
 boolean maxxed = false;
 int fontstyle, fontsize;
 String fontname;
 
 public void init()
 {
  d = size();
  image = createImage(d.width, d.height);
  gr = image.getGraphics();
  fontsize = Integer.valueOf(getParameter("fontsize")).intValue();
  fontstyle = Integer.valueOf(getParameter("fontstyle")).intValue();
  fontname = getParameter("fontname");
  line = Integer.valueOf(getParameter("line")).intValue();
  setBackground(Color.black);
 }
 public void start()
 {
  if (th == null) {
  th = new Thread(this);
  th.start(); }
  }

  public void stop()
  {
   th = null;
   }

 public void run()
  {
   int slp;
   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

   while (true)
   {
    text = getParameter("text"+a);
    slp = ChangeColors();
     
    repaint();
   try {				// viive
     th.sleep(10 * slp); }
     catch (InterruptedException e) {}
     
    }
   }

public void update(Graphics g)
 { gr.setColor(getBackground());          //tyhjä kuva
   gr.fillRect(0, 0, d.width, d.height);
 
 
 font = new Font(fontname, fontstyle, fontsize);

 gr.setFont(font);
      
    
   
 gr.setColor(new Color(ftr, ftg, ftb));
  
 x = (bounds().width - getFontMetrics(font).stringWidth(text))/2;
       //keskitä tekstiä
 y = (int)(bounds().height/1.5);   
 gr.drawString(text, x, y);  // tulostaa teksti
 
 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 int ChangeColors()
{
 int pause = 1;
 if (!maxxed)
  { ftr++; ftg++; ftb++;
 if (ftr == 255 && ftg == 255 && ftb == 255)
 { maxxed = true;
   pause = 1; }
 }
else
{ ftr--; ftg--; ftb--;
 if (ftr == 0 && ftg == 0 && ftb == 0)
 { maxxed = false;
   pause = 1;
   Next(); }
 }
 return pause;
}

public void Next()
{ a++;
  if (a == line) a = 0;
}

}
