import java.applet.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;

public class ScrollText2 extends Applet implements Runnable 
{	
String text = new String();
int y, x, a, speed;
int x1, y1, w, h; //CropImageFilter coords.
Graphics gr, g;
Dimension d = new Dimension();
Image upd_image;
Thread th;
int fontsize, fontstyle;
String fontname;
Font font;
Color filtercolor, fontcolor, bordercolor;
Image image, img, filter;
MediaTracker tracker;
int[] ints;
int border;

	
public void init()
{
 img = getImage(getDocumentBase(), getParameter("image"));
  
 tracker = new MediaTracker(this);
 ints = parseInt(getParameter("border_color"), ",");
 bordercolor = new Color(ints[0], ints[1], ints[2]);

 ints = parseInt(getParameter("font_color"), ",");
 fontcolor = new Color(ints[0], ints[1], ints[2]);

 ints = parseInt(getParameter("filter_color"), ",");
 filtercolor = new Color(ints[0], ints[1], ints[2]);

 border = Integer.parseInt(getParameter("border_height"));  

 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"));
 
 fontsize = Integer.parseInt(getParameter("font_size"));
 fontstyle = Integer.parseInt(getParameter("font_style"));
 fontname = getParameter("font_name");
 font = new Font(fontname, fontstyle, fontsize);
    
 d = getSize();
 upd_image = createImage(d.width, d.height);
 gr = upd_image.getGraphics();
 g = getGraphics();
 x = x1+w+5;

 try { tracker.waitForID(1); }
 catch (InterruptedException e) {}

}

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()
  {
   
   while (true)
   {
   Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
   
   while ((text = getParameter("text" + a)) == null) a = 0;
   if(x < x1-getFontMetrics(font).stringWidth(text)) { a++; x = x1+w+5; } 
   
   x--;
   repaint();         
   try {				// speed
     th.sleep(speed); }
     catch (InterruptedException e) {}
     
     }
   }

public void stop()
  {
   th = null;
   }

public void destroy()
  {
   th.destroy(); gr.dispose(); g.dispose();
   }
		
	
public void update(Graphics g)
{
 gr.drawImage(img, 0, 0, this);

 gr.drawImage(filter, x1, y1, w, h, this);

  drawBorder(gr, border);

  gr.setColor(fontcolor);

  gr.setFont(font);
 
  gr.clipRect(x1+1,y1+1,w,h);
    
  y = (int)(y1 + (h/1.6));
  
  gr.drawString(text, x, y);
   
    
 
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 drawBorder(Graphics gr, int b)
{
  gr.setColor(bordercolor);
		
 for (int i=0; i <= b; i++)
 {
  gr.drawLine(x1-i, y1-i, x1+w+i, y1-i);
  gr.drawLine(x1+w+i, y1-i, x1+w+i, y1+h+i);
  gr.drawLine(x1+w+i, y1+h+i, x1-i, y1+h+i);
  gr.drawLine(x1-i, y1+h+i, x1-i, y1-i);
 }
}


}

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 ;
	}
}

	

	
