Index
ImageCanvas.java


/*
 *  "COM.bensoft.widgets" Copyright (C) by Michael Benson - 7/27/97
 *
 *  "ImageCanvas" - a simple iconic area.
 */

package COM.bensoft.widgets;

import java.awt.*;
import java.applet.Applet;
import java.util.*;


/**
 * ImageCanvas implements a simple image area.
 *  
 * @author	Michael Benson
 */
public class ImageCanvas extends Canvas
{
	protected String	_fileName;
	protected Image		_image;
	protected Applet	_applet;

	//
	// Constructors:
	//
	
	/**
	 * ImageCanvas constructor.  Creates the image from a file name.
	 *
	 * @param	applet		the parent applet (used for code base).
	 * @param	name		image file name.
	 * @param	width		width of the image.
	 * @param	height		height of the image.
	 */
	public ImageCanvas(Applet applet, String name, int width, int height)
	{
		_fileName = new String(name);
		_applet = applet;
		_image = _applet.getImage(_applet.getCodeBase(), _fileName);
		reshape(0, 0, width, height);
	}
	
	/**
	 * ImageCanvas constructor.  Does not create the actual image.
	 * This should be used for things like animation.
	 *
	 * @param	applet		the parent applet (used for code base).
	 * @param	name		image file name.
	 * @param	width		width of the image.
	 * @param	height		height of the image.
	 * @see		COM.bensoft.widgets.ImageAnimator
	 */
	public ImageCanvas(Applet applet, int width, int height)
	{
		_applet = applet;
		_image = null;
		reshape(0, 0, width, height);
	}
	
	/**
	 * Sets the image file name and redraw the image.
	 *
	 * @param	name	the image file name.
	 */
	public void setImageFileName(String name)
	{
		_fileName = new String(name);
		_image = _applet.getImage(_applet.getCodeBase(), _fileName);
		paint(getGraphics());
	}
	
	/**
	 * Sets the actual image and redraws.  Use this if you already have
	 * the image and not the file name.
	 *
	 * @param	image	the image to be drawn.
	 */
	public void setImage(Image image)
	{
		_image = image;
		paint(getGraphics());
	}
	
	/**
	 * Returns the current image file name.
	 *
	 * @return	the current image file name.
	 */
	public String getImageFileName()
	{
		return _fileName;
	}
	
	/**
	 * Returns the current image.
	 *
	 * @return	the current image.
	 */
	public Image getImage()
	{
		return _image;
	}
	
	/**
	 * Overrides the action method in Canvas to redraw
	 * the image.
	 *
	 * @param	g	the graphics context.
	 * @see		java.awt.Canvas#paint(Graphics g)
	 */
	public void paint(Graphics g)
	{
		if (_image != null) {
			g.drawImage(_image, 0, 0, this);
		}
	}
}