Source of BangBean.java


  1: //: bangbean:BangBean.java
  2: // From 'Thinking in Java, 2nd ed.' by Bruce Eckel
  3: // www.BruceEckel.com. See copyright notice in CopyRight.txt.
  4: // A graphical Bean.
  5: package bangbean;
  6: import javax.swing.*;
  7: import java.awt.*;
  8: import java.awt.event.*;
  9: import java.io.*;
 10: import java.util.*;
 11: import com.bruceeckel.swing.*;

 13: public class BangBean extends JPanel
 14:      implements Serializable {
 15:   protected int xm, ym;
 16:   protected int cSize = 20; // Circle size
 17:   protected String text = "Bang!";
 18:   protected int fontSize = 48;
 19:   protected Color tColor = Color.red;
 20:   protected ActionListener actionListener;
 21:   public BangBean() {
 22:     addMouseListener(new ML());
 23:     addMouseMotionListener(new MML());
 24:   }
 25:   public int getCircleSize() { return cSize; }
 26:   public void setCircleSize(int newSize) {
 27:     cSize = newSize;
 28:   }
 29:   public String getBangText() { return text; }
 30:   public void setBangText(String newText) {
 31:     text = newText;
 32:   }
 33:   public int getFontSize() { return fontSize; }
 34:   public void setFontSize(int newSize) {
 35:     fontSize = newSize;
 36:   }
 37:   public Color getTextColor() { return tColor; }
 38:   public void setTextColor(Color newColor) {
 39:     tColor = newColor;
 40:   }
 41:   public void paintComponent(Graphics g) {
 42:     super.paintComponent(g);
 43:     g.setColor(Color.black);
 44:     g.drawOval(xm - cSize/2, ym - cSize/2, 
 45:       cSize, cSize);
 46:   }
 47:   // This is a unicast listener, which is
 48:   // the simplest form of listener management:
 49:   public void addActionListener (
 50:       ActionListener l) 
 51:         throws TooManyListenersException {
 52:     if(actionListener != null)
 53:       throw new TooManyListenersException();
 54:     actionListener = l;
 55:   }
 56:   public void removeActionListener(
 57:       ActionListener l) {
 58:     actionListener = null;
 59:   }
 60:   class ML extends MouseAdapter {
 61:     public void mousePressed(MouseEvent e) {
 62:       Graphics g = getGraphics();
 63:       g.setColor(tColor);
 64:       g.setFont(
 65:         new Font(
 66:           "TimesRoman", Font.BOLD, fontSize));
 67:       int width = 
 68:         g.getFontMetrics().stringWidth(text);
 69:       g.drawString(text, 
 70:         (getSize().width - width) /2,
 71:         getSize().height/2);
 72:       g.dispose();
 73:       // Call the listener's method:
 74:       if(actionListener != null)
 75:         actionListener.actionPerformed(
 76:           new ActionEvent(BangBean.this,
 77:             ActionEvent.ACTION_PERFORMED, null));
 78:     }
 79:   }
 80:   class MML extends MouseMotionAdapter {
 81:     public void mouseMoved(MouseEvent e) {
 82:       xm = e.getX();
 83:       ym = e.getY();
 84:       repaint();
 85:     }
 86:   }
 87:   public Dimension getPreferredSize() {
 88:     return new Dimension(200, 200);
 89:   }
 90: } ///:~