001: package com.xoetrope.awt;
002:
003: import java.awt.Color;
004: import java.awt.Component;
005: import java.awt.Dimension;
006: import java.awt.Font;
007: import java.awt.FontMetrics;
008: import java.awt.Graphics;
009: import java.awt.Image;
010: import java.awt.Point;
011:
012: import com.xoetrope.event.XClickListener;
013: import net.xoetrope.xui.XImageHolder;
014: import net.xoetrope.xui.XTextHolder;
015:
016: /**
017: * A button with an associated image icon.
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: * <p>$Revision: 1.2 $</p>
023: */
024: public class XImageButton extends Component implements XImageHolder,
025: XTextHolder {
026: private XClickListener clickListener;
027:
028: private Image toggleImage;
029: private String caption;
030:
031: /**
032: * Constructor for a new XImageButton
033: */
034: public XImageButton() {
035: clickListener = new XClickListener(this );
036:
037: addMouseListener(clickListener);
038: addMouseMotionListener(clickListener);
039: }
040:
041: /**
042: * Renders the image
043: * @param g Graphics g
044: */
045: public void paint(Graphics g) {
046: Dimension d = getSize();
047:
048: g.setColor(getBackground());
049: g.fillRect(0, 0, d.width - 1, d.height - 1);
050:
051: if (!clickListener.getIsSelected()
052: || !clickListener.getIsEntered())
053: super .paint(g);
054: else {
055: if (toggleImage != null)
056: g.drawImage(toggleImage, 0, 0, getBackground(), this );
057: else
058: super .paint(g);
059: }
060:
061: if ((clickListener.getIsEntered()) && (toggleImage != null)) {
062: g.setColor(Color.lightGray);
063: g.drawRect(0, 0, d.width - 1, d.height - 1);
064: }
065:
066: if (caption != null) {
067: // Get the font size
068: Font f = getFont();
069: g.setFont(f);
070: FontMetrics fm = g.getFontMetrics(f);
071:
072: // Determine the position for centering the text.
073: int xLoc = (int) ((d.width - fm.stringWidth(caption)) / 2.0);
074: int yLoc = fm.getAscent() + (d.height - fm.getAscent()) / 2;
075:
076: // Draw the text
077: g.setPaintMode();
078: g.setColor(getForeground());
079: g.drawString(caption, xLoc, yLoc);
080:
081: }
082: }
083:
084: /**
085: * Returns the location at which the mouse was clicked
086: * @return The click location represented as a Point Object
087: */
088: public Point getClickLocation() {
089: return clickListener.getClickLocation();
090: }
091:
092: /**
093: * Sets the text for this button, if any. The text is centered over the image.
094: * This method is also called by the localization methods.
095: * @param newText the new text
096: */
097: public void setText(String newText) {
098: caption = newText;
099: repaint();
100: }
101:
102: /**
103: * Gets the text for this button, if any.
104: * @return the text/caption
105: */
106: public String getText() {
107: return caption;
108: }
109:
110: /**
111: * Sets the image to display
112: * @param img the new image or null to clear.
113: */
114: public void setImage(Image img) {
115: toggleImage = img;
116: repaint();
117: }
118: }
|