import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class m0104287_kadai05 extends Applet {

   Dimension d;
   Panel p;

   TextField bTextField,  nTextField, sTextField, aTextField, lenTextField;
   Button boton;

   double L;

   double t0 = 90.0 * (Math.PI/180.0);
   double t1;    // = 20.0 * (Math.PI/180.0);  // 30


   double dd, ang;
   double len = 0.0;	//長さのランダム性補正
   double t;
   int b; 					//枝の本数
   int n;					//分岐回数
   int angle = 30;
   double z = 1.0;
   int w, h;
   double[] rand = new double[200];


 public void init (){
   w = getSize().width;
   h = getSize().height;
   L = 180.0;

   init_panel();
 }

 void init_panel(){



   String ar = getParameter ("angle");
   if (ar == null){
      t1=0*(Math.PI/180.0);
   }
   else t1 = Double.valueOf(ar).doubleValue();

   p = new Panel();
   p.add(new Label("枝の本数"));
   p.add(bTextField = new TextField("2"));

   p.add(new Label("分岐回数"));
   p.add(nTextField = new TextField("2"));

   p.add(new Label("生長する枝"));
   p.add(sTextField = new TextField("0.7"));
   p.add(new Label("長さのランダム性"));
   p.add(lenTextField = new TextField("0.0"));

   p.add(new Label("枝の角度"));
   p.add(aTextField = new TextField("20"));
   p.add(boton = new Button("Draw"));

   p.setBackground(Color.pink);
   setLayout(new BorderLayout());
   add("North",p);

   boton.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          b= Integer.parseInt(bTextField.getText());
          if(b > 200){
          	b = 200;
		}
          n= Integer.parseInt(nTextField.getText());
          dd = Double.valueOf(sTextField.getText()).doubleValue();
          len = Double.valueOf(lenTextField.getText()).doubleValue();
          if(len < 0){
		  	len = 0.0;
		  }else if(len > 1){
		  	len = 1.0;
		  }
          t1 = (Double.valueOf(aTextField.getText()).doubleValue())*(Math.PI/180.0);
          repaint();
        }
       });
 }




 public void paint (Graphics g){
   int x0, y0;
   x0= w/2;
   y0= 0;
   g.setColor(Color.white);
   g.fillRect(0,0,w,h);
   for(int i = 0; i < 200; i++){
	   rand[i] = Math.random();
   }
   write_node(g, n, L, t0, x0, 0);

 }



 public void write_node (Graphics g, int n, double l, double arg, int x, int y){

   int xx, yy;
   xx = (int)(l * Math.cos(arg)*dd);
   yy = (int)(l * Math.sin(arg)*dd);
   g.setColor(Color.blue);
   g.drawLine (x,  550-y, x+xx , 550-(y+yy));


   if (n>0){
	   for(int i = 0; i < b; i++){
		   switch(i){
			case 0:
			   write_node(g, n-1, (rand[i]*len + (1 - len))*l*z, arg - t1, x+xx, y+yy);
			   break;
			case 1:
			   write_node(g, n-1, (rand[i]*len + (1 - len))*l*z, arg + t1, x+xx, y+yy);
			   break;
			default:
			   write_node(g, n-1, (rand[i]*len + (1 - len))*l*z, arg + t1*(2*Math.random() - 1), x+xx, y+yy);
		   }
		}
   }

 }

}