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.
-
ImageCanvas()
- Set no images or event handler.
-
ImageCanvas(Image)
- Set only an image.
-
ImageCanvas(Image, Image, Component)
- Set two images and an event handler.
-
init()
-
-
minimumSize()
- Minimum size is also the size of the preferred image.
-
mouseDown(Event, int, int)
-
-
paint(Graphics)
- paint() uses double buffering to avoid flicker.
-
preferredSize()
- Preferred size is the size of the first image.
-
run()
- This object spawns a thread after a mouse click to wait and set the image
back to the original.
-
setEventHandler(Component)
- Set the event handler.
-
setImage(Image)
- Set the first image.
-
setImage(Image, Image)
- Set both images.
-
update(Graphics)
-
ImageCanvas
public ImageCanvas()
- Set no images or event handler.
ImageCanvas
public ImageCanvas(Image i)
- Set only an image.
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
init
protected void init()
setImage
public void setImage(Image i)
- Set the first image.
setImage
public void setImage(Image i,
Image i2)
- Set both images.
setEventHandler
public void setEventHandler(Component handler)
- Set the event handler.
preferredSize
public Dimension preferredSize()
- Preferred size is the size of the first image.
- Overrides:
- preferredSize in class Component
minimumSize
public Dimension minimumSize()
- Minimum size is also the size of the preferred image.
- Overrides:
- minimumSize in class Component
update
public void update(Graphics g)
- Overrides:
- update in class Component
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
mouseDown
public boolean mouseDown(Event event,
int x,
int y)
- Overrides:
- mouseDown in class Component
run
public void run()
- This object spawns a thread after a mouse click to wait and set the image
back to the original.