/**  @# ImageSetViewer1.java 1.0 99/10/20
 * */

 import java.io.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.awt.image.*;
 import java.util.*;

 public class ImageSetViewer1 extends Frame {
    static int MAX_IMAGES = 24;
    static int viewerWid = 400;
    static int viewerHt = 300;
    private String[] fileList;
    private int numImages=0;
    protected Image currentImage;
    int currentIndex =0;
    private Image[] imageSet;
    private String path;

    public ImageSetViewer1(){
       setTitle("Image Set Viewer");
       setSize(viewerWid, viewerHt);
       init();
    }

    public void init() {
       enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK |
                          AWTEvent.MOUSE_EVENT_MASK);
       addWindowListener(
          new WindowAdapter() {
            public void windowClosing(WindowEvent w) {
               dispose();
               System.exit(0);
            }
          }
       );
    }

    //Loads images from given path. First reads the image file names. Then
    //calls readImages to load images.
    public void loadImages(String path, String filter){
       if(path == null) return;
       this.path = path;
       File imgDir= new File(path);
       fileList = readImageFiles(imgDir, filter);
       if(fileList != null) numImages = fileList.length;
    }

    public String[] readImageFiles(File dir, String filter){
       ExtensionFilter efilter = new ExtensionFilter(filter);
       String files[] = dir.list(efilter);
       return files;
    }
    private boolean loadAnImage(int num){
        if((currentImage =
           Toolkit.getDefaultToolkit().getImage(path+fileList[num]))== null)
           return false;
        else return true;
    }


    public boolean imageUpdate(Image img,
                             int infoflags,
                             int x,
                             int y,
                             int width,
                             int height){

       if((infoflags & ImageObserver.ERROR) != 0){
          System.out.println("ERROR in image load or image draw");
          return false;
       }

       if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0) {
          repaint();
          return false;
       }
       return true;
   }

    public void displayImage(){
       loadAnImage(currentIndex++ % numImages);
       repaint();
    }

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

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

    public void quit(){
       dispose();
    }

    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;
      }
    }

    public static void main(String argv[]){
       ImageSetViewer1 viewer = new ImageSetViewer1();
       viewer.show();
       String path = new String("images/");
       String filterString = ".gif,.jpeg,.jpg";
       viewer.loadImages(path, filterString);
       viewer.displayImage();
    }
}
