S O U R C E C O D E
/ 1997 / 01
/ java
/ java.lst
"Java Alley"
by Bruce Eckel
Web Techniques, January 1997
Web Techniques grants permission to use these listings for private or
commercial use provided that credit to Web Techniques and the author is
maintained within the comments of the source. For questions, contact
editors@web-techniques.com.
[LISTING ONE]
import java.awt.*;
public class ColorBoxes extends Frame {
public ColorBoxes(int pause, int grid) {
setTitle("ColorBoxes");
setLayout(new GridLayout(grid, grid));
for (int i = 0; i < grid * grid; i++)
add(new CBox(pause));
}
public boolean handleEvent(Event evt) {
if (evt.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(evt);
}
public static void main(String[] args) {
int pause = 50;
int grid = 8;
if(args.length > 0)
pause = Integer.parseInt(args[0]);
if(args.length > 1)
grid = Integer.parseInt(args[1]);
Frame f = new ColorBoxes(pause, grid);
f.resize(500, 400);
f.show();
}
}
class CBox extends Canvas implements Runnable {
Thread t;
int pause;
static final Color colors[] = {
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
};
Color cColor = newColor();
static final Color newColor() {
return colors[
(int)(Math.random() * colors.length)
];
}
public void paint(Graphics g) {
g.setColor(cColor);
Dimension s = size();
g.fillRect(0, 0, s.width, s.height);
}
CBox(int Pause) {
pause = Pause;
t = new Thread(this);
t.start();
}
public void run() {
while(true) {
cColor = newColor();
repaint();
try {
t.sleep(pause);
} catch(InterruptedException e) {}
}
}
}
[LISTING TWO]
import java.awt.*;
import java.util.*;
public class ColorBoxes2 extends Frame {
CBoxVector v[];
public ColorBoxes2(int pause, int grid) {
setTitle("ColorBoxes2");
setLayout(new GridLayout(grid, grid));
v = new CBoxVector[grid];
for(int i = 0; i < grid; i++)
v[i] = new CBoxVector(pause);
for (int i = 0; i < grid * grid; i++) {
v[i % grid].addElement(new CBox2());
add((CBox2)v[i % grid].lastElement());
}
for(int i = 0; i < grid; i++)
v[i].go();
}
public boolean handleEvent(Event evt) {
if (evt.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(evt);
}
public static void main(String[] args) {
// Shorter default pause than ColorBoxes:
int pause = 5;
int grid = 8;
if(args.length > 0)
pause = Integer.parseInt(args[0]);
if(args.length > 1)
grid = Integer.parseInt(args[1]);
Frame f = new ColorBoxes2(pause, grid);
f.resize(500, 400);
f.show();
}
}
class CBoxVector
extends Vector implements Runnable {
Thread t;
int pause;
CBoxVector(int Pause) {
pause = Pause;
t = new Thread(this);
}
void go() { t.start(); }
public void run() {
while(true) {
int i = (int)(Math.random() * size());
((CBox2)elementAt(i)).nextColor();
try {
t.sleep(pause);
} catch(InterruptedException e) {}
}
}
}
class CBox2 extends Canvas {
static final Color colors[] = {
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
};
Color cColor = newColor();
static final Color newColor() {
return colors[
(int)(Math.random() * colors.length)
];
}
void nextColor() {
cColor = newColor();
repaint();
}
public void paint(Graphics g) {
g.setColor(cColor);
Dimension s = size();
g.fillRect(0, 0, s.width, s.height);
}
}
|
home
|
current issue
|
archives
|
source code
|
demo express
|
events
|
search
|
editorial calendar
|
advertising
|
customer service
|
author guidelines
|
jobs
|
about
Entire contents copyright 1996-2000 CMP Media Inc.
Read our privacy policy.