FileDocCategorySizeDatePackage
CLSFractal.javaAPI DocExample10430Sat Sep 12 03:01:00 BST 1998None

CLSFractal

public class CLSFractal extends Applet implements Runnable
A (not-yet) Context sensitive L-System Fractal applet class. The rules for the Context L-system are read from the java.applet.Applet's attributes and then the system is iteratively applied for the given number of levels, possibly drawing each generation as it is generated. Note that the ContextLSystem class does not yet handle the lContext and rContext attributes, although this class is already designed to parse the '[' and ']' characters typically used in Context sensitive L-Systems.
author
Jim Graham
version
1.1f, 27 Mar 1995

Fields Summary
ContextLSystem
cls
int
fractLevel
int
repaintDelay
boolean
incrementalUpdates
float
startAngle
float
rotAngle
float
Xmin
float
Xmax
float
Ymin
float
Ymax
int
border
boolean
normalizescaling
Thread
kicker
String
savedPath
Constructors Summary
Methods Summary
voidincludePt(float x, float y)

	if (x < Xmin)
	    Xmin = x;
	if (x > Xmax)
	    Xmax = x;
	if (y < Ymin)
	    Ymin = y;
	if (y > Ymax)
	    Ymax = y;
    
public voidinit()


       
	String s;
	cls = new ContextLSystem(this);
	s = getParameter("level");
	if (s != null) fractLevel = Integer.parseInt(s);
	s = getParameter("incremental");
	if (s != null) incrementalUpdates = s.equals("true");
	s = getParameter("delay");
	if (s != null) repaintDelay = Integer.parseInt(s);
	s = getParameter("startAngle");
	if (s != null) startAngle = Float.valueOf(s).floatValue();
	s = getParameter("rotAngle");
	if (s != null) rotAngle = Float.valueOf(s).floatValue();
	rotAngle = rotAngle / 360 * 2 * 3.14159265358f;
	s = getParameter("border");
	if (s != null) border = Integer.parseInt(s);
	s = getParameter("normalizescale");
	if (s != null) normalizescaling = s.equals("true");
    
public booleanmouseUp(java.awt.Event evt, int x, int y)

	cls = new ContextLSystem(this);
	savedPath = null;
	start();
	return true;
    
public voidpaint(java.awt.Graphics g)

	String fractalPath = cls.getPath();
	if (fractalPath == null) {
	    super.paint(g);
	    return;
	}
	if (savedPath == null || !savedPath.equals(fractalPath)) {
	    savedPath = fractalPath;
	    render(null, fractalPath);
	}

	for (int i = 0; i < border; i++) {
	    g.draw3DRect(i, i, size().width - i * 2, size().height - i * 2,false);
	}
	render(g, fractalPath);
    
voidrender(java.awt.Graphics g, java.lang.String path)

	Stack turtleStack = new Stack();
	CLSTurtle turtle;

	if (g == null) {
	    Xmin = 1E20f;
	    Ymin = 1E20f;
	    Xmax = -1E20f;
	    Ymax = -1E20f;
	    turtle = new CLSTurtle(startAngle, 0, 0, 0, 0, 1, 1);
	} else {
	    float frwidth = Xmax - Xmin;
	    if (frwidth == 0)
		frwidth = 1;
	    float frheight = Ymax - Ymin;
	    if (frheight == 0)
		frheight = 1;
	    float xscale = (size().width - border * 2 - 1) / frwidth;
	    float yscale = (size().height - border * 2 - 1) / frheight;
	    int xoff = border;
	    int yoff = border;
	    if (normalizescaling) {
		if (xscale < yscale) {
		    yoff += ((size().height - border * 2)
			     - ((Ymax - Ymin) * xscale)) / 2;
		    yscale = xscale;
		} else if (yscale < xscale) {
		    xoff += ((size().width - border * 2)
			     - ((Xmax - Xmin) * yscale)) / 2;
		    xscale = yscale;
		}
	    }
	    turtle = new CLSTurtle(startAngle, 0 - Xmin, 0 - Ymin,
				   xoff, yoff, xscale, yscale);
	}

	for (int pos = 0; pos < path.length(); pos++) {
	    switch (path.charAt(pos)) {
	    case '+":
		turtle.rotate(rotAngle);
		break;
	    case '-":
		turtle.rotate(-rotAngle);
		break;
	    case '[":
		turtleStack.push(turtle);
		turtle = new CLSTurtle(turtle);
		break;
	    case ']":
		turtle = (CLSTurtle) turtleStack.pop();
		break;
	    case 'f":
		turtle.jump();
		break;
	    case 'F":
		if (g == null) {
		    includePt(turtle.X, turtle.Y);
		    turtle.jump();
		    includePt(turtle.X, turtle.Y);
		} else {
		    turtle.draw(g);
		}
		break;
	    default:
		break;
	    }
	}
    
public voidrun()

	Thread me = Thread.currentThread();
	boolean needsRepaint = false;
	while (kicker == me && cls.getLevel() < fractLevel) {
	    cls.generate();
	    if (kicker == me && incrementalUpdates) {
		repaint();
		try {Thread.sleep(repaintDelay);} catch (InterruptedException e){}
	    } else {
		needsRepaint = true;
	    }
	}
	if (kicker == me) {
	    kicker = null;
	    if (needsRepaint) {
		repaint();
	    }
	}
    
public voidstart()

	kicker = new Thread(this);
	kicker.start();
    
public voidstop()

	kicker = null;