/*
 *  @# ImageSetViewerApplet.java 1.0 99/10/20
 * */
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import java.applet.*;
import java.net.*;
public class ImageSetViewerApplet extends Applet implements Runnable{
    static int viewerWid = 400;
    static int viewerHt = 300;
    private String[] fileList;
    private int numImages=0;
    private Image currentImage;
    private int currentIndex =0;
    private Image offScrImage;
    private Graphics offScrGc;
    private Thread mainThread;

    public void init() {
       String param = getParameter("IMAGENAMES");
       getFileNamesFromHTML(param);
       param = getParameter("WIDTH");
       if(param != null) viewerWid = Integer.valueOf(param).intValue();
       param = getParameter("HEIGHT");
       if(param != null) viewerHt = Integer.valueOf(param).intValue();
       initApp();
    }

    public void start(){ mainThread.start(); }

    public void run() { displayImage(); }

    public void dispose(){
       if(currentImage != null) currentImage.flush();
    }

    private void getFileNamesFromHTML(String param){
       StringTokenizer st = new StringTokenizer(param, ",");
       int size = st.countTokens();
       if(size <=0) return;
       fileList = new String[size];
       int i=0;
       while(st.hasMoreTokens()){
          fileList[i++] = st.nextToken();
       }
       numImages = fileList.length;
    }

    public void initApp() {
       this.setSize(viewerWid, viewerHt);
       enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK |
                          AWTEvent.MOUSE_EVENT_MASK);
       createOffScreenImage();
       mainThread = new Thread(this);
    }

    //Creates an off screen image
    private void createOffScreenImage(){
       Rectangle rect = getBounds();
       viewerWid = rect.width;
       viewerHt = rect.height;
       offScrImage = this.createImage(rect.width, rect.height);
       offScrGc = offScrImage.getGraphics();
       offScrGc.setColor(Color.black);
       offScrGc.fillRect(0,0,rect.width, rect.height);
    }

    //To overcome Netscape getResource() bug, use this method to load images from jar in applets.
    public  Image loadAnImage(String name){
       try{
          InputStream is = this.getClass().getResourceAsStream(name);
          ByteArrayOutputStream bout = new ByteArrayOutputStream();
          int aByte;
          while((aByte = is.read()) >= 0) bout.write(aByte);
          return Toolkit.getDefaultToolkit().createImage(bout.toByteArray());
       }  catch (Exception e){ return null;}
    }

    public Image loadFromJar(String name){
       try{
          URL url = this.getClass().getClassLoader().getResource(name);
          return Toolkit.getDefaultToolkit().createImage(((ImageProducer)(url.getContent())));
       }  catch (Exception e){ return null;}
    }

    public boolean imageUpdate(Image img,
                             int infoflags,
                             int x,int y,
                             int width,int height){
       if((infoflags & ImageObserver.ERROR) != 0){return false;}
       if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0) {
          repaint();
          return false;
       }
       return true;
    }

    public void displayImage(){
       currentImage = loadAnImage(fileList[currentIndex++ % numImages]);
       repaint();
    }

    public void paint(Graphics gc){
       Rectangle rect = getBounds();
       if(currentImage != null)
          gc.drawImage(currentImage, 0,0, rect.width, rect.height, this);
    }

    public void update(Graphics gc){
       Rectangle rect = getBounds();
       if(currentImage != null)
          gc.drawImage(currentImage, 0,0, rect.width, rect.height, this);
    }

    public void  processMouseEvent(MouseEvent e){
       switch(e.getID()){
          case MouseEvent.MOUSE_PRESSED:
             setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
             displayImage();
             break;
          case MouseEvent.MOUSE_CLICKED:
             break;
          case MouseEvent.MOUSE_RELEASED:
             setCursor(Cursor.getDefaultCursor());
             break;
       }
    }
    class  ExtensionFilter implements FilenameFilter{
      private String extString;
      public ExtensionFilter(String ext){ extString = ext;}
      public boolean accept(File dir, String name){
  	 StringTokenizer st = new StringTokenizer(extString, ",");
         while(st.hasMoreTokens()){
             if(name.endsWith(st.nextToken())){
                return true;
             }
         }
         return false;
      }
    }
}
