import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

/**
 * A simple image viewer that demonstrates the use of
 * content handlers
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ImageViewer
{

    /**
     * The entry point of the application
     *
     * @param args  The command line arguments (which are ignored)
     */
    public static void main(String[] args) throws Exception
    {
//[0
       BufferedReader               in; 
       ImageContentHandlerFactory   factory;
       ImageIcon                    icon;
//]0
       JComponent                   contentPane;
       JFrame                       frame;
       JLabel                       label;
//[0
       Object                       content;       
       String                       resource;
       URL                          url;
//]0


       frame = new JFrame();
       frame.setSize(300,300);
       contentPane = (JComponent)frame.getContentPane();
       contentPane.setLayout(new BorderLayout());
       label = new JLabel(" ");
       contentPane.add(label, BorderLayout.CENTER);
       frame.setVisible(true);
       
       

//[0
       // Tell the URLConnection class what ContentHandlerFactory to use
       factory = new ImageContentHandlerFactory();
       URLConnection.setContentHandlerFactory(factory);
       
       
       in = new BufferedReader(new InputStreamReader(System.in));

       System.out.println("URL for the image: ");
       while ((resource = in.readLine()) != null)
       {
          // Connect to a URL
          url = new URL(resource);          

          // Get the content
          content = url.getContent();         

          if (content != null)
          {
             // Cast the content appropriately
             icon = (ImageIcon)content;       

             // Display the image
             label.setIcon(icon);
          }
          System.out.println("URL for the image: ");
       }
//]0
    
          

    }
    
}

