home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / bangbean / BangBean.java
Encoding:
Java Source  |  2000-05-25  |  5.4 KB  |  156 lines

  1. //: BangBean.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // A graphical Bean
  50. package bangbean;
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import java.io.*;
  54. import java.util.*;
  55.  
  56. public class BangBean extends Canvas
  57.      implements Serializable {
  58.   protected int xm, ym;
  59.   protected int cSize = 20; // Circle size
  60.   protected String text = "Bang!";
  61.   protected int fontSize = 48;
  62.   protected Color tColor = Color.red;
  63.   protected ActionListener actionListener;
  64.   public BangBean() {
  65.     addMouseListener(new ML());
  66.     addMouseMotionListener(new MML());
  67.   }
  68.   public int getCircleSize() { return cSize; }
  69.   public void setCircleSize(int newSize) {
  70.     cSize = newSize;
  71.   }
  72.   public String getBangText() { return text; }
  73.   public void setBangText(String newText) {
  74.     text = newText;
  75.   }
  76.   public int getFontSize() { return fontSize; }
  77.   public void setFontSize(int newSize) {
  78.     fontSize = newSize;
  79.   }
  80.   public Color getTextColor() { return tColor; }
  81.   public void setTextColor(Color newColor) {
  82.     tColor = newColor;
  83.   }
  84.   public void paint(Graphics g) {
  85.     g.setColor(Color.black);
  86.     g.drawOval(xm - cSize/2, ym - cSize/2, 
  87.       cSize, cSize);
  88.   }
  89.   // This is a unicast listener, which is
  90.   // the simplest form of listener management:
  91.   public void addActionListener (
  92.       ActionListener l) 
  93.         throws TooManyListenersException {
  94.     if(actionListener != null)
  95.       throw new TooManyListenersException();
  96.     actionListener = l;
  97.   }
  98.   public void removeActionListener(
  99.       ActionListener l) {
  100.     actionListener = null;
  101.   }
  102.   class ML extends MouseAdapter {
  103.     public void mousePressed(MouseEvent e) {
  104.       Graphics g = getGraphics();
  105.       g.setColor(tColor);
  106.       g.setFont(
  107.         new Font(
  108.           "TimesRoman", Font.BOLD, fontSize));
  109.       int width = 
  110.         g.getFontMetrics().stringWidth(text);
  111.       g.drawString(text, 
  112.         (getSize().width - width) /2,
  113.         getSize().height/2);
  114.       g.dispose();
  115.       // Call the listener's method:
  116.       if(actionListener != null)
  117.         actionListener.actionPerformed(
  118.           new ActionEvent(BangBean.this,
  119.             ActionEvent.ACTION_PERFORMED, null));
  120.     }
  121.   }
  122.   class MML extends MouseMotionAdapter {
  123.     public void mouseMoved(MouseEvent e) {
  124.       xm = e.getX();
  125.       ym = e.getY();
  126.       repaint();
  127.     }
  128.   }
  129.   public Dimension getPreferredSize() {
  130.     return new Dimension(200, 200);
  131.   }
  132.   // Testing the BangBean:
  133.   public static void main(String[] args) {
  134.     BangBean bb = new BangBean();
  135.     try {
  136.       bb.addActionListener(new BBL());
  137.     } catch(TooManyListenersException e) {}
  138.     Frame aFrame = new Frame("BangBean Test");
  139.     aFrame.addWindowListener(
  140.       new WindowAdapter() {
  141.         public void windowClosing(WindowEvent e) {
  142.           System.exit(0);
  143.         }
  144.       });
  145.     aFrame.add(bb, BorderLayout.CENTER);
  146.     aFrame.setSize(300,300);
  147.     aFrame.setVisible(true);
  148.   }
  149.   // During testing, send action information
  150.   // to the console:
  151.   static class BBL implements ActionListener {
  152.     public void actionPerformed(ActionEvent e) {
  153.       System.out.println("BangBean action");
  154.     }
  155.   }
  156. } ///:~