import java.awt.*;
import java.applet.*;

public class RText4 extends Applet implements Runnable
{
 String text[] = {"Best Links", "Game", "Estonia", "Java", "Midi",
     "Security", "Sex", "Soft", "Opas", "Lehdet"};
 int x, y, a = 0;
 Thread th;
 int ftr = 255, ftg = 0, ftb = 0;
 Font font;
 int fonts[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 
                 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52};
 int fontIndex = 0;
 int numFonts = 26;
 Graphics gr;
 Dimension d = new Dimension();
 Image image;
 
 public void init()
 {
  d = size();
  image = createImage(d.width, d.height);
  gr = image.getGraphics();
  setBackground(Color.white);
  
 }
 public void start()
 {
  if (th == null) {
  th = new Thread(this);
  th.start(); }
  }

 public void run()
  {
   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);  
       
   while (true)
   {
    gr.setColor(getBackground());          //tyhjä kuva
    gr.fillRect(0, 0, d.width, d.height);
   
    gr.setColor(new Color(ftr, ftg, ftb));
    
    if (fontIndex > numFonts - 1)
       { fontIndex = 0;
      try {
     th.sleep(1000);
       } catch (InterruptedException e) {}  
         a++; 
     ftr = (int)(Math.random()*255);	// satunnaiset värit
     ftg = (int)(Math.random()*255);
     ftb = (int)(Math.random()*255); }

    font = new Font("Arial",Font.BOLD,fonts[fontIndex++]);
    gr.setFont(font);
       
    if (a == 10)
       a = 0;       
    
   x = (size().width - getFontMetrics(font).stringWidth(text[a]))/2; 
           //keskitä tekstiä
   y = (int)(size().height/1.5); 
   gr.drawString(text[a], x, y);  // tulostaa teksti 
   
   super.repaint(); 
      try {
     th.sleep(100);
       } catch (InterruptedException e) {} 
     }
   }

 public void update(Graphics g)
 { g.drawImage(image, 0, 0, d.width, d.height, this); 
      }

 public void stop()
  { th = null; }
  
}
