2015-04-20 15:36:01 -07:00
|
|
|
//: gui/ColorBoxes.java
|
|
|
|
// A visual demonstration of threading.
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.*;
|
|
|
|
import static net.mindview.util.SwingConsole.*;
|
|
|
|
|
|
|
|
class CBox extends JPanel implements Runnable {
|
|
|
|
private int pause;
|
|
|
|
private static Random rand = new Random();
|
|
|
|
private Color color = new Color(0);
|
|
|
|
public void paintComponent(Graphics g) {
|
|
|
|
g.setColor(color);
|
|
|
|
Dimension s = getSize();
|
|
|
|
g.fillRect(0, 0, s.width, s.height);
|
|
|
|
}
|
|
|
|
public CBox(int pause) { this.pause = pause; }
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
while(!Thread.interrupted()) {
|
2015-04-29 12:53:35 -07:00
|
|
|
color = new Color(rand.nextInt(0x1000000));
|
2015-04-20 15:36:01 -07:00
|
|
|
repaint(); // Asynchronously request a paint()
|
|
|
|
TimeUnit.MILLISECONDS.sleep(pause);
|
|
|
|
}
|
|
|
|
} catch(InterruptedException e) {
|
|
|
|
// Acceptable way to exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ColorBoxes extends JFrame {
|
|
|
|
private int grid = 12;
|
|
|
|
private int pause = 50;
|
|
|
|
private static ExecutorService exec =
|
|
|
|
Executors.newCachedThreadPool();
|
|
|
|
public void setUp() {
|
|
|
|
setLayout(new GridLayout(grid, grid));
|
|
|
|
for(int i = 0; i < grid * grid; i++) {
|
|
|
|
CBox cb = new CBox(pause);
|
|
|
|
add(cb);
|
|
|
|
exec.execute(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
ColorBoxes boxes = new ColorBoxes();
|
|
|
|
if(args.length > 0)
|
|
|
|
boxes.grid = new Integer(args[0]);
|
|
|
|
if(args.length > 1)
|
|
|
|
boxes.pause = new Integer(args[1]);
|
|
|
|
boxes.setUp();
|
|
|
|
run(boxes, 500, 400);
|
|
|
|
}
|
2015-05-05 11:20:13 -07:00
|
|
|
} ///:~
|