import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;

public class Images5a extends Applet implements Runnable 
{
 int index = 0, sleep, speed, i, load, n;                
 Image[] image;
 Thread th;
 boolean b = false;
 int[] num;
 Graphics g = getGraphics(), gr;
 FontMetrics fm;
 Font font;
 MediaTracker tracker;
 int image_pixels[], work_pixels[], image_w, image_h;
 int step;
 int pixel, field_end;
 PixelGrabber grabber;
 Image upd_image;
 int X, Y, fontsize, fontstyle, ints[];
 String text, fontname;
 Color color, bgcolor;
    
public void init()
{
 sleep = Integer.parseInt(getParameter("sleep"));
 speed = Integer.parseInt(getParameter("speed"));
 
 step = Integer.parseInt(getParameter("step"));
 load = Integer.parseInt(getParameter("load_speed"));

 fontsize = Integer.parseInt(getParameter("load_fontsize"));
 fontstyle = Integer.parseInt(getParameter("load_fontstyle"));
 fontname = getParameter("load_fontname");

 ints = parseInt(getParameter("load_color"), ",");
 color = new Color(ints[0], ints[1], ints[2]);

 ints = parseInt(getParameter("bgcolor"), ",");
 bgcolor = new Color(ints[0], ints[1], ints[2]);

 tracker = new MediaTracker(this);

 for (int i = 0; ;i++)
  {
   if (getParameter("image"+i) == null)
   {
      num = new int[i];

      break;
   }
 }
 image = new Image[num.length];
 
 for(int i = 0; i < image.length; i++) 
 {
  image[i] = getImage(getDocumentBase(), getParameter("image"+i));
  tracker.addImage(image[i], i);
  prepareImage(image[i], this); 
 }

 image_w = getSize().width;
 image_h = getSize().height;
 pixel = image_w*image_h;
    
 image_pixels = new int[pixel];
 work_pixels = new int[pixel];

 setBackground(bgcolor);
 font = new Font(fontname, fontstyle, fontsize);
 fm = getFontMetrics(font);

 upd_image = createImage(getSize().width, getSize().height);
 gr = upd_image.getGraphics();

}

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 update(Graphics g) 
{
 g.drawImage(upd_image, 0, 0, getSize().width, getSize().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 destroy()
 {
  th.destroy(); g.dispose(); gr.dispose();
    
 }

public void run() 
{
 Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

   while(true) 
   {
   if(b) {
        
   image[index] = getImage(getDocumentBase(), getParameter("image"+index));
  
   grabber = new PixelGrabber(image[index],0,0,image_w,image_h,
                                              image_pixels,0,image_w);
   try{ grabber.grabPixels(); } catch(InterruptedException e){}

   for (n = 0; n <= image_h; n += step)
   {
    NextFrame();

    try { Thread.sleep(speed); } catch (InterruptedException e) {}

    image[index] = createImage(new MemoryImageSource(image_w, image_h,
                  work_pixels, 0, image_w));

    prepareImage(image[index], this);

    gr.drawImage(image[index], 0, 0, this);

    System.gc();
    repaint();
  }
 
index++;

try { Thread.sleep(sleep); } catch (InterruptedException e) {}

if (index >= image.length) index = 0;
  
repaint();
        }
         else {

  for (int i = 1; ; i++)
  {
  text = getParameter("load_text") + i +" / " + image.length;

  X = (getSize().width - getFontMetrics(font).stringWidth(text))/2;
       // X ja Y keskitävät textiä
  Y = (int)(getSize().height/2);

  field_end = text.indexOf("/");
  gr.setColor(getBackground());
  gr.fillRect(X+fm.stringWidth(text.substring(0, field_end-2)), Y-fm.getAscent(), 
                    fm.getAscent()-fm.getDescent(), fm.getAscent());
  
  gr.setColor(color);
  gr.setFont(font);
    
  gr.drawString(text, X, Y);

  try { Thread.sleep(load); tracker.waitForID(i); } 
         catch (InterruptedException e) {}  
  if (i >= image.length ) { b = true; break; }
  repaint();
  } 
               }
   }
}

 public void NextFrame()
  {
   int x = image_h/2-n/2;
   
   for (int p = pixel - image_h; p >= 0; p -=image_h)

     System.arraycopy((Object)image_pixels, x+p, (Object)work_pixels,
     x+p, n); // kopioi image_pixels -- work_pixels :in

 
     
  }

}
 

