2017-01-14 07:34:50 -08:00
|
|
|
// patterns/BoxObserver.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2021-03-04 16:15:04 -07:00
|
|
|
// Demonstration of the Observer pattern using
|
2020-10-07 14:16:29 -06:00
|
|
|
// Java's built-in observer classes.
|
|
|
|
// {ExcludeFromGradle} // Won't work under WSL2
|
2015-06-15 17:47:35 -07:00
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.*;
|
|
|
|
import java.util.*;
|
2016-07-07 14:58:56 -06:00
|
|
|
import onjava.*;
|
2015-11-11 20:20:04 -08:00
|
|
|
import onjava.MouseClick;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
2020-10-07 17:06:42 -06:00
|
|
|
@SuppressWarnings("deprecation")
|
2021-03-04 16:15:04 -07:00
|
|
|
class Boxes extends JFrame {
|
|
|
|
Observable notifier = new Observable() {
|
|
|
|
@Override
|
|
|
|
public void notifyObservers(Object b) {
|
|
|
|
setChanged();
|
|
|
|
super.notifyObservers(b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public Boxes(int grid) {
|
2015-06-15 17:47:35 -07:00
|
|
|
setTitle("Demonstrates Observer pattern");
|
|
|
|
Container cp = getContentPane();
|
|
|
|
cp.setLayout(new GridLayout(grid, grid));
|
|
|
|
for(int x = 0; x < grid; x++)
|
|
|
|
for(int y = 0; y < grid; y++)
|
2021-03-04 16:15:04 -07:00
|
|
|
cp.add(new Box(x, y, notifier));
|
|
|
|
setSize(500, 400);
|
|
|
|
setVisible(true);
|
|
|
|
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 17:06:42 -06:00
|
|
|
@SuppressWarnings("deprecation")
|
2021-03-04 16:15:04 -07:00
|
|
|
class Box extends JPanel implements Observer {
|
|
|
|
int x, y; // Location in grid
|
|
|
|
Color color = COLORS[
|
|
|
|
(int)(Math.random() * COLORS.length)
|
|
|
|
];
|
2016-11-21 12:37:57 -08:00
|
|
|
static final Color[] COLORS = {
|
2015-06-15 17:47:35 -07:00
|
|
|
Color.black, Color.blue, Color.cyan,
|
|
|
|
Color.darkGray, Color.gray, Color.green,
|
|
|
|
Color.lightGray, Color.magenta,
|
|
|
|
Color.orange, Color.pink, Color.red,
|
|
|
|
Color.white, Color.yellow
|
|
|
|
};
|
2021-03-04 16:15:04 -07:00
|
|
|
Box(int x, int y, Observable notifier) {
|
2015-06-15 17:47:35 -07:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
notifier.addObserver(this);
|
2015-11-03 12:00:44 -08:00
|
|
|
addMouseListener((MouseClick)
|
2021-03-04 16:15:04 -07:00
|
|
|
e -> notifier.notifyObservers(Box.this));
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public void paintComponent(Graphics g) {
|
2015-06-15 17:47:35 -07:00
|
|
|
super.paintComponent(g);
|
2021-03-04 16:15:04 -07:00
|
|
|
g.setColor(color);
|
2015-06-15 17:47:35 -07:00
|
|
|
Dimension s = getSize();
|
|
|
|
g.fillRect(0, 0, s.width, s.height);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void update(Observable o, Object arg) {
|
2021-03-04 16:15:04 -07:00
|
|
|
Box clicked = (Box)arg;
|
2015-06-15 17:47:35 -07:00
|
|
|
if(nextTo(clicked)) {
|
2021-03-04 16:15:04 -07:00
|
|
|
color = clicked.color;
|
2015-06-15 17:47:35 -07:00
|
|
|
repaint();
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
private boolean nextTo(Box b) {
|
2015-06-15 17:47:35 -07:00
|
|
|
return Math.abs(x - b.x) <= 1 &&
|
|
|
|
Math.abs(y - b.y) <= 1;
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
2021-03-04 16:15:04 -07:00
|
|
|
|
|
|
|
public class BoxObserver {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
int grid = 8;
|
|
|
|
if(args.length > 0)
|
|
|
|
grid = Integer.parseInt(args[0]);
|
|
|
|
new Boxes(grid);
|
|
|
|
}
|
|
|
|
}
|