Previous <--> Next <==> All
<applet code=StackedRects.class width=150 height=75>
<param name=thick  value=8>
<param name=nrects value=3>
Cannot run the StackedRects applet.
</applet>
Cannot run the StackedRects applet.
<applet code=StackedRects.class width=150 height=75>
<param name=thick  value=8>
<param name=nrects value=3>
<param name=cover  value=true>
Cannot run the StackedRects applet.
</applet>
Cannot run the StackedRects applet.
<applet code=StackedRects.class width=150 height=75>
<param name=thick  value=8>
<param name=nrects value=3>
<param name=center value=true>
<param name=grid   value=false>
Cannot run the StackedRects applet.
</applet>
Cannot run the StackedRects 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 05: add grid and center parameters\n"
+ "Steve Kinzler, kinzler@cs.indiana.edu, Nov 95/Mar 96\n" + url;
	}

	int	 nrects, thick;
	boolean  cover, center, grid;

	Image	 image;
	Graphics imageG;

	String pinfo[][] = {
		{"nrects", "# rects",  "starting rectangles [0]"},
		{"thick",  "# pixels", "line thickness (5%)"},
		{"cover",  "boolean",  "overlap rectangles [false]"},
		{"center", "boolean",  "center rectangles [false]"},
		{"grid",   "boolean",  "constrain to a grid [true]"}
	};

	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("COVER");
		cover  = (s != null) ? s.equals("true") : false;
		s = getParameter("CENTER");
		center = (s != null) ? s.equals("true") : false;
		s = getParameter("GRID");
		grid   = (s != null) ? s.equals("true") : true;
		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() {
		int a, b, x, y;
		int t = thick;

		if (cover || center) {
			double maxa = width  / 2.0 - 2.0 * t;
			double maxb = height / 2.0 - 2.0 * t;
			a =		 (int) (Math.random() * maxa);
			b =		 (int) (Math.random() * maxb);
			x = width  - 1 - (int) (Math.random() * maxa);
			y = height - 1 - (int) (Math.random() * maxb);
			if (center) {
				x = width  - 1 - a;
				y = height - 1 - b;
			}
			if (grid) {
				a -= a % t; x += (width  - 1 - x) % t;
				b -= b % t; y += (height - 1 - y) % t;
			}
		} else {
			int maxa = width  - 4 * t + 1;
			int maxb = height - 4 * t + 1;
			a =		 (int) (Math.random() * maxa);
			b =		 (int) (Math.random() * maxb);
			x = width  - 1 - (int) (Math.random() * (maxa - a));
			y = height - 1 - (int) (Math.random() * (maxb - b));
			if (Math.random() >= 0.5) {
				int c = a, d = b;
				a = width - 1 - x; b = height - 1 - y;
				x = width - 1 - c; y = height - 1 - d;
			}
			if (grid) {
				a -= a % t; x -= (x + 1) % t;
				b -= b % t; y -= (y + 1) % t;
			}
		}

		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);
	}
}