Ryoichi Mizuno - Scientific Computer Graphics | Upper Directory |

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class branching extends Applet implements Runnable{
    Thread th;
    Image buf_i;
    Graphics buf_g;
    boolean goFlag=true,growFlag=true,lineFlag=true,plineFlag=true,fpolyFlag=true;
	
	int w,h,xp0,yp0,stack,step;
	
	int MAX=100;
	double x[]=new double[MAX];
	double y[]=new double[MAX];
	double z[]=new double[MAX];
	int t[]=new int[MAX];
	int p[]=new int[MAX];
	
	String str,str_next="";
	
	int phi=25,alpha=32,beta=25,scale=35,stepmax=15,view=0;
	
	//GUI
    Button startstop,reset,b_line,b_polyline,b_fillpoly;

	public void init(){
		//get screen size
		w=getSize().width-20;
		h=getSize().height-100;
		//create buffer layer
		buf_i=createImage(w,h);
		buf_g=buf_i.getGraphics();
		init_values();
		//GUI
		setLayout(new FlowLayout(1,20,h+30));
		add(b_line=new Button("hide"));
		add(b_polyline=new Button("hide"));
		add(b_fillpoly=new Button("hide"));
		add(startstop=new Button("stop"));
		add(reset=new Button("reset"));
		b_line.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    linectrl();
			}
		    });
		b_polyline.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    polylinectrl();
			}
		    });
		b_fillpoly.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    fillpolyctrl();
			}
		    });
		startstop.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    startstop_p();
			}
		    });
		reset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
			    reset_p();
			}
		    });
    }
    
    public void start(){
		th=new Thread(this);
		th.start();
    }
    
    public void stop(){
		goFlag=false;
    }
    
    public void run(){
		while(goFlag){
		    repaint();
		    try{Thread.sleep(750);}
		    catch(InterruptedException e){}
		}	
    }		
    
    public void update(Graphics g){
		this.paint(g);
    }

    public void paint(Graphics g){
		//initialize
		if(growFlag)step++;
		else view+=5;
		if(goFlag) draw();
		//System.out.println(str);
		g.drawImage(buf_i,10,10,this);
		if(step>=stepmax)growFlag=false;
		//start message
		g.drawString("frame",w/2-125,h+75);
		g.drawString("polygon",w/2-70,h+75);
		g.drawString("filled",w/2-5,h+70);
		g.drawString("polygon",w/2-10,h+80);
		g.drawString("start",w/2+55,h+70);
		g.drawString("/stop",w/2+55,h+80);
		g.drawString("initialize",w/2+105,h+75);
	}
	
	public void draw(){
		init_b();
		init_point();
		str_next="";
		read();
		str=str_next;
	}

	public void init_point(){
		x[0]=0; y[0]=0; z[0]=0; t[0]=0; p[0]=45;
		xp0=w/2; yp0=0;
	}
	
    public void init_values(){
		str="A(-5)";
		stack=0; step=0;
    }

    public void init_b(){
		buf_g.setColor(Color.white);
		buf_g.fillRect(0,0,w,h);
    }
    
    public void read(){
		int len,now=0,param,endp;
		String str_buf;
		char type;
		//get length of string pn
		len=str.length();
		//decode string one by one
		while(nowMath.random()){
					//p1
					if(prob>Math.random()){
						str_next+="[+("+(-alpha)+")F(1)A("+(k+1)+")]-("+(-beta)+")F(1)A("+(k+1)+")";
					}
					//p2
					else{
						str_next+="B(1)-("+(-beta)+")F(1)A("+(k+1)+")";
					}
				}
				else{
					//p1
					if(prob>Math.random()){
						str_next+="[+("+alpha+")F(1)A("+(k+1)+")]-("+beta+")F(1)A("+(k+1)+")";
					}
					//p2
					else{
						str_next+="B(1)-("+beta+")F(1)A("+(k+1)+")";
					}
				}*/
				if(0.5>Math.random()){
					if(0.5>Math.random()){
						//p1
						if(prob>Math.random()){
							str_next+="/("+phi+")[+("+(-alpha)+")F(1)A("+(k+1)+")]-("+(-beta)+")F(1)A("+(k+1)+")";
						}
						//p2
						else{
							str_next+="/("+phi+")B(1)-("+(-beta)+")F(1)A("+(k+1)+")";
						}
					}
					else{
						//p1
						if(prob>Math.random()){
							str_next+="/("+phi+")[+("+alpha+")F(1)A("+(k+1)+")]-("+beta+")F(1)A("+(k+1)+")";
						}
						//p2
						else{
							str_next+="/("+phi+")B(1)-("+beta+")F(1)A("+(k+1)+")";
						}
					}
				}
				else{
					if(0.5>Math.random()){
						//p1
						if(prob>Math.random()){
							str_next+="/("+(-phi)+")[+("+(-alpha)+")F(1)A("+(k+1)+")]-("+(-beta)+")F(1)A("+(k+1)+")";
						}
						//p2
						else{
							str_next+="/("+(-phi)+")B(1)-("+(-beta)+")F(1)A("+(k+1)+")";
						}
					}
					else{
						//p1
						if(prob>Math.random()){
							str_next+="/("+(-phi)+")[+("+alpha+")F(1)A("+(k+1)+")]-("+beta+")F(1)A("+(k+1)+")";
						}
						//p2
						else{
							str_next+="/("+(-phi)+")B(1)-("+beta+")F(1)A("+(k+1)+")";
						}
					}
				}
			}
		}
		else str_next+="A("+k+")";
	}
	
	public void bud(int s){
		//start draw circle
		xp0=(int)((double)w/2+y[0]);
		yp0=(int)((double)h-z[0]);
		//buf_g.setColor(Color.green);
		//buf_g.fillOval(xp0,yp0,s,s);
		//end draw cirle
		if(growFlag)str_next+="B("+(s+1)+")";
		else str_next+="B("+s+")";
	}
	public void startstop_p(){
		if(goFlag){
			goFlag=false;
			startstop.setLabel("start");
		}
		else{
			goFlag=true; 
			start();
			startstop.setLabel("stop");
		}
	}

	public void reset_p(){
		init_b();
		init_point();
		init_values();
		growFlag=true;
		goFlag=false;
		startstop.setLabel("start");
		startstop_p();
	}
	
	public void linectrl(){
		if(lineFlag){
			lineFlag=false;
			b_line.setLabel("show");
		}
		else{
			lineFlag=true;
			b_line.setLabel("hide");
		}
	}
	
	public void polylinectrl(){
		if(plineFlag){
			plineFlag=false;
			b_polyline.setLabel("show");
		}
		else{
			plineFlag=true;
			b_polyline.setLabel("hide");
		}
	}
	
	public void fillpolyctrl(){
		if(fpolyFlag){
			fpolyFlag=false;
			b_fillpoly.setLabel("show");
		}
		else{
			fpolyFlag=true;
			b_fillpoly.setLabel("hide");
		}
	}
}