import java.awt.*; /** *
ImageCanvas is a Canvas that holds an Image. The Image can be * switched, but ImageCanvas assumes all Images have the same * dimensions of the first image.
* * @author Mark L. Irons * @version 1.0 * @date 18-11-1998 */ public class ImageCanvas extends Canvas { ImageCanvasEventHandler eventHandler_ = null; Image image_ = null; // current image /** * This is provided for convenience only. ImageCanvas objects * must be initialized by calling a constructor that has * parameters. */ public ImageCanvas() { } /** * Creates an ImageCanvas with the specified event handler and * initial image. * * @param handler a callback function that handles events * generated by this ImageCanvas * @param Image initial Image to be displayed. All subsequent * Images set withsetImage
should have the same
* dimensions.
*/
public ImageCanvas(ImageCanvasEventHandler handler, Image image)
{
eventHandler_ = handler;
image_ = image;
preferredSize();
minimumSize();
}
public Dimension preferredSize()
{
return (new Dimension(image_.getWidth(this),image_.getHeight(this)));
}
public Dimension minimumSize()
{
return (new Dimension(image_.getWidth(this),image_.getHeight(this)));
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image_,0,0,this);
}
/**
* Draws the ImageCanvas
*/
public void draw()
{
Graphics g = getGraphics();
if ( g != null ) {
g.drawImage(image_,0,0,this);
}
}
/**
* Sets the ImageCanvas to a new Image. This should have the same
* dimensions as the initial Image.
*/
public void setImage(Image image)
{
image_ = image;
draw();
}
/**
* Handles a mouseDown event by calling the ImageCanvas' callback
* (set in the constructor).
*/
public boolean mouseDown(Event event, int x, int y)
{
eventHandler_.imageCanvasEvent(this);
return true;
}
/**
* Doesn't do anything right now
*/
public boolean mouseUp(Event event, int x, int y)
{
return true;
}
} // class ImageCanvas