//---------------- class variables ------------------------------ //---- recursive generation parameters public static double sizeRatio = 0.5; // integer represent % public static double offset = 0.5; // offset/100 = parametric value // child positioning offset public static double p2projection = 0.5; // parametric value of projection of // vertex p2 onto the base; can be < 0 public static boolean outside = true; public static boolean fillTriangles = true; public static int maxDepth = 6; public static int minHeight = 7; // min height of p2 for further // subdivision. private static Color[] colors = { Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.YELLOW, Color.BLACK }; //---------------- instance variables ------------------------------ public static ATriangle _tri; private static ATriangle[] children; private int _depth; private FractalTriangle _parent = null; private static FractalTriangle[] childTri; //---- triangle shape parameters private double _p2height; //------------------------ constructors ---------------------------- /** * initial constructor. Needs an ATriangle object. It also gets * passed the height of this triangle and the projection position of the * 3rd point onto the opposite line. These could be computed from the * ATriangle object, but it's pretty complicated to do and in the context * of this assignment, the caller got those parameters from the user, * so it's easiest to just pass them along. */ public FractalTriangle( int depth, ATriangle tri, int height ) { // set up values of instance variables _depth = 1; _depth = depth; _p2height = height; _parent = null; children = new ATriangle[3]; childTri = new FractalTriangle[3]; //////////////////////////////////////////////// // Set the triangle's color to a value that depends // on its depth. // // Generate the children triangles for this root. // Make sure your code for this is as clean and // modular as possible. ///////////////////////////////////////////////////// _parent = this; _tri = tri; if ( _depth < maxDepth || _p2height >= minHeight ) { childTri[ 0 ] = new FractalTriangle( tri, this, depth ); childTri[ 1 ] = new FractalTriangle( tri, this, depth ); childTri[ 2 ] = new FractalTriangle( tri, this, depth ); } } /** * recursive constructor; it's private, so can only be called from methods * of this class. It will get called by the public FractalTriangle. */ private FractalTriangle ( ATriangle tri, FractalTriangle parent, int depth ) { /////////////////////////////////////////////////////////// // get parent's height from parent parameter to compute this height // // save other parameters in instance variables // // recursively create children -- only if recursion depth limit hasn't // been reached and the minimum height test passes /////////////////////////////////////////////////////////// System.err.println( "depth before if: " + depth ); _tri = tri; _depth = depth; _parent = this; _p2height = _tri.getHeight( ); if ( _depth < maxDepth || _p2height >= minHeight) { System.err.println( "depth after if: " + depth ); //_parent = this; Point[] pts = tri.getPoints(); children[0] = makeTriangle( pts[0], pts[1] ); children[1] = makeTriangle( pts[1], pts[2] ); children[2] = makeTriangle( pts[2], pts[0] ); System.err.println( "child1: " + children[0] ); for( int i = 0; i < children.length; i++ ) { childTri[ i ] = new FractalTriangle( children[ i ], _parent, depth ); } } else { return; } } //--------------------- display( Graphics2D ) ----------------------- /** * method called by Java's repaint mechanism. */ public void display ( Graphics2D context ) { ////////////////////////////////////////////////////// // 1. Need to determine whether to draw the ATriangle's // display method (which fills it) or its "draw" method // which only does the outline. /////////////////////////////////////////////////////// _tri.draw( context ); //draws outline on base triangle if( fillTriangles == true ) { _tri.display( context ); // fills base triangle for( int i = 0; i < children.length; i++ ) { if( children == null ) { System.err.println( "Children is null" ); return; } children[i].display( context ); // fills children } } for( int i = 0; i < children.length; i++ ) { if( children == null ) { System.err.println( "Children is null" ); return; } else { children[i].draw( context ); //draws outline of children } } ////////////////////////////////////////////////////// // 2. Need to recursively invoke the display methods of // all children (if they exist) // ////////////////////////////////////////////////////// }