import java.awt.*;
import javax.swing.JFrame;
public class myMain{ // Main App
public static void main(String[] args){
draw window = new draw( "This is a JFrame" , 500 , 500 );
window.repaint();
}
}
public class Lgen //creates the Lindenmayer system string
{
String godstring;
Lgen(String seed)
{
godstring=seed;
}
public void iterate()
{
String demigodstring=godstring;
godstring="";
for(int index=0;index<demigodstring.length();index++)
{
char F = 70;
char Plus=43;
char minus=45;
char lbrack=91;
char rbrack=93;
if (demigodstring.charAt(index)==F)
godstring+="FF-[-F+F+F]+[+F-F-F]";
else if (demigodstring.charAt(index)==Plus)
godstring+="+";
else if (demigodstring.charAt(index)==minus)
godstring+="-";
else if (demigodstring.charAt(index)==lbrack)
godstring+="[";
else if (demigodstring.charAt(index)==rbrack)
godstring+="]";
}
}
public String get_system()
{
return godstring;
}
}
public class turtle //for turtle graphics
{
public double x;
public double y;
public double angle;
turtle(double x, double y, double angle)
{
this.x=x;
this.y=y;
this.angle=angle;
}
turtle(turtle value)
{
x=value.x;
y=value.y;
angle=value.angle;
}
public void move(double dist)
{
x+=Math.cos(angle)*dist;
y+=Math.sin(angle)*dist;
}
public Graphics move(Graphics g, double dist)
{
double x2=x;
double y2=y;
x+=Math.cos(angle)*dist;
y+=Math.sin(angle)*dist;
g.drawLine((int)x, (int)y, (int)x2, (int)y2);
return g;
}
public void turn(double angle)
{
this.angle+=angle;
this.angle=wrapval(this.angle);
}
private double wrapval(double num)
{
if(num>0)
{
num=num%360;
}
else
{
num=360-num%360;
}
return num;
}
}
public class dynamic_turtle //dynamic array of "turtles"
{
private turtle[] data;
private turtle voidturtle=new turtle((double)0,(double)0,(double)0);
public dynamic_turtle()
{
data = new turtle[1];
}
public turtle get(int position)
{
if (position >= data.length)
return voidturtle;
else
return data[position];
}
public void put(int position, turtle value)
{
if (position >= data.length)
{
int newSize = 2 * data.length;
if (position >= newSize)
newSize = 2 * position;
turtle[] newData = new turtle[newSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
}
data[position] = new turtle(value.x,value.y,value.angle);
}
public void delete(int position)
{
if (position <= data.length)
{
if(position>0)
{
turtle[] newData = new turtle[data.length];
System.arraycopy(data, 0, newData, 0, position-1);
System.arraycopy(data,position+1,newData,position,data.length-position-1);
data = newData;
}
else if (position==0)
{
turtle[] newData = new turtle[data.length];
System.arraycopy(data, 1, newData, 0, data.length-1);
data=newData;
}
}
}
}
public class turtlestack //so that the array can be accessed like a stack.
{
dynamic_turtle stack;
int currentpos;
private turtle voidturtle=new turtle((double)0,(double)0,(double)0);
turtlestack()
{
stack=new dynamic_turtle();
currentpos=-1;
}
public void add_to_stack(turtle value)
{
currentpos+=1;
stack.put(currentpos, value);
}
public turtle get_from_stack()
{
System.out.println(currentpos);
if (currentpos>-1)
{
turtle value=stack.get(currentpos);
currentpos-=1;
return value;
}
else
{
return voidturtle;
}
}
}
public class drawSystem //Draws the system to the screen
{
private final double DEG_TO_RAD=Math.PI/180.0;
Graphics g;
double dist;
String system;
turtle godTurtle;
turtlestack stack;
drawSystem(Graphics g, String system, double dist, int startx, int starty)
{
this.g=g;
this.system=system;
this.dist=dist;
godTurtle=new turtle((double)startx,(double)starty,270*DEG_TO_RAD);
stack=new turtlestack();
}
public Graphics Draw()
{
int l = system.length();
for(int index=0;index<l;index++)
{
char F = 70;
char Plus=43;
char minus=45;
char lbrack=91;
char rbrack=93;
if (system.charAt(index)==F)
{
g=godTurtle.move(g,dist);
}
else if (system.charAt(index)==Plus)
{
godTurtle.turn(22.5*DEG_TO_RAD);
}
else if (system.charAt(index)==minus)
{
godTurtle.turn(-22.5*DEG_TO_RAD);
}
else if (system.charAt(index)==lbrack)
{
stack.add_to_stack(godTurtle);
}
else if (system.charAt(index)==rbrack)
{
godTurtle=stack.get_from_stack();
}
}
return g;
}
}
class draw extends JFrame //creates the screen
{ //Draw Class
private String title;
private int height;
private int width;
Image trophy;
Lgen mysys;
draw( String title , int width , int height ){ //Draw Class Constructor
this.title = title;
this.width = width;
this.height = height;
setTitle( this.title );
setSize( this.width , this.height );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
setVisible( true );
mysys=new Lgen("F");
for(int a=1;a<6;a++)
{
mysys.iterate();
}
}
public void paint( Graphics g )
{ //Paint Method, uses Graphics object
drawSystem mydraw=new drawSystem(g,mysys.get_system(),4.0,width/2,height);
System.out.println(mysys.get_system());
g=mydraw.Draw();
}
}
w00t, got a L system working! Learned all about passing Graphics to different functions.
Now onto applets!