Previous <--> Next <==> All
<applet code=StackedRects.class width=150 height=75>
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
	int thick  = 10;
	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 02: use browser background as applet background\n"
+ "Steve Kinzler, kinzler@cs.indiana.edu, Nov 95/Mar 96\n" + url;
	}

	Image	 image;
	Graphics imageG;

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

		image  = createImage(width, height);
		imageG = image.getGraphics();
		clear();
		stackRect();
	}

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

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