/* imageDisplay03.java last mod 27 JAN 2012 by Ed James */
// Runs as Application or Applet
// Application accepts command-line parm - image file name.  If not
//    supplied, user is prompted for image file name.
// Applet checks applet html for URL for desired file name

// NOTE: Prolly should add way to run in AppletViewer

import java.awt.*;
import java.awt.event.*;              // for the window adapter
import java.applet.*;
import javax.swing.JFrame;            // for JFrame class (to hold panel)
import javax.swing.JPanel;            // for JPanel class (to hold label)
import javax.swing.JLabel;            // for JLabel (to hold image)
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;         // for ImageIO.read and ImageURL
import java.io.*;                     // for BufferedReader class
import java.net.URL;                  // for URL/applet use

class imageDisplay03Frame extends JFrame {
   public static final long serialVersionUID = 99L; // Not currently used.
   Image myImage;
   JPanel myPanel;
   String fileName;
   String strImageURL;

   void setFileName (String pFileName) { fileName = pFileName; }
   void setImageURL (String pImageURL) { strImageURL = pImageURL; }

   public imageDisplay03Frame () {
      super();

      setTitle ("imageDisplay03");
      }

   void buildGUI () {
      myPanel = new JPanel();

      JLabel myLabel = new JLabel(new ImageIcon( myImage ));
      myPanel.add(myLabel);
      int iWidth  = myImage.getWidth(myPanel);
      int iHeight = myImage.getHeight(myPanel);
      getContentPane().add(myPanel);
      setVisible(true);
      setSize(iWidth+20, iHeight+40);
      }

   void addWindowListener () {
   // When top-level window is closed, kill the process
      try {
         addWindowListener (new WindowAdapter() {
            public void windowClosing (WindowEvent myEvent) {
               System.exit (0);
               }
            });
         }
      catch (java.security.AccessControlException ACE2) {
         System.out.println("Error 2: " + ACE2.getMessage());
         }
      }

   void promptForFileName () {
      try {
         System.out.println("Enter image name\n");
         BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
         fileName = bf.readLine();
         }
      catch (IOException IOE3) {   // didn't get user-supplied file name
         System.out.println("Error 3: " + IOE3.getMessage());
         }
      }

   public void loadImageFromFile () {
      File imageFile = new File(fileName);
      try {
         myImage = ImageIO.read(imageFile);
         }
      catch (IOException IOE4) {   // can't find/load image
         System.out.println ("Error 4" + IOE4.getMessage());
         }
      }

   public void loadImageFromURL () {
      try {
         URL tempURL = new URL (strImageURL);

         myImage = ImageIO.read (tempURL);
//       NOTE: above works from webserver, but throws exception with AppletViewer
//       imageURL == http://www.greenbelt.com/edjames/javdocs/clake.jpg
//       java.security.AccessControlException: access denied
//          (java.net.SocketPermission www.greenbelt.com:80 connect,resolve)
//       NOTE: don't *need* to resolve this but it would be kinda kewl to do so.
         }
      catch (java.net.MalformedURLException MRULE1) {  // bad URL
         System.out.println ("Error 5: " + MRULE1.getMessage());
         }
      catch (java.io.IOException IOE4) {  // can't find/load image
         System.out.println ("Error 6: " + IOE4.getMessage());
         }
      }
   }

public class imageDisplay03 extends Applet {
   public static final long serialVersionUID = 454L; // Not currently used.
   public static imageDisplay03Frame myFrame;

   public void init () {                     // run in Applet mode
      String imageURL;

      imageURL = getParameter("imageURL");   // query html for image file name

      myFrame = new imageDisplay03Frame();   // build the top-leve GUI widet
      myFrame.setImageURL (imageURL);        // query the html for image file name
      myFrame.loadImageFromURL ();           // load image from URL-specified file
      myFrame.buildGUI();                    // build the complete GUI; frame adjusts size to image
      }

//   public void paint (Graphics g) { /* not used in    }

   public static void main (String[] args) { // run in Application mode

      myFrame = new imageDisplay03Frame();   // build the top-level GUI widget

      if (args.length == 1)                  // if command-line parm is used
         myFrame.setFileName (args[0]);      // use the parm as image file name
      else                                   // else
         myFrame.promptForFileName();        // prompt user for image file name

      myFrame.loadImageFromFile();           // load image from user-specified file
      myFrame.buildGUI();                    // build the complete GUI; frame adjusts size to image
      myFrame.addWindowListener();           // used to close main window/kill the process
      }
   }