/*
 * A simple applet.  We (almost) don't handle events, we just draw.
 * This example illustrates:
 *	(Gratuitous) use of classes: subclassing, constructors
 *	Using a sub-Panel to allow running in either environment.
 */
import java.awt.*;
import java.applet.*;

public class Flake extends Applet {

    int maxdepth;
    FlakePanel fp;

    // Parameters for our drawer:
    // Base points in relative coordinates
    // Note, since we want floats, we must affix "f" to every blasted one of these.
    static final float points[][] = {  // Equilateral triangle centered at 0,0
	{-.5f, -.43302f}, {.5f, -.43302f}, {0f, .43302f}
    };

    // Our subdivision rule: each subarray contains
    // a length and an angle.  The endpoints are assumed to be {0,0} and {1,0}.
    static final float subdivision[][] = {
	{.333f,0f}, {.5f,.25f}, {.667f,0f}, {1f,0f}
    };


    // Note we don't have a Flake() { ... } constructor to initialize things;
    // instead, we wait for the init() method to be called.
    // That's what Applets want.

    public void init() {

	// Did we get a maxdepth parameter?
	try {
	    maxdepth = Integer.parseInt( getParameter( "maxdepth" ) );
	} catch(Exception e) {
	    // Guess not, or it didn't look like a number.
	    maxdepth = 6;
	}

	// Initially, we've got a blank panel; add a FlakeDrawer subpanel.
	setLayout( new BorderLayout() );
	fp = new FlakePanel(points, subdivision, maxdepth);
	add("Center", fp);

    }


    public void start() {
	repaint();
    }

    public void stop() {
    }

    // In case we were invoked as a stand-alone program, ...
    // Note this is a "static" method -- even though we're defined inside
    // an Applet-like class, there's no actual Applet in this case.
    // Being static, we don't (can't) refer to any Applet instance variables, etc.
    public static void main( String args[] ) {

	int main_maxdepth;
	FlakePanel main_fp;

	if(args.length > 0) {
	    main_maxdepth = Integer.parseInt( args[0] );
	} else {
	    main_maxdepth = 5;
	}
	Frame mywin = new SensitiveFrame("Flake");
	main_fp = new FlakePanel(points, subdivision, main_maxdepth);
	mywin.add("Center", main_fp);
	mywin.resize( 400, 500 );
	mywin.show();
    }
}

// We need to have this class just so we can catch WINDOW_DESTROY events
// reaching the frame.
class SensitiveFrame extends Frame {

    public SensitiveFrame( String s ) {
	super( s );
    }

    public SensitiveFrame() {
    }

    public boolean handleEvent( Event ev ) {
	if(ev.id == Event.WINDOW_DESTROY) {
	    System.exit(0);
	}
	return false;
    }

}
