Previous <--> Next <==> All
-rw-r--r--   1 kinzler  www           912 Apr 11  1996 RectMaker.class
-rw-r--r--   1 kinzler  www          7120 Apr 11  1996 StackedRects.class
-rw-r--r--   1 kinzler  www          7759 May 29  1996 StackedRects.java
<applet code=StackedRects.class width=150 height=75>
Cannot run the StackedRects applet.
</applet>
Cannot run the StackedRects applet.
<applet code=StackedRects.class width=150 height=75>
<param name=outthick value=10>
<param name=inthick  value=3>
Cannot run the StackedRects applet.
</applet>
Cannot run the StackedRects applet.
import java.applet.Applet;
import java.awt.*;
import java.net.*;

///////////////////////////////////////////////////////////////////////////////
// StackedRects // Declaration ////////////////////////////////////////////////

public class StackedRects extends Applet implements Runnable {

	final static int MINSLEEP = 25;
	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 13: define and use a RectMaker object\n"
+ "Steve Kinzler, kinzler@cs.indiana.edu, Nov 95/Mar 96\n" + url;
	}

	int	  nrects, pause;
	boolean   cover, center, grid, freeze;
	RectMaker outrect, inrect;

	int	  num = 1;
	boolean	  innum = false;
	Image	  image;
	Graphics  imageG;
	Thread	  kicker = null;

	String pinfo[][] = {
		{"nrects",   "# rects",  "starting rectangles [0]"},
		{"pause",    "ds",       "pause between rectangles [1]"},
		{"cover",    "boolean",  "overlap rectangles [false]"},
		{"center",   "boolean",  "center rectangles [false]"},
		{"grid",     "boolean",  "constrain to a grid [true]"},
		{"freeze",   "boolean",  "start applet suspended [false]"},
		{"outthick", "# pixels", "outer line thickness (5%)"},
		{"inthick",  "# pixels", "inner 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);

		outrect = new RectMaker(getForeground());
		inrect  = new RectMaker(getBackground());
		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("PAUSE");
		pause  = (s != null) ? Math.abs(Integer.parseInt(s)) : 1;
		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("FREEZE");
		freeze = (s != null) ? s.equals("true") : false;

		setParams(outrect);
		setParams(inrect);
	}

	void setParams(RectMaker rect) {
		boolean out = (rect == outrect);
		String s    = getParameter((out) ? "OUTTHICK" : "INTHICK");
		setThick(rect, (s != null) ? Integer.parseInt(s)
					   : Math.min(width, height) / 20);
	}

	void setThick(RectMaker rect, int t) {
		RectMaker other = ((rect == outrect) ? inrect : outrect);
		int max = Math.min(width, height) / 2 - other.getThick();
		rect.setThick((t < max) ? t : max);
	}

	void addThick(RectMaker rect, int n) {
		setThick(rect, rect.getThick() + n);
	}

// StackedRects // Graphics ///////////////////////////////////////////////////

	public void update(Graphics g) {
		paint(g);
	}

	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  = outrect.getThick();
		int tt = t + inrect.getThick();

		if (cover || center) {
			double maxa = width  / 2.0 - tt;
			double maxb = height / 2.0 - tt;
			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  - 2 * tt + 1;
			int maxb = height - 2 * tt + 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;
			}
		}

		outrect.paint(imageG, a,   b,   x,   y);
		inrect.paint( imageG, a+t, b+t, x-t, y-t);
	}

// StackedRects // Execution //////////////////////////////////////////////////

	public void start() {
		start(! freeze);
	}

	synchronized void start(boolean b) {
		if (kicker == null && b) {
			kicker = new Thread(this, this.getClass().getName());
			kicker.setPriority(Thread.MIN_PRIORITY);
			kicker.start();
		}
	}

	public void run() {
		while (kicker != null) {
			stackRect();
			repaint();
			try { kicker.sleep(MINSLEEP + pause * 100); }
			catch (InterruptedException e) { break; }
		}
	}

	void toggle() {
		if (kicker == null) { start(true); } else { stop(); }
	}

	public synchronized void stop() {
		if (kicker != null) kicker.stop();
		kicker = null;
	}

// StackedRects // Events /////////////////////////////////////////////////////

	public boolean mouseEnter(Event evt, int x, int y) {
		showStatus(url);
		return true;
	}

	public boolean mouseExit(Event evt, int x, int y) {
		showStatus(null);
		return true;
	}

	public boolean mouseUp(Event evt, int x, int y) {
		goHome();
		return true;
	}

	public void goHome() {
		if (url != null) {
			try { getAppletContext().showDocument(new URL(url)); }
			catch (MalformedURLException e) {
				url = null;
				showStatus("Ignoring Malformed URL");
			}
		}
	}

	public synchronized boolean keyDown(Event evt, int key) {
		if ('0' <= key && key <= '9') {
			num   = (innum) ? 10 * num + key - '0' : key - '0';
			innum = true;
			return true;
		}

		switch (key) {
		case 'h':	goHome();	    break;
		case 'i':	setParams();	    break;
		case 'I':	setParams(outrect); break;
		case '\011':	setParams(inrect);  break;
		case '!':	cover  = ! cover;   break;
		case '@':	center = ! center;  break;
		case '#':	grid   = ! grid;    break;
		case 'f':	freeze = ! freeze;  break;
		case 'n':	nrects = Math.max(0, nrects - num); break;
		case 'N':	nrects = nrects + num;		    break;
		case 'p':
		case '+':	pause  = Math.max(0, pause - num);  break;
		case 'P':
		case '-':	pause  = pause + num;		    break;
		case 't':	addThick(inrect,   num);
		case 'T':	addThick(outrect,  num); break;
		case '\024':	addThick(inrect,   num); break;
		case 'y':	addThick(inrect,  -num);
		case 'Y':	addThick(outrect, -num); break;
		case '\031':	addThick(inrect,  -num); break;
		case 'c':	clear(); stackRect(nrects); repaint(); break;
		case 'C':	clear();		    repaint(); break;
		case '\f':				    repaint(); break;
		case ' ':		 stackRect(num);    repaint(); break;
		case '\r':
		default:	toggle();
		}

		num   = 1;
		innum = false;
		return true;
	}
}

///////////////////////////////////////////////////////////////////////////////
// RectMaker //////////////////////////////////////////////////////////////////

class RectMaker {
	private int thick = 1;
	Color	    color = null;

	RectMaker(Color c) {
		color = c;
	}

	RectMaker(int t, Color c) {
		this(c);
		setThick(t);
	}

	int  getThick()      { return thick; }
	void setThick(int t) { thick = Math.max(1, t); }

	void paint(Graphics g, int a, int b, int x, int y) {
		int t = thick, w = x - a + 1, h = y - b + 1;

		if (color != null) g.setColor(color);
		g.fillRect(a,     b,     w, t);
		g.fillRect(a,     y-t+1, w, t);
		g.fillRect(a,     b,     t, h);
		g.fillRect(x-t+1, b,     t, h);
	}
}