import java.awt.*;
import java.applet.*;

public class RText2 extends Applet implements Runnable
{
 String teksti[] = new String[4];
 Graphics gr;
 Dimension d = new Dimension();
 Image image;
 int a, y, x, X;
 int t, T, S, s = 1;
 int ftkoko, bgr, bgg, bgb, bgcolor;
 Thread th;
 int ftr = 255, ftg = 0, ftb = 0;
 Font fonti;
 FontMetrics fm;

 public void init()
 {
  teksti[0] = getParameter("teksti[0]");
  teksti[1] = getParameter("teksti[1]");
  teksti[2] = getParameter("teksti[2]");
  teksti[3] = getParameter("teksti[3]");
  ftkoko = Integer.valueOf(getParameter("ftkoko")).intValue();
  bgcolor = Integer.valueOf(getParameter("bgcolor")).intValue();
  d = size();
  image = createImage(d.width, d.height);
  gr = image.getGraphics();
  setBackground(new Color(bgcolor));
  fonti = new Font("Arial",Font.BOLD,ftkoko);
  gr.setFont(fonti);
  fm = gr.getFontMetrics();
  
 }
 public void start()
 {
  if (th == null) {
  th = new Thread(this);
  th.start(); }
  }

 public void run()
  {
   x = d.width+fm.stringWidth(teksti[a]) + 50;
   y = (int)(bounds().height/1.5);
    
   while (true)
   {
    gr.setColor(getBackground());          //tyhi pilt
    gr.fillRect(0, 0, d.width, d.height);
    gr.setColor(new Color(ftr, ftg, ftb));
    
    X = (bounds().width - getFontMetrics(fonti).stringWidth(teksti[a]))/2;
    gr.drawChars(teksti[a].toCharArray(), t, s, x, y);
     x -= 5;					// vieritää tekstiä
     if (x <= X)
     { x = d.width+fm.stringWidth(teksti[a]) + 50;
      t++;
      S++; }
       
    if (S == teksti[a].length())	
     { S = 0;				//montako merkkiä tulostetaan
       t = 0;                		// aloitus index 
         a++;  				// vaihda teksti[a]
  ftr = (int)(Math.random()*128);	// satunnaiset fontin värit
  ftg = (int)(Math.random()*128);
  ftb = (int)(Math.random()*128); }
      
   if (a == 4)				
    a = 0;
   gr.drawChars(teksti[a].toCharArray(), T, S, X, y); 
  
   super.repaint(); 
      try {
     th.sleep(50);
       } 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)
 {
  return true;
 }

 public void stop()
  { th = null; }
  
}
