package awt;

import java.util.*;
import java.awt.*;
import java.awt.event.*;

Button - a class that produces a lightweight Open Look button. Lightweight components can have "transparent" areas, meaning that you can see the background of the container behind them.
public class Button extends Component { static final int capWidth = 20; // The width of the Button's endcap private String label; // The Button's text private boolean pressed = false; // true if the button is detented. private boolean mousePressed = false; // true if user is still pressing button private ActionListener actionListener; // Post action events to listeners
Constructs a Button with no label.
public Button() { this(""); }
Constructs a Button with the specified label. @param label the label of the button
public Button(String label) { this.label = label; enableEvents(AWTEvent.MOUSE_EVENT_MASK); }
gets the label @see setLabel
public String getLabel() { return label; } public void setBackground(Color c) { super.setBackground(c); repaint(); } public void setForeground(Color c) { super.setForeground(c); repaint(); } public void setEnabled(boolean b) { if (b != isEnabled()) { super.setEnabled(b); repaint(); } }
sets the label @see getLabel
public void setLabel(String label) { this.label = label; invalidate(); repaint(); }
factor is e.g. 0.7
private Color darker(Color col,float factor) { return new Color((int)(col.getRed() * factor), (int)(col.getGreen() * factor), (int)(col.getBlue() * factor)); } private Color brighter(Color col,float factor) { return new Color(Math.min((int)(col.getRed() / factor),255), Math.min((int)(col.getGreen() / factor),255), Math.min((int)(col.getBlue() / factor),255)); } public void paint(Graphics g) { int width = getSize().width - 1; int height = getSize().height - 1; Color interior = getBackground(); Color highlight1; Color highlight2; int labelWidth; int capwidth; Font f = getFont(); if (f != null) { FontMetrics fm = getFontMetrics(getFont()); labelWidth = fm.stringWidth(label); capwidth = width - labelWidth; if (capwidth > height) capwidth = height; if (capwidth < capWidth) capwidth = capWidth; if (capwidth > (width*4)/5) capwidth = (width*4)/5; } else { capwidth = capWidth; labelWidth = 0; } // ***** determine what colors to use if(pressed) { highlight1 = darker(interior,0.7f); highlight2 = brighter(interior,0.7f); } else { highlight1 = brighter(interior,0.7f); highlight2 = darker(interior,0.7f); } // ***** paint the interior of the button g.setColor(pressed ? darker(interior,0.85f) : interior); // left cap g.fillArc(0,0, // start capwidth,height, // size 90,180); // angle // right cap g.fillArc(width - capwidth,0, // start capwidth,height, // size 270,180); // angle // inner rectangle g.fillRect(capwidth/2, 0, width - capwidth + 1, height); // ***** highlight the perimeter of the button drawOpenlookOval(g,0,0,width,height,highlight1,highlight2,capwidth); // lower arc right cap g.setColor(highlight2); g.drawArc(width - capwidth,0, // start capwidth,height, // size 270,180-40); // angle // ***** draw the label centered in the button if (f != null) { FontMetrics fm = getFontMetrics(getFont()); g.setColor(getForeground()); g.drawString(label, (width-labelWidth) / 2, (height+fm.getHeight()) / 2 - fm.getMaxDescent() ); // very strange bug in jdk1.2 g.setColor(getBackground()); g.drawLine(0,0,0,0); } if (!isEnabled()) { g.setColor(Color.white); for (int y = 0; y < height; y+=2) { for (int x = (y%4==0) ? 0 : 2; x < width; x+=4) g.drawLine(x,y,x,y); } } }
The preferred size of the button.
public Dimension getPreferredSize() { Font f = getFont(); if (f != null) { FontMetrics fm = getFontMetrics(getFont()); return new Dimension(fm.stringWidth(label) + capWidth/* *2 */,fm.getHeight() + 5); // was +10 (gve) } else { return new Dimension(100, 50); } }
The minimum size of the button.
public Dimension getMinimumSize() { return getPreferredSize(); }
Adds the specified action listener to receive action events from this button. @param listener the action listener
public void addActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.add(actionListener, listener); enableEvents(AWTEvent.MOUSE_EVENT_MASK); }
Removes the specified action listener so it no longer receives action events from this button. @param listener the action listener
public void removeActionListener(ActionListener listener) { actionListener = AWTEventMulticaster.remove(actionListener, listener); }
Paints the button and sends an action event to all listeners.
public void processMouseEvent(MouseEvent e) { if (isEnabled()) switch (e.getID()) { case MouseEvent.MOUSE_PRESSED: // render myself inverted.... pressed = mousePressed = true; // Repaint might flicker a bit. To avoid this, you can use // double buffering (see the Gauge example). repaint(); break; case MouseEvent.MOUSE_RELEASED: // render myself normal again mousePressed = false; if (pressed) { pressed = false; // Repaint might flicker a bit. To avoid this, you can use // double buffering (see the Gauge example). repaint(); if (actionListener!=null) { actionListener.actionPerformed(new ActionEvent( this,ActionEvent.ACTION_PERFORMED,label)); } } break; case MouseEvent.MOUSE_ENTERED: if (!pressed && mousePressed) { pressed = true; repaint(); } break; case MouseEvent.MOUSE_EXITED: if (pressed && mousePressed) { // Cancel! Don't send action event. pressed = false; // Repaint might flicker a bit. To avoid this, you can use // double buffering (see the Gauge example). repaint(); } break; } super.processMouseEvent(e); }
This method works out nice colors given the interior of the oval (e.g. getBackground() of a component)
static public void drawOpenlookOval(Graphics g,int x,int y,int width,int height,Color interior,boolean pressed) { if (pressed) drawOpenlookOval(g,x,y,width,height,interior.darker(),interior.brighter()); else drawOpenlookOval(g,x,y,width,height,interior.brighter(),interior.darker()); } static public void drawOpenlookOval(Graphics g,int x,int y,int width,int height,Color highlight1,Color highlight2) { drawOpenlookOval(g,x,y,width,height,highlight1,highlight2,capWidth); } static public void drawOpenlookOval(Graphics g,int x,int y,int width,int height,Color highlight1,Color highlight2,int capwidth) { // draw upper and lower highlight lines g.setColor(highlight1); g.drawLine(x+capwidth/2,y,x+width-capwidth/2,y); g.setColor(highlight2); g.drawLine(x+capwidth/2,y+height,x+width-capwidth/2,y+height); // upper arc left cap g.setColor(highlight1); g.drawArc(x, y, // start capwidth,height, // size 90, 180-40); // angle // lower arc left cap g.setColor(highlight2); g.drawArc(x,y, // start capwidth, height, // size 270-40, 40); // angle // upper arc right cap g.setColor(highlight1); g.drawArc(x+width - capwidth,y, // start capwidth, height, // size 90-40,40); // angle // lower arc right cap g.setColor(highlight2); g.drawArc(x+width - capwidth,y, // start capwidth,height, // size 270,180-40); // angle } }