/* File: LFractal.java Author: Stefan Robila Description: The name LFractal was chosen to show the origin of my idea: Lyndemeyer Systems (or L Systems) that are used to describe this class of fractals. Although the information pertaining to them is not scarce at all and I saw even java implementations of them I have to say that the entire code from this file is original and does not copy other authors. An important thing in this code is the CharList class (for more information, see the file CharList.java). Structure of LFractal class: public class LFractal{ Data: ---- private String name; - the name of the fractal private int angle; - the angle of the fractal private float initialangle; - initial angle (useful to determine the initial direction) private float size; - size of the segment to be drawn private float standard_size;- a copy of above value that will not be changed while the methods are called private int xinitial; - initial (x,y) position of the turtle private int yinitial; private char start; - the replace character from the string private CharList pattern; - the string viewed as a string of characters private String initial; - the inital string private String replace; - the string that, at every iteration replaces the character from the string private float ratio; - the ratio to which the size is reduced after every iteration Methods: ------- public LFractal (String fname, int fangle, float fratio, char fstart, String finitial, String freplace, int fxinitial, int fyinitial, int fsize); - pretyy much, just places something in every data member public void expand(int depth); - expands the pattern by replacing every occurence of the start character with the replace String. This is done depth- times and in fact is not done on characters but on the CharList object public void DrawPattern(Graphics g); - draws the fractal according to the fractal public void clear(); - resets the initial characteristics of the fractal - The following functions just return one of the members: public String getName(); - returns the name public int getAngle(); - returns the angle public String getInitial(); - returns the initial string public char getStart(); - returns the replaced character public String getReplace(); - returns the replacing string } */ // The code: // -------- import java.awt.Graphics; import java.awt.Polygon; public class LFractal{ private String name; private int angle; private float initialangle; private float size; private float standard_size; private int xinitial; private int yinitial; private char start; private CharList pattern; private String initial; private String replace; private float ratio; // this constructor just places values in all the members public LFractal (String fname, int fangle, float fratio, char fstart, String finitial, String freplace, int fxinitial, int fyinitial, int fsize) { name = fname; angle= fangle; ratio = fratio; start = fstart; // initialize the CharList pattern pattern = new CharList(start); initial = finitial; // put the initial string as a CharList pattern.replaceList(start,initial); replace = freplace; // transform the angle from degrees in radians initialangle=(float)Math.PI/2; xinitial=fxinitial; yinitial=fyinitial; standard_size=fsize; size = fsize; }; // the function expand() refines the pattern by replacing every occurence // of the start character with the replace String. This is done depth- // times and in fact is not done on characters but on the CharList object // in fact the real refination is done in the CharList class public void expand(int depth){ for(int i=1;i<=depth;i++){ // call the CharList replaceList() method to do the expansion of the //list of characters pattern.replaceList(start,replace); size=size*ratio; } }; // again, the only thing that I am doing is to send to the CharList class // all the information necessary to print the fractal. The pattern of the // fractal is a CHarList object public void DrawPattern(Graphics g){ pattern.printList(g,initialangle,angle,xinitial,yinitial,size); }; // clear() resets to the original data public void clear() { pattern = new CharList(start); pattern.replaceList(start,initial); size =standard_size; }; // The next five functions just return some data members public String getName() { return name; }; public int getAngle() { return angle; }; public String getInitial() { return initial; }; public char getStart() { return start; }; public String getReplace() {return replace; }; }