Class ImageCanvas

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Canvas
                   |
                   +----ImageCanvas

public class ImageCanvas
extends Canvas
implements Runnable
This is a component on which you can place an image. I've extended it so you can add a second image. When the image is clicked on, it's replaced by the second image for 1/5 of a second, and an event is passed to a designated event handler, so you can use it as an image button. Use JDK 1.02 event model.


Constructor Index

 o ImageCanvas()
Set no images or event handler.
 o ImageCanvas(Image)
Set only an image.
 o ImageCanvas(Image, Image, Component)
Set two images and an event handler.

Method Index

 o init()
 o minimumSize()
Minimum size is also the size of the preferred image.
 o mouseDown(Event, int, int)
 o paint(Graphics)
paint() uses double buffering to avoid flicker.
 o preferredSize()
Preferred size is the size of the first image.
 o run()
This object spawns a thread after a mouse click to wait and set the image back to the original.
 o setEventHandler(Component)
Set the event handler.
 o setImage(Image)
Set the first image.
 o setImage(Image, Image)
Set both images.
 o update(Graphics)

Constructors

 o ImageCanvas
  public ImageCanvas()
Set no images or event handler.

 o ImageCanvas
  public ImageCanvas(Image i)
Set only an image.

 o ImageCanvas
  public ImageCanvas(Image i,
                     Image i2,
                     Component handler)
Set two images and an event handler.

Parameters:
i - the image which is usually displayed.
i2 - the image which is displayed after a click
handler - the component which will handle the click event

Methods

 o init
  protected void init()
 o setImage
  public void setImage(Image i)
Set the first image.

 o setImage
  public void setImage(Image i,
                       Image i2)
Set both images.

 o setEventHandler
  public void setEventHandler(Component handler)
Set the event handler.

 o preferredSize
  public Dimension preferredSize()
Preferred size is the size of the first image.

Overrides:
preferredSize in class Component
 o minimumSize
  public Dimension minimumSize()
Minimum size is also the size of the preferred image.

Overrides:
minimumSize in class Component
 o update
  public void update(Graphics g)
Overrides:
update in class Component
 o paint
  public void paint(Graphics g)
paint() uses double buffering to avoid flicker. The unused area around the image is filled with the background colour.

Overrides:
paint in class Canvas
 o mouseDown
  public boolean mouseDown(Event event,
                           int x,
                           int y)
Overrides:
mouseDown in class Component
 o run
  public void run()
This object spawns a thread after a mouse click to wait and set the image back to the original.