/*
 * @(#) SimpleViewer1.java 1.0 99/10/4
 * Copyright (c) 1999 Larry Rodrigues
 */
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
/** View an image
 * @version 1.0  10 April 1999
 * @author Lawrence Rodrigues
 */
public class SimpleViewer1 extends Frame {
   protected Image awtImage;
   protected boolean drawFlag = true;

   public void loadAndDisplay(String filename){
      awtImage = Toolkit.getDefaultToolkit().getImage(filename);
      int width = awtImage.getWidth(this);
      int height = awtImage.getHeight(this);
      if((width <0 )|| (height <0)) drawFlag = false;
      else displayImage(awtImage, width, height);
   }

   public void displayImage(Image img, int width, int height) {
      addWindowListener(
         new WindowAdapter() {
            public void windowClosing(WindowEvent e){
               System.exit(0);
            }
         } );
      setTitle("Simple Viewer ");
      pack();
      setSize(new Dimension(width, height));
      show();
      repaint();
   }

   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 loading or drawing");
          return false;
       }

       if((infoflags & (ImageObserver.FRAMEBITS | ImageObserver.ALLBITS))!= 0) {
          if(drawFlag == false) {
             displayImage(img,width, height);
             drawFlag = true;
          }
          repaint();
          return false;
       }
       return true;
   }


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

   public static void  main(String[] args){
      SimpleViewer1 ip = new SimpleViewer1();
      if(args.length <1) {
         System.out.println("Enter a valid image file name");
         System.exit(0);
      }
      ip.loadAndDisplay(args[0]);
   }
}
