/*
 * @(#)ImageCapsule.java    1.1  23 August 2001
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.gui.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

/**
 * A <CODE>JComponent</CODE> displaying an image.
 * 
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.1
 */
public class ImageCapsule extends JComponent {

    /** The image displayed by this component. */
    protected Image image = null;
    
    //////////////////
    // Constructors //
    //////////////////

    /**
     * Constructs an encapsulation of the given image.
     *
     * @param i the image to encapsulate
     */
    public ImageCapsule(Image i) {
        setImage(i);
        
        if (image != null) {
            setSize(new Dimension(
                image.getWidth(this), 
                image.getHeight(this)));
        }
    }

    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Sets the image encapsulated by this component
     * to the given image.
     *
     * @param i an image to be encapsulated
     * @see #getImage()
     */
    public void setImage(Image i) {
        if (i != null) {
            image = i;
            repaint();
        }
    }
    
    /** 
     * Returns the image encapsulated by this component. 
     *
     * @see #setImage(Image)
     */
    public Image getImage() {
        return image;
    }
    
    ///////////////////////
    // Protected methods //
    ///////////////////////

    /**
     * Paints this component to the given graphics context.
     *
     * @param g the graphics context to which to paint
     */
    protected void paintComponent(Graphics g) {
        Insets insets = getInsets();

        if (getImage() != null) {
            g.drawImage(
                getImage(), 
                insets.left, 
                insets.top, 
                getWidth()  - insets.left - insets.right,
                getHeight() - insets.top  - insets.bottom,
                this);
        }
    }
}

