package com.itpwebsolutions.awt; import java.awt.Button; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Image; import java.awt.Insets; import java.awt.Rectangle; import java.awt.SystemColor; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.ImageObserver; /** *
Title: DropBox
*Description:
*Copyright: Copyright (c) 2002
*Company: ITP Web Solutions Inc.
* @author Brian Cairns * @version 0.1 */ public class ImageButton extends Button implements ImageObserver, MouseListener { boolean pressed = false; // true if mouse button has been pressed on this button and not released yet boolean inside = false; // true if the mouse cursor is over the button String label; // text label for this button Image enabled, disabled; // images to display to the left of the text Image imageTemp; int iWidth, iHeight; // image width and height int iHgap = 3; // horizontal gap between image and text Rectangle rectBounds = new Rectangle(); Dimension preferredSize; FontMetrics fm; int topOffset, leftOffset; // offsets for image position int textHeight; // ascent height of text int textGutter; // difference between top of image and top of text /** * Global default insets for the button. This was designed to emulate * Windows ME native buttons, suitability for use with other OS's may vary. */ static Insets insets = new Insets( 5, 5, 5, 5 ); /** * These control how much the text and image are offset when the button is drawn * in "pressed" state. This was designed to emulate Windows ME native buttons, * suitability for use with other OS's may vary */ static int hPressOffset = 1; static int vPressOffset = 1; /** * Construct a new ImageButton with the supplied image and text * @param text the text label * @param img the image to display */ public ImageButton( String text, Image img ) { super( text ); label = text; enabled = img; if( img != null ) { iWidth = img.getWidth( this ); iHeight = img.getHeight( this ); disabled = GrayFilter.createDisabledImage( img ); super.setForeground( SystemColor.control ); } else { iHgap = 0; } this.addMouseListener( this ); } /** * Se the horizontal gap between the image and the text * @param h the horizontal gap in pixels */ public void setHgap( int h ) { iHgap = h; } /** * Set insets which will be applied to all image buttons. * This may be useful for customizing ImageButton for different operating * systems * @param ins the new insets */ public static void setGlobalInsets( Insets ins ) { insets = ins; } /** * Set the offsets for displaying a "pressed" button. This may be useful * for customizing ImageButton's pressed look for different operating systems. * @param horiz * @param vert */ public static void setGlobalPressOffsets( int horiz, int vert ) { hPressOffset = horiz; vPressOffset = vert; } /** * paints the image button * @param g the graphics context */ public void paint( Graphics g ) { if( enabled == null ) { super.paint( g ); return; } if( preferredSize == null ) getPreferredSize(); rectBounds = super.getBounds(); fm = g.getFontMetrics(); textHeight = fm.getAscent() - fm.getDescent() - fm.getLeading(); topOffset = insets.top + ( rectBounds.height - iHeight - insets.top - insets.bottom ) / 2; leftOffset = insets.left + ( rectBounds.width - fm.stringWidth( label ) - iWidth - iHgap - insets.left - insets.right ) / 2; textGutter = ( iHeight - textHeight ) / 2; super.paint( g ); if( isEnabled() ) { imageTemp = enabled; g.setColor( SystemColor.textText ); } else { imageTemp = disabled; g.setColor( SystemColor.textInactiveText ); } if( pressed && inside ) { if( imageTemp != null ) g.drawImage( imageTemp, leftOffset + hPressOffset, topOffset + vPressOffset, this ); g.drawString( label, leftOffset + iWidth + iHgap + hPressOffset, topOffset + textGutter + textHeight + vPressOffset ); } else { if( imageTemp != null ) g.drawImage( imageTemp, leftOffset, topOffset, this ); g.drawString( label, leftOffset + iWidth + iHgap, topOffset + textGutter + textHeight ); } } /** * Designed to capture the getPreferredSize() set by the OS, then delete the * label, and add to the preferred size based on the image size * * @return preferred dimensions of this button */ public Dimension getPreferredSize() { if( enabled == null ) return super.getPreferredSize(); if( preferredSize == null ) { preferredSize = super.getPreferredSize(); preferredSize.width += iWidth + iHgap; preferredSize.height = Math.max( iHeight + insets.top + insets.bottom, preferredSize.height ); super.setLabel( null ); } return preferredSize; } public void setLabel( String text ) { label = text; preferredSize = null; super.setLabel( text ); } public String getLabel() { return label; } public boolean imageUpdate( Image img, int infoflags, int x, int y, int width, int height ) { if( infoflags == ALLBITS ) { repaint(); return false; } return true; } public void mouseClicked(MouseEvent e) { pressed = false; } public void mousePressed(MouseEvent e) { pressed = true; } public void mouseReleased(MouseEvent e){ pressed = false; } public void mouseEntered(MouseEvent e) { inside = true; } public void mouseExited(MouseEvent e) { inside = false; repaint(); } }