Previous <-->
Next <==>
All
<applet code=StackedRects.class width=150 height=75>
Cannot run the StackedRects applet.
</applet>
<applet code=StackedRects.class width=150 height=75>
<param name=thick value=8>
<param name=nrects value=3>
Cannot run the StackedRects applet.
</applet>
import java.applet.Applet;
import java.awt.*;
///////////////////////////////////////////////////////////////////////////////
// StackedRects // Declaration ////////////////////////////////////////////////
public class StackedRects extends Applet {
int width = 480, height = 280; // 4x4 minimum
String url = "http://www.cs.indiana.edu/hyplan/kinzler/fun/"
+ "StackedRects/index.html";
public String getAppletInfo() {
return
"StackedRects - a continuous, random graphic of overlaying rectangles\n"
+ "Development version 04: add nrects parameter\n"
+ "Steve Kinzler, kinzler@cs.indiana.edu, Nov 95/Mar 96\n" + url;
}
int nrects, thick;
Image image;
Graphics imageG;
String pinfo[][] = {
{"nrects", "# rects", "starting rectangles [0]"},
{"thick", "# pixels", "line thickness (5%)"}
};
public String[][] getParameterInfo() {
return pinfo;
}
// StackedRects // Initialization /////////////////////////////////////////////
public void init() {
Dimension d = size();
if (d.width >= 4) width = d.width;
if (d.height >= 4) height = d.height;
resize(width, height);
setParams();
image = createImage(width, height);
imageG = image.getGraphics();
clear();
stackRect(nrects);
}
void setParams() {
String s = getParameter("NRECTS");
nrects = (s != null) ? Math.abs(Integer.parseInt(s)) : 0;
s = getParameter("THICK");
if (s != null) {
int p = Math.max(1, Math.abs(Integer.parseInt(s)));
thick = (p <= Math.min(width, height) / 4) ? p :
Math.max(1, Math.min(width, height) / 20);
} else
thick = Math.max(1, Math.min(width, height) / 20);
}
// StackedRects // Graphics ///////////////////////////////////////////////////
public synchronized void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
synchronized void clear() {
imageG.setColor(getBackground());
imageG.fillRect(0, 0, width, height);
}
void stackRect(int n) {
for (int i = 0; i < n; i++) stackRect();
}
synchronized void stackRect() {
double maxa = width / 2.0 - 2.0 * thick;
double maxb = height / 2.0 - 2.0 * thick;
int a = (int) (Math.random() * maxa);
int b = (int) (Math.random() * maxb);
int x = width - 1 - (int) (Math.random() * maxa);
int y = height - 1 - (int) (Math.random() * maxb);
int t = thick;
int w = x - a + 1;
int h = y - b + 1;
imageG.setColor(getForeground());
imageG.fillRect(a, b, w, t);
imageG.fillRect(a, y-t+1, w, t);
imageG.fillRect(a, b, t, h);
imageG.fillRect(x-t+1, b, t, h);
imageG.setColor(getBackground());
imageG.fillRect(a+t, b+t, w-t-t, t);
imageG.fillRect(a+t, y-t-t+1, w-t-t, t);
imageG.fillRect(a+t, b+t, t, h-t-t);
imageG.fillRect(x-t-t+1, b+t, t, h-t-t);
}
}