/*
 * @(#) SimpleViewer.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 SimpleViewer extends Frame {
   protected Image awtImage;

   public void loadAndDisplay(String filename){
      Image img = readImage(filename);
      awtImage = img;
      int width = img.getWidth(this);
      int height = img.getHeight(this);
      displayImage(img, 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 Image readImage(String imageName){
      Image image = Toolkit.getDefaultToolkit().getImage(imageName);
      MediaTracker imageTracker = new MediaTracker(this);
      imageTracker.addImage(image, 0);
      try{
        imageTracker.waitForID(0);
      }catch(InterruptedException e){ return null;}
      return image;
   }


   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){
      SimpleViewer ip = new SimpleViewer();
      if(args.length <1) {
         System.out.println("Enter a valid image file name");
         System.exit(0);
      }
      ip.loadAndDisplay(args[0]);
   }
}
