home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c16 / BoxObserver.java next >
Encoding:
Java Source  |  2000-05-25  |  4.6 KB  |  133 lines

  1. //: BoxObserver.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. // Demonstration of Observer pattern using
  50. // Java's built-in observer classes.
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import java.util.*;
  54.  
  55. // You must inherit a new type of Observable:
  56. class BoxObservable extends Observable {
  57.   public void notifyObservers(Object b) {
  58.     // Otherwise it won't propagate changes:
  59.     setChanged();
  60.     super.notifyObservers(b);
  61.   }
  62. }
  63.  
  64. public class BoxObserver extends Frame {
  65.   Observable notifier = new BoxObservable();
  66.   public BoxObserver(int grid) {
  67.     setTitle("Demonstrates Observer pattern");
  68.     setLayout(new GridLayout(grid, grid));
  69.     for(int x = 0; x < grid; x++)
  70.       for(int y = 0; y < grid; y++)
  71.         add(new OCBox(x, y, notifier));
  72.   }   
  73.   public static void main(String[] args) {
  74.     int grid = 8;
  75.     if(args.length > 0)
  76.       grid = Integer.parseInt(args[0]);
  77.     Frame f = new BoxObserver(grid);
  78.     f.setSize(500, 400);
  79.     f.setVisible(true);
  80.     f.addWindowListener(
  81.       new WindowAdapter() {
  82.         public void windowClosing(WindowEvent e) {
  83.           System.exit(0);
  84.         }
  85.       });
  86.   }
  87. }
  88.  
  89. class OCBox extends Canvas implements Observer {
  90.   Observable notifier;
  91.   int x, y; // Locations in grid
  92.   Color cColor = newColor();
  93.   static final Color[] colors = { 
  94.     Color.black, Color.blue, Color.cyan, 
  95.     Color.darkGray, Color.gray, Color.green,
  96.     Color.lightGray, Color.magenta, 
  97.     Color.orange, Color.pink, Color.red, 
  98.     Color.white, Color.yellow 
  99.   };
  100.   static final Color newColor() {
  101.     return colors[
  102.       (int)(Math.random() * colors.length)
  103.     ];
  104.   }
  105.   OCBox(int x, int y, Observable notifier) {
  106.     this.x = x;
  107.     this.y = y;
  108.     notifier.addObserver(this);
  109.     this.notifier = notifier;
  110.     addMouseListener(new ML());
  111.   }
  112.   public void paint(Graphics  g) {
  113.     g.setColor(cColor);
  114.     Dimension s = getSize();
  115.     g.fillRect(0, 0, s.width, s.height);
  116.   }
  117.   class ML extends MouseAdapter {
  118.     public void mousePressed(MouseEvent e) {
  119.       notifier.notifyObservers(OCBox.this);
  120.     }
  121.   }
  122.   public void update(Observable o, Object arg) {
  123.     OCBox clicked = (OCBox)arg;
  124.     if(nextTo(clicked)) {
  125.       cColor = clicked.cColor;
  126.       repaint();
  127.     }
  128.   }
  129.   private final boolean nextTo(OCBox b) {
  130.     return Math.abs(x - b.x) <= 1 && 
  131.            Math.abs(y - b.y) <= 1;
  132.   }
  133. } ///:~