import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class RBannerText3 extends Applet implements Runnable {
	
String text = new String();
String[] lines;           
int[] linewidths;
int i, y, x, a, line, sleep;
int lineascent, numlines;          
FontMetrics fm;
Graphics gr;
Dimension d = new Dimension();
Image image;
Thread th;
int fontsize1, fontsize2, fontstyle, bgcolor,fontcolor1, fontcolor2;
String fontname;
Font font1, font2;
	
public void init(){

line = Integer.valueOf(getParameter("line")).intValue();
  
sleep = Integer.valueOf(getParameter("sleep")).intValue();
  
fontsize1 = Integer.valueOf(getParameter("fontsize1")).intValue();
fontsize2 = Integer.valueOf(getParameter("fontsize2")).intValue();
fontstyle = Integer.valueOf(getParameter("fontstyle")).intValue();
fontname = getParameter("fontname");

fontcolor1 = Integer.valueOf(getParameter("fontcolor1")).intValue();
fontcolor2 = Integer.valueOf(getParameter("fontcolor2")).intValue();

bgcolor = Integer.valueOf(getParameter("bgcolor")).intValue();
setBackground(new Color(bgcolor));

d = size();
image = createImage(d.width, d.height);
gr = image.getGraphics();

fm = this.getToolkit().getFontMetrics(this.getFont());
lineascent = fm.getAscent();

font1 = new Font(fontname, fontstyle, fontsize1);
font2 = new Font(fontname, fontstyle, fontsize2);

  
}

public void start()
 {
  if (th == null) {
  th = new Thread(this);
  th.start(); }
 }

 public void run()
  {
   
   while (true)
   {
   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);  
   text = getParameter("text"+a);     
   repaint();
   
         
   try {				// viive
     th.sleep(sleep); }
     catch (InterruptedException e) {}
     
     }
   }

public void stop()
  {
   th = null;
   }
		
	
public void update(Graphics g)
{
gr.setColor(getBackground());          //tyhjä kuva
gr.fillRect(0, 0, d.width, d.height);

StringTokenizer t = new StringTokenizer(text, "|");
numlines = t.countTokens();
lines = new String[numlines]; 
linewidths = new int[numlines];

for(i = 0; i < numlines; i++) lines[i] = t.nextToken();
 
 y = lineascent + (d.height - numlines * fontsize2)/2;
 
 gr.setColor(new Color(fontcolor1));
 
 for(i = 0; i < numlines; i++, y += fontsize2)
 {
 gr.setFont(font1);
 linewidths[i] = getFontMetrics(font1).stringWidth(lines[i]);
 x = (d.width - linewidths[i])/2; 

 if (i > 0) { gr.setFont(font2);
 
 linewidths[i] = getFontMetrics(font2).stringWidth(lines[i]);
 x = (d.width - linewidths[i])/2;
 } 
 gr.drawString(lines[i], x, y);
 gr.setColor(new Color(fontcolor2));
 }
 a++; //vaihtaa text
 if (a == line) a = 0;

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;
 }

}



