import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class Filter4 extends Applet implements Runnable
 {
  Image[] image;
  Image img;
  Thread th;
  int i, index, sleep, c;
  String next;
  String col;
  Graphics gr;
  Dimension d = new Dimension();
  Image upd_image;
  Color color;
  MediaTracker tracker;
  int[] ints;

public void init()
{
 tracker = new MediaTracker(this);
 image = new Image[1000];
 
 for (i = 0; (next = getParameter("image" + i)) != null; i++)
 {
 image[i] = getImage(getDocumentBase(), next);
 tracker.addImage(image[i], i);
 showStatus("Loading images..." + i);
 try { tracker.waitForAll(); } catch (InterruptedException e) {}
 }
 sleep = Integer.parseInt(getParameter("sleep"));
  
 d = getSize();
 upd_image = createImage(d.width, d.height);
 gr = upd_image.getGraphics();
 
 
}
  
public void update(Graphics g)
  {
    gr.drawImage(img, 0, 0, d.width, d.height, this);
    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 start()
 {
  if (th == null) {
  th = new Thread(this);
  th.start(); }
 }

public void stop()
{
 th = null;
}

public void run() 
{
 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
   
 while(true) 
   {
    while ((col = getParameter("color" + c++)) == null) c = 0;
    ints = parseInt((col), ",");

    color = new Color(ints[0],ints[1],ints[2]);

    while ((next = getParameter("image" + index++)) == null) index = 0;  
    image[index] = getImage(getDocumentBase(), next);
        
    FilterImage();
    repaint();
    try { Thread.sleep(sleep); } catch (InterruptedException e) {}
   }
}

int[] parseInt(String s, String sep) 
    {

	StringTokenizer st = new StringTokenizer(s, sep);
        int[] result = new int[st.countTokens()];

	for (i=0; i<result.length; i++) {
            result[i] = Integer.parseInt(st.nextToken());
	}
        return result;
    }

public void FilterImage()
{
 img = createImage(new FilteredImageSource(image[index].getSource(),
      new ShadowFilter(getSize().width, getSize().height, color)));
 repaint();
}
}

class ShadowFilter extends RGBImageFilter {
  Color color;
  double edge;

  public ShadowFilter(int width, int height, Color mask) {
    edge = Math.sqrt(width*width + height*height); color = mask;
  }

  public int filterRGB(int x, int y, int pixel) {

    double fraction = 1.0 - Math.sqrt(x*x + y*y)/edge;
    if (fraction <= 0) return 0xFF000000;
    
    int red = (((pixel & 0xff0000) >> 16) * color.getRed()/255);
    
    int green = (((pixel & 0xff00) >> 8) * color.getGreen()/255);
    
    int blue = (pixel & 0xff) * color.getBlue()/255;

    int r = (int) (red * fraction);
    int g = (int) (green * fraction);
    int b = (int) (blue * fraction);

    return (0x000000FF << 24) | (r << 16) | (g << 8) | b;
   }
}




