com.tecnick.jwtm
Class ImageButton

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by com.tecnick.jwtm.ImageButton
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible

public class ImageButton
extends java.awt.Panel

Title: ImageButton Class

Description: Class to create image buttons with text and behaviour


Copyright (c) 2002-2006 Tecnick.com S.r.l (www.tecnick.com) Via Ugo Foscolo n.19 - 09045 Quartu Sant'Elena (CA) - ITALY - www.tecnick.com - info@tecnick.com
Project homepage: http://jxhtmledit.sourceforge.net
License: http://www.gnu.org/copyleft/gpl.html GPL 2

Version:
1.1.003
Author:
Nicola Asuni [www.tecnick.com].
See Also:
Serialized Form

Nested Class Summary
(package private)  class ImageButton.imageButtonMouseAdapter
          Inner class for handling mouse events.
 
Nested classes/interfaces inherited from class java.awt.Panel
java.awt.Panel.AccessibleAWTPanel
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  java.lang.String actionCommand
          action command to be returned to the listener
protected  java.awt.event.ActionListener actionListener
          the action listener (for events)
private  java.applet.AudioClip audio_click
          audio clip for button click event
private  java.applet.AudioClip audio_over
          audio clip for mouse over event
private  int bevel_width
          3D bevel width in pixels
private  int bh
          height of background image in pixels
static int BOTTOM
          constant BOTTOM
private  java.awt.Graphics bufferGraphics
          graphics for double buffering
private  int buttonID
          menu item id to be returned on event as actionCommand
private  int bw
          width of background image in pixels
private  int bx
          horizontal position of background image
private  int by
          vertical position of background image
static int CENTER
          constant CENTER
private  boolean center_block
          if true center the block [image + label]
private  java.awt.Color col_off_bck
          background color for mouse-off state
private  java.awt.Color col_off_sdw
          text shadow color for mouse-off state
private  java.awt.Color col_off_txt
          text color for mouse-off state
private  java.awt.Color col_on_bck
          background color for mouse-on state
private  java.awt.Color col_on_sdw
          text shadow color for mouse-on state
private  java.awt.Color col_on_txt
          text color for mouse-on state
private  java.awt.Color col_over_bck
          background color for mouse-over state
private  java.awt.Color col_over_sdw
          text shadow color for mouse-over state
private  java.awt.Color col_over_txt
          text color for mouse-over state
private  java.awt.Color col_shadow
          remember current text shadow color
protected  int current_action
          current button action to send to listener -1 means no action
private  boolean fDrawPushedIn
          if true make the button "depress" when clicked.
private  boolean fShowBorder
          if true display the 3D button border
private  int gap
          distance in pixels between image and label
private  int ih
          height of internal button icon image in pixels
private  float imageScale
          float parameter to scale the image size
private  java.awt.Image img
          current button icon
private  java.awt.Image img_bg
          current background button image
private  java.awt.Image img_bg_off
          button background image for normal state
private  java.awt.Image img_bg_on
          button background image for button pressed
private  java.awt.Image img_bg_over
          button background image for mouse over
private  java.awt.Image img_off
          button icon for normal state
private  java.awt.Image img_on
          button icon for button pressed
private  java.awt.Image img_over
          button icon for mouse over
private  java.awt.Image inactive_img
          inactive image for disabled button
protected  boolean isButtonPushed
          Remember mouse up/down status
private  int iw
          width of internal button icon image in pixels
private  int ix
          horizontal position of internal button icon image
private  int iy
          vertical position of internal button icon image
private  java.lang.String label
          button label
static int LEFT
          constant LEFT
private  java.awt.Image offscreen
          offscreen image to handle double buffering
private  int[] padding
          array of button paddings (Left, Right, Top, Bottom) distance in pixels between button border and button objects (image or label)
private  int pos
          label position relative to icon position.
static int RIGHT
          constant RIGHT
private static long serialVersionUID
          serialVersionUID
private  int shadow_x
          text shadow relative horizontal position in pixels
private  int shadow_y
          text shadow relative vertical position in pixels
static int TOP
          constant TOP
 
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(java.awt.Image image)
          Constructor for void label
ImageButton(java.awt.Image image, java.lang.String label)
          Constructor
ImageButton(java.lang.String label)
          Constructor for void image
 
Method Summary
 void addActionListener(java.awt.event.ActionListener l)
          Adds the specified action listener to receive action events from this button.
 void addNotify()
          Creates the Panel's peer.
protected  void fireActionEvent(int actiontype)
          Fire an action event to the listeners.
 java.lang.String getActionCommand()
          Returns the command name of the action event fired by this button.
 int getButtonID()
          Get the buttonID (unique numeric identifier for current component)
 int getImageLabelGap()
          Get the gap in pixels between the label (if any) and image.
 java.lang.String getLabel()
          Get the button text label
 int getLabelPosition()
          Get the label position (relative to button icon) possible values are: ImageButton.LEFT, ImageButton.RIGHT, ImageButton.TOP, ImageButton.BOTTOM
 int[] getPadding()
          Get button paddings in pixels as array (Left, Right, Top, Bottom).
protected  void imageButton_MouseOff()
          Gets called when the mouse crosses out of the button area.
protected  void imageButton_MouseOver(boolean sound_on)
          Gets called when the mouse crosses into the button area.
protected  void imageButton_MousePushed()
          Gets called when the mouse button is pressed.
 boolean isFocusable()
          Returns whether this Component can become the focus owner.
 boolean isFocusTraversable()
          Returns whether this Component can become the focus owner.
 void paint(java.awt.Graphics g)
          Draw button elements (border, image and label) at calculated positions
 void removeActionListener(java.awt.event.ActionListener l)
          Removes the specified action listener so it no longer receives action events from this button.
protected  void repaintImages(java.awt.Graphics gbuffer)
          Redraw the button images
 void resize()
          Resize the size of button and components.
 void setActionCommand(java.lang.String command)
          Sets the command name of the action event fired by this button.
 void setAudioStateClips(java.applet.AudioClip a, java.applet.AudioClip b)
          Set the audio clips to play for mouse-on and mouse over events.
 void setBgImage(java.awt.Image i)
          Set the current background image to be shown.
 void setBorderWidth(int bb)
          Set the button bevel width in pixels
private  void setButtonComponents(java.applet.AudioClip sound, java.awt.Color foreground, java.awt.Color background, java.awt.Color shadow, java.awt.Image bg, java.awt.Image icon)
          Set and repaint button components
 void setButtonID(int id)
          Set buttonID (unique numeric identifier for current component)
 void setCenterBlock(boolean a)
          Set the position of the block (label + image)
 void setDrawPushedIn(boolean b)
          Make the button "depress" when clicked.
 void setEnable(boolean a)
          Enables or disables this component, depending on the value of the parameter b.
 void setFont(java.awt.Font f)
          Set the button label font
 void setImage(java.awt.Image i)
          Set the current image icon to be shown.
 void setImageLabelGap(int g)
          Set the gap in pixels between the label (if any) and image.
 void setImageScale(double f)
          Scale the image to the given value (1.0 = 100%).
 void setImageScale(float pct)
          Scale the image to the given value (1.0 = 100%).
 void setLabel(java.lang.String l)
          Set the button text label
 void setLabelPosition(int a)
          Set the position of the label relative to icon
 void setPadding(int[] p)
          Set the padding in pixels.
 void setShadowPosition(int x, int y)
          Set shadow relative position
 void setStateBgImages(java.awt.Image imgoff, java.awt.Image imgon, java.awt.Image imgover)
          Set the background state images (one image for each button/mouse state)
 void setStateColors(java.awt.Color ba, java.awt.Color bb, java.awt.Color bc, java.awt.Color ta, java.awt.Color tb, java.awt.Color tc, java.awt.Color sa, java.awt.Color sb, java.awt.Color sc)
          Set the button colors for each state
 void setStateImages(java.awt.Image imgoff, java.awt.Image imgon, java.awt.Image imgover)
          Set the state images (one image for each button/mouse state)
 
Methods inherited from class java.awt.Panel
getAccessibleContext
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, update, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusOwner, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
serialVersionUID

See Also:
Constant Field Values

bufferGraphics

private java.awt.Graphics bufferGraphics
graphics for double buffering


offscreen

private java.awt.Image offscreen
offscreen image to handle double buffering


actionCommand

protected java.lang.String actionCommand
action command to be returned to the listener


actionListener

protected java.awt.event.ActionListener actionListener
the action listener (for events)


isButtonPushed

protected boolean isButtonPushed
Remember mouse up/down status


buttonID

private int buttonID
menu item id to be returned on event as actionCommand


bevel_width

private int bevel_width
3D bevel width in pixels


LEFT

public static final int LEFT
constant LEFT

See Also:
Constant Field Values

TOP

public static final int TOP
constant TOP

See Also:
Constant Field Values

RIGHT

public static final int RIGHT
constant RIGHT

See Also:
Constant Field Values

BOTTOM

public static final int BOTTOM
constant BOTTOM

See Also:
Constant Field Values

CENTER

public static final int CENTER
constant CENTER

See Also:
Constant Field Values

label

private java.lang.String label
button label


img

private java.awt.Image img
current button icon


img_off

private java.awt.Image img_off
button icon for normal state


img_on

private java.awt.Image img_on
button icon for button pressed


img_over

private java.awt.Image img_over
button icon for mouse over


img_bg

private java.awt.Image img_bg
current background button image


img_bg_off

private java.awt.Image img_bg_off
button background image for normal state


img_bg_on

private java.awt.Image img_bg_on
button background image for button pressed


img_bg_over

private java.awt.Image img_bg_over
button background image for mouse over


inactive_img

private java.awt.Image inactive_img
inactive image for disabled button


imageScale

private float imageScale
float parameter to scale the image size


audio_click

private java.applet.AudioClip audio_click
audio clip for button click event


audio_over

private java.applet.AudioClip audio_over
audio clip for mouse over event


fShowBorder

private boolean fShowBorder
if true display the 3D button border


fDrawPushedIn

private boolean fDrawPushedIn
if true make the button "depress" when clicked.


pos

private int pos
label position relative to icon position. possible values are: ImageButton.LEFT, ImageButton.RIGHT, ImageButton.TOP, ImageButton.BOTTOM


center_block

private boolean center_block
if true center the block [image + label]


padding

private int[] padding
array of button paddings (Left, Right, Top, Bottom) distance in pixels between button border and button objects (image or label)


gap

private int gap
distance in pixels between image and label


shadow_x

private int shadow_x
text shadow relative horizontal position in pixels


shadow_y

private int shadow_y
text shadow relative vertical position in pixels


ix

private int ix
horizontal position of internal button icon image


iy

private int iy
vertical position of internal button icon image


iw

private int iw
width of internal button icon image in pixels


ih

private int ih
height of internal button icon image in pixels


bx

private int bx
horizontal position of background image


by

private int by
vertical position of background image


bw

private int bw
width of background image in pixels


bh

private int bh
height of background image in pixels


col_off_bck

private java.awt.Color col_off_bck
background color for mouse-off state


col_over_bck

private java.awt.Color col_over_bck
background color for mouse-over state


col_on_bck

private java.awt.Color col_on_bck
background color for mouse-on state


col_off_txt

private java.awt.Color col_off_txt
text color for mouse-off state


col_over_txt

private java.awt.Color col_over_txt
text color for mouse-over state


col_on_txt

private java.awt.Color col_on_txt
text color for mouse-on state


col_off_sdw

private java.awt.Color col_off_sdw
text shadow color for mouse-off state


col_over_sdw

private java.awt.Color col_over_sdw
text shadow color for mouse-over state


col_on_sdw

private java.awt.Color col_on_sdw
text shadow color for mouse-on state


col_shadow

private java.awt.Color col_shadow
remember current text shadow color


current_action

protected int current_action
current button action to send to listener -1 means no action

Constructor Detail

ImageButton

public ImageButton(java.awt.Image image)
Constructor for void label

Parameters:
image - Button Image

ImageButton

public ImageButton(java.lang.String label)
Constructor for void image

Parameters:
label - Text label of the button

ImageButton

public ImageButton(java.awt.Image image,
                   java.lang.String label)
Constructor

Parameters:
image - Button Image
label - Text label of the button
Method Detail

isFocusTraversable

public boolean isFocusTraversable()
Returns whether this Component can become the focus owner.

Overrides:
isFocusTraversable in class java.awt.Component
Returns:
boolean true

isFocusable

public boolean isFocusable()
Returns whether this Component can become the focus owner.

Overrides:
isFocusable in class java.awt.Component
Returns:
boolean true

addNotify

public void addNotify()
Creates the Panel's peer. The peer allows you to modify the appearance of the panel without changing its functionality.

Overrides:
addNotify in class java.awt.Panel

setFont

public void setFont(java.awt.Font f)
Set the button label font

Overrides:
setFont in class java.awt.Container
Parameters:
f - font

setStateColors

public void setStateColors(java.awt.Color ba,
                           java.awt.Color bb,
                           java.awt.Color bc,
                           java.awt.Color ta,
                           java.awt.Color tb,
                           java.awt.Color tc,
                           java.awt.Color sa,
                           java.awt.Color sb,
                           java.awt.Color sc)
Set the button colors for each state

Parameters:
ba - background button color when mouse is off
bb - background button color when mouse is over
bc - background button color when button is clicked
ta - foreground text color when mouse is off
tb - foreground text color when mouse is over
tc - foreground text color when button is clicked
sa - shadow text color when mouse is off
sb - shadow text color when mouse is over
sc - shadow text color when button is clicked

setShadowPosition

public void setShadowPosition(int x,
                              int y)
Set shadow relative position

Parameters:
x - horizontal pixels between text position and shadow position
y - vertical pixels between text position and shadow position

setButtonID

public void setButtonID(int id)
Set buttonID (unique numeric identifier for current component)

Parameters:
id - button ID

getButtonID

public int getButtonID()
Get the buttonID (unique numeric identifier for current component)

Returns:
buttonID

setBorderWidth

public void setBorderWidth(int bb)
Set the button bevel width in pixels

Parameters:
bb - bevel size

setDrawPushedIn

public void setDrawPushedIn(boolean b)
Make the button "depress" when clicked. If false the button will behave normally, except when pushed it will give no visual feedback.

Parameters:
b - true or false

getLabelPosition

public int getLabelPosition()
Get the label position (relative to button icon) possible values are: ImageButton.LEFT, ImageButton.RIGHT, ImageButton.TOP, ImageButton.BOTTOM

Returns:
label position

setLabelPosition

public void setLabelPosition(int a)
Set the position of the label relative to icon

Parameters:
a - ImageButton.LEFT, ImageButton.RIGHT, ImageButton.TOP, ImageButton.BOTTOM

setCenterBlock

public void setCenterBlock(boolean a)
Set the position of the block (label + image)

Parameters:
a - if true center the block

getLabel

public java.lang.String getLabel()
Get the button text label

Returns:
current text label string

setLabel

public void setLabel(java.lang.String l)
Set the button text label

Parameters:
l - button text label

setAudioStateClips

public void setAudioStateClips(java.applet.AudioClip a,
                               java.applet.AudioClip b)
Set the audio clips to play for mouse-on and mouse over events.

Parameters:
a - audio clip when button is clicked
b - audio clip when mouse is over

setPadding

public void setPadding(int[] p)
Set the padding in pixels. Padding is the distance between the button border and button components.

Parameters:
p - array of button paddings in pixels (Left, Right, Top, Bottom)

getPadding

public int[] getPadding()
Get button paddings in pixels as array (Left, Right, Top, Bottom). Padding is the distance between the button border and button components.

Returns:
padding in pixels

setImageLabelGap

public void setImageLabelGap(int g)
Set the gap in pixels between the label (if any) and image.

Parameters:
g - pixels

getImageLabelGap

public int getImageLabelGap()
Get the gap in pixels between the label (if any) and image.

Returns:
gap in pixels

setStateImages

public void setStateImages(java.awt.Image imgoff,
                           java.awt.Image imgon,
                           java.awt.Image imgover)
Set the state images (one image for each button/mouse state)

Parameters:
imgoff - button icon image when mouse is off
imgon - button icon image when button is pressed
imgover - button icon image when mouse is off

setStateBgImages

public void setStateBgImages(java.awt.Image imgoff,
                             java.awt.Image imgon,
                             java.awt.Image imgover)
Set the background state images (one image for each button/mouse state)

Parameters:
imgoff - button background image when mouse is off
imgon - button background image when button is pressed
imgover - button background image when mouse is off

setImage

public void setImage(java.awt.Image i)
Set the current image icon to be shown.

Parameters:
i - Button Image

setBgImage

public void setBgImage(java.awt.Image i)
Set the current background image to be shown.

Parameters:
i - Button Background Image

setImageScale

public void setImageScale(double f)
Scale the image to the given value (1.0 = 100%).

Parameters:
f - float

setImageScale

public void setImageScale(float pct)
Scale the image to the given value (1.0 = 100%).

Parameters:
pct - float

setEnable

public void setEnable(boolean a)
Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially by default.

Parameters:
a - If true, this component is enabled; otherwise this component is disabled

resize

public void resize()
Resize the size of button and components.


repaintImages

protected void repaintImages(java.awt.Graphics gbuffer)
Redraw the button images

Parameters:
gbuffer - current graphic context

paint

public void paint(java.awt.Graphics g)
Draw button elements (border, image and label) at calculated positions

Overrides:
paint in class java.awt.Container
Parameters:
g - the graphic area when diplay button elements

setActionCommand

public void setActionCommand(java.lang.String command)
Sets the command name of the action event fired by this button.

Parameters:
command - The name of the action event command fired by this button

getActionCommand

public java.lang.String getActionCommand()
Returns the command name of the action event fired by this button.

Returns:
the action command name

addActionListener

public void addActionListener(java.awt.event.ActionListener l)
Adds the specified action listener to receive action events from this button.

Parameters:
l - the action listener

removeActionListener

public void removeActionListener(java.awt.event.ActionListener l)
Removes the specified action listener so it no longer receives action events from this button.

Parameters:
l - the action listener

fireActionEvent

protected void fireActionEvent(int actiontype)
Fire an action event to the listeners.

Parameters:
actiontype - ActionEvent.ACTION_PERFORMED,

imageButton_MousePushed

protected void imageButton_MousePushed()
Gets called when the mouse button is pressed. Repaint button setting images and colors.


imageButton_MouseOver

protected void imageButton_MouseOver(boolean sound_on)
Gets called when the mouse crosses into the button area. Repaint button setting images and colors.

Parameters:
sound_on - boolean to turn off sound

imageButton_MouseOff

protected void imageButton_MouseOff()
Gets called when the mouse crosses out of the button area. Repaint button setting images and colors.


setButtonComponents

private void setButtonComponents(java.applet.AudioClip sound,
                                 java.awt.Color foreground,
                                 java.awt.Color background,
                                 java.awt.Color shadow,
                                 java.awt.Image bg,
                                 java.awt.Image icon)
Set and repaint button components

Parameters:
sound - audioclip to play
foreground - foreground color
background - background color
shadow - text shadow color
bg - background image
icon - image icon