

package bean1;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Boton extends Canvas implements java.io.Serializable{
  private String titulo;
  private Color color;
  private static final int MARGEN_X=12;
  private static final int MARGEN_Y=8;
  private transient Vector actionListeners;
  boolean bPulsado=false;
  public Boton() {
    try  {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    setSize(60,40);
    this.titulo="Bean";
    color=Color.yellow;
    setFont(new Font("Dialog", Font.BOLD, 12));
    this.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(MouseEvent e) {
        this_mousePressed(e);
      }
      public void mouseReleased(MouseEvent e) {
        this_mouseReleased(e);
      }
    });
  }

  public void setTitulo(String newTitulo) {
    titulo = newTitulo;
    Dimension d = getPreferredSize();
    setSize(d.width, d.height);
    invalidate();
  }

  public String getTitulo() {
    return titulo;
  }

  public void setColor(Color newColor) {
    color = newColor;
    repaint();
  }

  public Color getColor() {
    return color;
  }
  public synchronized void paint(Graphics g) {
    int ancho=getSize().width;
    int alto=getSize().height;

    g.setColor(color);
    g.fillRect(1, 1, ancho-2, alto-2);
    g.draw3DRect(0, 0, ancho-1, alto-1, false);

    g.setColor(getForeground());
    g.setFont(getFont());

    g.drawRect(2, 2, ancho-4, alto-4);
    FontMetrics fm = g.getFontMetrics();
    g.drawString(titulo, (ancho-fm.stringWidth(titulo))/2, (alto+fm.getMaxAscent()-fm.getMaxDescent())/2);
 }

   public Dimension getPreferredSize() {
    FontMetrics fm=getFontMetrics(getFont());
    return new Dimension(fm.stringWidth(titulo)+MARGEN_X, fm.getMaxAscent()+fm.getMaxDescent()+MARGEN_Y);
  }

  public Dimension getMinimumSize() {
    return getPreferredSize();
  }

  void this_mousePressed(MouseEvent e) {
    bPulsado=true;
  }

  void this_mouseReleased(MouseEvent e) {
    if(bPulsado){
        bPulsado=false;
        ActionEvent ev=new ActionEvent(e.getSource(), e.getID(), "acción");
        fireActionPerformed(ev);
    }
  }

  public synchronized void removeActionListener(ActionListener l) {
    if (actionListeners != null && actionListeners.contains(l)) {
      Vector v = (Vector) actionListeners.clone();
      v.removeElement(l);
      actionListeners = v;
    }
  }

  public synchronized void addActionListener(ActionListener l) {
    Vector v = actionListeners == null ? new Vector(2) : (Vector) actionListeners.clone();
    if (!v.contains(l)) {
      v.addElement(l);
      actionListeners = v;
    }
  }

  protected void fireActionPerformed(ActionEvent e) {
    if (actionListeners != null) {
      Vector listeners = actionListeners;
      int count = listeners.size();
      for (int i = 0; i < count; i++)
        ((ActionListener) listeners.elementAt(i)).actionPerformed(e);
    }
  }

}

