FileDocCategorySizeDatePackage
OpenlookButton.javaAPI DocExample6786Wed Apr 19 11:21:54 BST 2000actual

OpenlookButton

public class OpenlookButton extends Component
OpenlookButton - a class that produces a lightweight button. Lightweight components can have "transparent" areas, meaning that you can see the background of the container behind them.

Fields Summary
static int
capWidth
String
label
protected boolean
pressed
ActionListener
actionListener
Constructors Summary
public OpenlookButton()
Constructs an OpenlookButton with no label.

     // Post action events to listeners
  
  
           
    
      this("");
  
public OpenlookButton(String label)
Constructs an OpenlookButton with the specified label.

param
label the label of the button

      this.label = label;
      enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  
Methods Summary
public voidaddActionListener(java.awt.event.ActionListener listener)
Adds the specified action listener to receive action events from this button.

param
listener the action listener

       actionListener = AWTEventMulticaster.add(actionListener, listener);
       enableEvents(AWTEvent.MOUSE_EVENT_MASK);
   
public java.lang.StringgetLabel()
gets the label

see
setLabel

      return label;
  
public java.awt.DimensiongetMinimumSize()
The minimum size of the button.

      return new Dimension(100, 50);
  
public java.awt.DimensiongetPreferredSize()
The preferred size of the button.

      Font f = getFont();
      if(f != null) {
	  FontMetrics fm = getFontMetrics(getFont());
	  return new Dimension(fm.stringWidth(label) + capWidth*2,
			       fm.getHeight() + 10);
      } else {
	  return new Dimension(100, 50);
      }
  
public voidpaint(java.awt.Graphics g)
paints the button

      int width = getSize().width - 1;
      int height = getSize().height - 1;

      Color interior;
      Color highlight1;
      Color highlight2;
      
      interior = getBackground();

      // ***** determine what colors to use
      if(pressed) {
	  highlight1 = interior.darker();
	  highlight2 = interior.brighter();
      } else {
	  highlight1 = interior.brighter();
	  highlight2 = interior.darker();
      }

      // ***** paint the interior of the button
      g.setColor(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, height);
      

      // ***** highlight the perimeter of the button
      // draw upper and lower highlight lines
      g.setColor(highlight1);
      g.drawLine(capWidth/2, 0, width - capWidth/2, 0);
      g.setColor(highlight2);
      g.drawLine(capWidth/2, height, width - capWidth/2, height);

      // upper arc left cap
      g.setColor(highlight1);
      g.drawArc(0, 0,                       // start
                capWidth, height,           // size
                90, 180-40                  // angle
      );
 
      // lower arc left cap
      g.setColor(highlight2);
      g.drawArc(0, 0,                       // start
                capWidth, height,           // size
                270-40, 40                  // angle
      );

      // upper arc right cap
      g.setColor(highlight1);
      g.drawArc(width - capWidth, 0,        // start
                capWidth, height,           // size
                90-40, 40                   // angle
      );
 
      // 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
      Font f = getFont();
      if(f != null) {
	  FontMetrics fm = getFontMetrics(getFont());
	  g.setColor(getForeground());
	  g.drawString(label,
		       width/2 - fm.stringWidth(label)/2,
		       height/2 + fm.getHeight()/2 - fm.getMaxDescent()
	  );
      }
  
public voidprocessMouseEvent(java.awt.event.MouseEvent e)
Paints the button and sends an action event to all listeners.

       Graphics g;
       switch(e.getID()) {
          case MouseEvent.MOUSE_PRESSED:
            // render myself inverted....
            pressed = true;
	
	    // Repaint might flicker a bit. To avoid this, you can use
	    // double buffering (see the Gauge example).
	    repaint();
            break;
          case MouseEvent.MOUSE_RELEASED:
            if(actionListener != null) {
               actionListener.actionPerformed(new ActionEvent(
                   this, ActionEvent.ACTION_PERFORMED, label));
            }
            // render myself normal again
            if(pressed == true) {
                pressed = false;

	        // Repaint might flicker a bit. To avoid this, you can use
	        // double buffering (see the Gauge example).
		repaint();
            }
            break;
          case MouseEvent.MOUSE_ENTERED:
 
            break;
          case MouseEvent.MOUSE_EXITED:
            if(pressed == true) {
                // 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();

                // Note: for a more complete button implementation,
                // you wouldn't want to cancel at this point, but
                // rather detect when the mouse re-entered, and
                // re-highlight the button. There are a few state
                // issues that that you need to handle, which we leave
                // this an an excercise for the reader (I always
                // wanted to say that!)
            }
            break;
       }
       super.processMouseEvent(e);
   
public voidremoveActionListener(java.awt.event.ActionListener listener)
Removes the specified action listener so it no longer receives action events from this button.

param
listener the action listener

       actionListener = AWTEventMulticaster.remove(actionListener, listener);
   
public voidsetLabel(java.lang.String label)
sets the label

see
getLabel

      this.label = label;
      invalidate();
      repaint();