import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

/**
 * An example that illustrates the rendering of sampled static visual
 * content
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class ImageComponentDriver implements Runnable
{
    private String name;
    
    /**
     * The entry point of the application.
     *
     * @param args The command line arguments (args[0] is the file)
     */
    public static void main(String[] args) throws Exception
    {
        String name;
        
        if (args.length == 0) name = "firstsnow.gif";
        else                  name = args[0];
       
        SwingUtilities.invokeAndWait(new ImageComponentDriver(name));
    }

    /**
     * Construtor.
     *
     * @param name  The name of the file to read
     */
    public ImageComponentDriver(String name)
    {
        super();
        this.name = name;
    }
    
    /**
     * The code to run in the event dispatch thread.
     */
    public void run()
    {
        try
        {
            // Read the Image
            Image image = ImageIO.read(new File(name));

            // Construct the JComponent
            JComponent component = new ImageComponent(image);          

            // Construct the JFrame and add the JComponent
            JFrame frame = new JFrame();
            frame.setSize(600,400);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel contentPane = (JPanel)frame.getContentPane();
            contentPane.setLayout(new BorderLayout());
            contentPane.add(component, BorderLayout.CENTER);
            frame.setVisible(true);
        }
        catch (IOException ioe)
        {
            System.out.println("Unable to load image!");
        }
    }
}
