com.jthomas.util
Class ImageButton

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.Canvas
              |
              +--com.jthomas.util.ImageLabel
                    |
                    +--com.jthomas.util.ImageButton
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable
Direct Known Subclasses:
FaceButton

public class ImageButton
extends ImageLabel

A button class that uses an image instead of a textual label. Unlike many of the ImageButton classes available, it triggers an ACTION_EVENT, so you can add behavior in the same two ways as you can with normal Buttons:

  1. Make a subclass and put the behavior in the action method of that subclass.
  2. Use the main class but then catch the event in the action method of the Container.

By default, with FlowLayout the ImageButton takes its minimum size (just enclosing the image). The default with BorderLayout is to expand to fill the region in width (North/South), height (East/West) or both (Center). This is the same behavior as the builtin Button class. If you give an explicit resize or reshape call before adding the ImageButton to the Container, this size will override the defaults.

The original of the source code can be found at http://www.apl.jhu.edu/~hall/java/ImageLabel/ImageButton.java, the documentation is at .../ImageButton.html, and a small example program can be found at .../ImageButtonTest.html.

No warranty of any kind is provided. Permission is granted to use and/or modify for any purpose.

7/96 Marty Hall:

See Also:
Icon, GrayFilter, Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
 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.
protected static java.awt.Color defaultBorderColor
          Default color of 3D border around image.
protected static int defaultBorderWidth
          Default width of 3D border around image.
protected  java.awt.Image grayImage
          Gray image created automatically from regular image via an image filter to use when button is depressed.
 
Fields inherited from class com.jthomas.util.ImageLabel
debug, defaultImageString, explicitSize, height, image, imageString, margin, marginColor, width
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
ImageButton()
          Create an ImageButton with the default image.
ImageButton(java.awt.Image image)
          Create an ImageButton using the image specified.
ImageButton(java.lang.String imageURLString)
          Create an ImageButton using the image at URL specified by the string.
ImageButton(java.net.URL imageURL)
          Create an ImageButton using the image at URL specified.
 
Method Summary
 boolean action(java.awt.Event event, java.lang.Object arg)
          Generated when the button is clicked and released.
 java.awt.Color getBorderColor()
          Returns the Color for the border around the outside of the image.
 int getBorderWidth()
          Returns the width around the outside of the image.
 boolean mouseDown(java.awt.Event event, int x, int y)
          When the mouse is clicked, reverse the 3D border and draw a dark-gray version of the image.
 boolean mouseExit(java.awt.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.
 boolean mouseUp(java.awt.Event event, int x, int y)
          If cursor is still inside, trigger the action event and redraw the image (non-gray, button "out").
 void paint(java.awt.Graphics g)
          Draws the image with the border around it.
 void setBorderColor(java.awt.Color borderColor)
          Sets the Color for the border around the outside of the image.
 void setBorderWidth(int borderWidth)
          Sets the width around the outside of the image.
 
Methods inherited from class com.jthomas.util.ImageLabel
centerAt, contains, debug, drawRect, getMinimumSize, getPreferredSize, setBounds, setSize, waitForImage
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update
 
Methods inherited from class java.awt.Component
add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDrag, mouseEnter, mouseMove, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultBorderWidth

protected static final int defaultBorderWidth
Default width of 3D border around image. Currently 4.

See Also:
setBorderWidth(int), getBorderWidth(), Constant Field Values

defaultBorderColor

protected static final java.awt.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(java.awt.Color), getBorderColor()

grayImage

protected java.awt.Image grayImage
Gray image created automatically from regular image via an image filter to use when button is depressed. You won't normally use this directly.


darkness

public 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.

Constructor Detail

ImageButton

public ImageButton()
Create an ImageButton with the default image.

See Also:
ImageLabel.defaultImageString

ImageButton

public ImageButton(java.lang.String imageURLString)
Create an ImageButton using the image at URL specified by the string.

Parameters:
imageURLString - A String specifying the URL of the image.

ImageButton

public ImageButton(java.net.URL imageURL)
Create an ImageButton using the image at URL specified.

Parameters:
imageURL - The URL of the image.

ImageButton

public ImageButton(java.awt.Image image)
Create an ImageButton using the image specified. You would only want to call it directly if you already have an image (e.g. created via createImage).

Parameters:
image - The image.
Method Detail

setBorderWidth

public void setBorderWidth(int borderWidth)
Sets the width around the outside of the image.

See Also:
getBorderWidth()

getBorderWidth

public int getBorderWidth()
Returns the width around the outside of the image.

See Also:
setBorderWidth(int)

setBorderColor

public void setBorderColor(java.awt.Color borderColor)
Sets the Color for the border around the outside of the image.

See Also:
getBorderColor()

getBorderColor

public java.awt.Color getBorderColor()
Returns the Color for the border around the outside of the image.

See Also:
setBorderColor(java.awt.Color)

paint

public void paint(java.awt.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

mouseDown

public boolean mouseDown(java.awt.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 java.awt.Component

mouseUp

public boolean mouseUp(java.awt.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 java.awt.Component

action

public boolean action(java.awt.Event event,
                      java.lang.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 java.awt.Component
See Also:
Component.action(java.awt.Event, java.lang.Object)

mouseExit

public boolean mouseExit(java.awt.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 java.awt.Component