Class ImageButton

Class ImageButton

java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Canvas
                   |
                   +----ImageLabel
                           |
                           +----ImageButton

Note: To see an on-line demo, see the page for ImageButtons.html. To download the source code, see ImageButton.java. For more examples, see the Core Web Programming source code archive.
public class ImageButton
extends ImageLabel
A button class that uses an image instead of a textual label. Clicking and releasing the mouse over the button triggers an ACTION_EVENT, so you can add behavior in the same two ways as you with a normal Button (in Java 1.0):
  1. Make an ImageButton subclass and put the behavior in the action method of that subclass.
  2. Use the main ImageButton class but then catch the events in the action method of the Container.

Normally, the ImageButton's preferredSize (used, for instance, by FlowLayout) is just big enough to hold the image. However, if you give an explicit resize or reshape call before adding the ImageButton to the Container, this size will override the defaults.

Version:
1.0 (1997)
Author:
Marty Hall (hall@apl.jhu.edu)
See Also:
Icon, GrayFilter

Variable Index

 o defaultBorderColor
Default color of 3D border around image.
 o defaultBorderWidth
Default width of 3D border around image.

Constructor Index

 o ImageButton()
Create an ImageButton with the default image.
 o ImageButton(Image)
Create an ImageButton using the image specified.
 o ImageButton(String)
Create an ImageButton using the image at URL specified by the string.
 o ImageButton(URL)
Create an ImageButton using the image at URL specified.
 o ImageButton(URL, String)
Creates an ImageButton using the file in the directory specified.

Method Index

 o action(Event, Object)
Generated when the button is clicked and released.
 o getDarkness()
The darkness value to use for grayed images.
 o getGrayImage()
The gray image used when button is down.
 o mouseDown(Event, int, int)
When the mouse is clicked, reverse the 3D border and draw a dark-gray version of the image.
 o mouseExit(Event, int, int)
If you move the mouse off the button while the mouse is down, abort and do not trigger the action.
 o mouseUp(Event, int, int)
If cursor is still inside, trigger the action event and redraw the image (non-gray, button "out").
 o paint(Graphics)
Draws the image with the border around it.
 o setDarkness(int)
An int whose bits are combined via "and" ("&") with the alpha, red, green, and blue bits of the pixels of the image to produce the grayed-out image to use when button is depressed.
 o setGrayImage(Image)
Sets gray image created automatically from regular image via an image filter to use when button is depressed.

Variables

 o defaultBorderWidth
  protected final static int defaultBorderWidth
Default width of 3D border around image. Currently 4.
See Also:
setBorder, getBorder
 o defaultBorderColor
  protected final static Color defaultBorderColor
Default color of 3D border around image. Currently a gray with R/G/B of 160/160/160. Light grays look best.
See Also:
setBorderColor, getBorderColor

Constructors

 o ImageButton
  public ImageButton()
Create an ImageButton with the default image.
See Also:
getDefaultImageString
 o ImageButton
  public ImageButton(String imageURLString)
Create an ImageButton using the image at URL specified by the string.
Parameters:
imageURLString - A String specifying the URL of the image.
 o ImageButton
  public ImageButton(URL imageURL)
Create an ImageButton using the image at URL specified.
Parameters:
imageURL - The URL of the image.
 o ImageButton
  public ImageButton(URL imageDirectory,
                     String imageFile)
Creates an ImageButton using the file in the directory specified.
Parameters:
imageDirectory - The URL of a directory
imageFile - File in the above directory
 o ImageButton
  public ImageButton(Image image)
Create an ImageButton using the image specified. You would only want to use this if you already have an image (e.g. created via createImage).
Parameters:
image - The image.

Methods

 o paint
  public void paint(Graphics g)
Draws the image with the border around it. If you override this in a subclass, call super.paint().
Overrides:
paint in class ImageLabel
 o mouseDown
  public boolean mouseDown(Event event,
                           int x,
                           int y)
When the mouse is clicked, reverse the 3D border and draw a dark-gray version of the image. The action is not triggered until mouseUp.
Overrides:
mouseDown in class Component
 o mouseUp
  public boolean mouseUp(Event event,
                         int x,
                         int y)
If cursor is still inside, trigger the action event and redraw the image (non-gray, button "out"). Otherwise ignore this.
Overrides:
mouseUp in class Component
 o action
  public boolean action(Event event,
                        Object arg)
Generated when the button is clicked and released. Override this in subclasses to give behavior to the button. Alternatively, since the default behavior is to pass the ACTION_EVENT along to the Container, you can catch events for a bunch of buttons there.
Overrides:
action in class Component
See Also:
action
 o mouseExit
  public boolean mouseExit(Event event,
                           int x,
                           int y)
If you move the mouse off the button while the mouse is down, abort and do not trigger the action. Ignore this if button was not already down.
Overrides:
mouseExit in class Component
 o getDarkness
  public int getDarkness()
The darkness value to use for grayed images.
See Also:
setDarkness
 o setDarkness
  public void setDarkness(int darkness)
An int whose bits are combined via "and" ("&") with the alpha, red, green, and blue bits of the pixels of the image to produce the grayed-out image to use when button is depressed. Default is 0xffafafaf: af combines with r/g/b to darken image.
 o getGrayImage
  public Image getGrayImage()
The gray image used when button is down.
See Also:
setGrayImage
 o setGrayImage
  public void setGrayImage(Image grayImage)
Sets gray image created automatically from regular image via an image filter to use when button is depressed. You won't normally use this directly.