
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

/** An instance is a JPanel that contains an image. Since it is
    a JPanel, it can be placed in a window. The system calls method
    paint whenever it is necessary to redraw the image.<br><br>
   */
public class ImagePanel extends JPanel {
    
    private Image image;  // the image for which the canvas is created
 
    /** Constructor: create an image canvas for image */
    public ImagePanel(Image im) {
        image= im;// store the dimensions of the image map
        if (im == null)
            return;
        int rows= image.getHeight(this);
        int cols= image.getWidth(this);
        setPreferredSize(new Dimension(cols, rows)); 
    }
    
    /** = the image that is on this panel */
    public Image getImage() {
      return image;
    }
    
    /** Display the image of the image on this JPanel. */
    public void paint(Graphics g) {
        g.drawImage(image,0,0,this);
    }
    
    /** Change the image in the image panel to im */
    public void changeImage(Image im) {
        image= im;
        if (im == null)
            return;
        int rows= image.getHeight(this);
        int cols= image.getWidth(this);
        Dimension d= new Dimension(cols,rows);
        setPreferredSize(d);
        setSize(d);
        repaint();
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}



