/* * XPixButton.java -- * * button with a picture and a label. * * Copyright 1996 Regents of the University of California * Permission to use, copy, modify, and distribute this * software and its documentation for any purpose and without * fee is hereby granted, provided that this copyright * notice appears in all copies. The University of California * makes no representations about the suitability of this * software for any purpose. It is provided "as is" without * express or implied warranty. * * rcsid = "$Header: /disks/barad-dur/now/rywang/src/java/classes/ryw/XPict.java,v 1.1 1995/09/27 07:33:22 rywang Exp $ xFS (Berkeley)" */ package ryw; import java.awt.*; import java.awt.image.*; /** * A button with a picture and a label. * I can't extend the Button object because it's too closely tied to * its peer implementation. */ public class XPixButton extends Canvas { protected Image image; protected boolean down; protected String label; public XPixButton (String label, Image image) { this.label = label; this.image = image; } public String getLabel () { return label; } public void paint (Graphics g) { Rectangle bounds = bounds (); Font font = g.getFont (); Font newFont; FontMetrics fm; String label = getLabel (); g.setColor (down ? Color.gray : Color.lightGray); g.fill3DRect (0, 0, bounds.width, bounds.height, !down); g.setFont (newFont = new Font (font.getName (), font.BOLD, 36)); g.setColor (down ? Color.yellow : Color.blue); g.drawImage (image, 0, 0, this); fm = g.getFontMetrics (newFont); g.drawString (label, (bounds.width - fm.stringWidth (label)) / 2 - 6, 40); } public void setDown(boolean down) { if (down != this.down) { this.down = down; repaint (); } } public void pop () { if (!down) return; down = false; repaint (); } public boolean mouseDown (Event ev, int x, int y) { String event; if (!inside (x, y)) return true; event = down ? "off" : "on"; setDown (!down); getParent ().action (ev, event); return true; } public boolean mouseDrag(Event ev, int x, int y) { return true; } public boolean mouseUp(Event ev, int x, int y) { return true; } }