import java.awt.*;
import java.awt.image.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;

/** An instance is a JFrame that contains an image and its file name.
    When no image is in it, it is hidden. When an image is placed in it,
    it becomes visible. */
public class ImageJFrame extends JFrame {
    
    private Image image; // The image (can be null)
    private ImagePanel panel;  // The panel on which the image is placed
    
    /** Constructor: an instance for Image image with title t.
        If im is null, the window is not shown. */
    public ImageJFrame(Image im, String t) {
        panel= new ImagePanel(null);
        getContentPane().add(BorderLayout.CENTER, panel);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocation(200,100);
        placeImage(im,t);
    }
    
    // = the panel on which the image is placed
    public ImagePanel getImagePanel() {
        return panel;
    }
    
    // set the image in the JFrame to im (which is not null); leave the title unchanged
    public void setImage(Image im) {
        placeImage(im, getTitle());
    }
    
    /** Remove the image in this JFrame (if any), put image
        im there, with title t. If im is empty, make the window 
        invisible; otherwise, make it visible */
    public void placeImage(Image im, String t) {
        setTitle(t);
        image= im;
        panel.changeImage(im);
        if (im == null) {
            setVisible(false);
            return;
        }
        
        //  {image is not null and should be visible }  
        int rows= im.getHeight(this);
        int cols= im.getWidth(this);
        setSize(new Dimension(cols, rows));
        pack();
        setVisible(true);
        repaint();
    }
}