import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class FilterBannerText extends Applet implements Runnable 
{	
String text = new String();
String[] lines;           
int i, y, x, a=1, speed, index, u;
int x1, y1, w, h; //CropImageFilter coords.
int numlines;          
Graphics gr, g;
Dimension d = new Dimension();
Image upd_image;
Thread th;
int fontsize1, fontsize2, fontstyle;
String fontname;
Font font1, font2;
Color filtercolor, fontcolor1, fontcolor2;
Image image, img, filter;
MediaTracker tracker;
int[] ints;
FontMetrics fm;
int border_size;
	
public void init()
{
  img = getImage(getDocumentBase(), getParameter("image"));
  border_size = Integer.parseInt(getParameter("bordersize"));
 
  tracker = new MediaTracker(this);
  ints = parseInt(getParameter("filter_color"), ",");
  filtercolor = new Color(ints[0], ints[1], ints[2]);

  ints = parseInt(getParameter("font_color_1"), ",");
  fontcolor1 = new Color(ints[0], ints[1], ints[2]);  
  
  ints = parseInt(getParameter("font_color_2"), ",");
  fontcolor2 = new Color(ints[0], ints[1], ints[2]);  

  StringTokenizer crop = new StringTokenizer(getParameter("crop"), ",");
  x1 = Integer.parseInt(crop.nextToken());
  y1 = Integer.parseInt(crop.nextToken());
  w = Integer.parseInt(crop.nextToken());
  h = Integer.parseInt(crop.nextToken());

  image = createImage(new FilteredImageSource(img.getSource(),
  new CropImageFilter(x1, y1, w, h)));

  filter = createImage(new FilteredImageSource(image.getSource(),
      new CFilter(filtercolor)));
  
  tracker.addImage(filter, 1);
  tracker.addImage(img, 1);
  
  speed = Integer.parseInt(getParameter("speed"));
   
  fontsize1 = Integer.parseInt(getParameter("font_size_1"));
  fontsize2 = Integer.parseInt(getParameter("font_size_2"));
  fontstyle = Integer.parseInt(getParameter("font_style"));
  fontname = getParameter("font_name");
  font1 = new Font(fontname, fontstyle, fontsize1);
  font2 = new Font(fontname, fontstyle, fontsize2);
  fm = getFontMetrics(font2);

 
  d = getSize();
  upd_image = createImage(d.width, d.height);
  gr = upd_image.getGraphics();
  g = getGraphics();

  try { tracker.waitForID(1); }
  catch (InterruptedException e) {}

  y = y1 + h + fm.getDescent();
}

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 run()
  {
   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
   while (true)
   {
    while ((text = getParameter("text" + a)) == null) a = 1;
  
    y--;
    
    try{ th.sleep(speed);} catch(InterruptedException e){}

    repaint();
   
     }

   }

public void stop()
  {
   th = null;
   }

public void destroy()
  {
   th.destroy(); g.dispose(); gr.dispose(); 
   }
		
	
public void update(Graphics g)
{
gr.drawImage(img, 0, 0, this);
gr.drawImage(filter, x1, y1, w, h, this);

gr.setColor(filtercolor);
for(int u = 0; u < border_size; u++)
gr.draw3DRect(x1+u, y1+u, w-2*u-1, h-2*u-1, u<border_size/2);
gr.clipRect(x1+border_size,y1+border_size,w-2*border_size,h-2*border_size);
readText();

g.drawImage(upd_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 readText()
{
 StringTokenizer t = new StringTokenizer(text, getParameter("enter"));
 numlines = t.countTokens();
 lines = new String[numlines]; 
 
 for(i = 0; i < numlines; i++) lines[i] = t.nextToken();

 u = fontsize2;
 for(i = 0; i < numlines; i++)
   {
 gr.setColor(fontcolor1);
 gr.setFont(font1);
 
 x = x1+border_size+5;
  
    if (i > 0) 
     { 
     gr.setColor(fontcolor2);
     gr.setFont(font2);
 
     u += fontsize2;
     x = x1+border_size+20;
     }
 gr.drawString(lines[i], x, y + u);

  }
if (y < y1-(numlines*fm.getHeight()))
  {
   a++; y = y1 + h + fm.getDescent();
  }  	     
}


}

class CFilter extends RGBImageFilter {
	Color color;

     public CFilter(Color mask) {
		color = mask;
		canFilterIndexColorModel = true;
	}
	public int filterRGB(int x, int y, int pixel) {

return 255 << 24 |
       (((pixel & 0xff0000) >> 16) * color.getRed()/255) << 16 |
       (((pixel & 0xff00) >> 8) * color.getGreen()/255) << 8 |
       (pixel & 0xff) * color.getBlue()/255 ;
	}
}
