import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;


public class kadai5c extends Applet {

   Dimension d;
   Panel p;

   TextField nTextField, sTextField, aTextField, kTextField;
   Button boton1,boton2,boton3,boton4;

   double L;                      // tamanio del arbol

   double t0 = 90.0 * (Math.PI/180.0);
   double t1 = 60.0 * (Math.PI/360.0);
   double t2;    // = 20.0 * (Math.PI/180.0);  // 30
   double t3;


   double dd, ang;
   double t;
   int n;
   double dx = 0.7;  // tamanio(largo) de las ramitas
   double dy = 0.7;
   double tx = 0.95;  // tamanio(largo) de las ramitas
   double ty = 0.95;
   double px = 0.5;  // tamanio(largo) de las ramitas
   double py = 0.5;
   int angle = 30;
   double z = Math.sqrt((dx*dx+dy*dy)/2.0);
   double s = Math.sqrt((tx*tx+ty*ty)/2.0);
   double o = Math.sqrt((px*px+py*py)/2.0);
   int w, h;


 public void init (){
   w = getSize().width;
   h = getSize().height;
   L = 70.0;

   init_panel();
 }

 void init_panel(){



   String ar = getParameter ("angle");
   if (ar == null){
      t2=0*(Math.PI/180.0);
   }
   else t2 = Double.valueOf(ar).doubleValue();

   p = new Panel();
   p.add(new Label("分岐回数"));
   p.add(nTextField = new TextField("15"));
   p.add(boton1 = new Button("更新1"));

   p.add(new Label("生長する枝"));
   p.add(sTextField = new TextField("0.7"));
   p.add(boton2 = new Button("更新2"));

   p.add(new Label("枝の角度"));
   p.add(aTextField = new TextField("15"));
   p.add(boton3 = new Button("更新3"));

   p.add(new Label("分岐の大きさ(2〜7)"));
   p.add(kTextField = new TextField("5.0"));
   p.add(boton4 = new Button("更新4"));

   p.setBackground(Color.pink);
   setLayout(new BorderLayout());
   add("North",p);

   boton1.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          n= Integer.parseInt(nTextField.getText())*2;      // numero
          repaint();
        }
       });

   boton2.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          dd = Double.valueOf(sTextField.getText()).doubleValue();   // escala
          repaint();
        }
       });

   boton3.addActionListener ( new java.awt.event.ActionListener ()
      {
        public void actionPerformed (ActionEvent e)
        {
          t2 = (Double.valueOf(aTextField.getText()).doubleValue())*(Math.PI/360.0);   // angulo
          repaint();
        }
       });
   boton4.addActionListener ( new java.awt.event.ActionListener ()
         {
           public void actionPerformed (ActionEvent e)
           {
             t3 = Double.valueOf(kTextField.getText()).doubleValue();   // angulo
             if(t3 < 2.0){
				 t3 = 2.0;
			 }else if(t3 > 7.0){
				 t3 = 7.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);
   write_node(g, n, L, t0, 0, 0, 1);

 }



public void write_node (Graphics g, int n, double l, double arg, int x, int y, int flag){
	int xx, yy,i;
	xx = (int)(l * Math.cos(arg) * dd);
	yy = (int)(l * Math.sin(arg) * dd);
	if(flag == 1) g.setColor(new Color(180,180,0));
	if(flag == 2) g.setColor(new Color(0,200,97));
	if(flag == 3) g.setColor(new Color(200,200,0));
	if(flag == 4) g.setColor(new Color(0,245,120));
	g.drawLine (300+x,  500-y, 300+(x+xx) , 500-(y+yy));


	if (n>0){
		if(flag == 1){
			write_node(g, n-2, l*s, (arg + t2/2.0) + 0.0*t2/1.0, x+xx, y+yy, 1);
			write_node(g, n-2, l*0.3, (arg + t2/2.0) + t3*t2/1.0, x+xx, y+yy, 3);
			write_node(g, n-2, l*0.3, (arg + t2/2.0) - t3*t2/1.0, x+xx, y+yy, 3);
		}
		else if(flag == 2){
			write_node(g, n-2, (l*z), (arg + t2/4.0) + 0.0*t2/1.0, x+xx, y+yy, 2);
		}
		else if(flag == 3){
			write_node(g, n-2, (l*s), (arg + t2/4.0) + 0.0*t2/1.0, x+xx, y+yy, 3);
			write_node(g, n-2, (l*s), (arg + t2/2.0) + t3*t2/1.0, x+xx, y+yy, 4);
			write_node(g, n-2, (l*s), (arg + t2/2.0) - t3*t2/1.0, x+xx, y+yy, 4);
		}
		else{
			write_node(g, n-2, (l*z), (arg + t2/4.0) + 0.0*t2/1.0, x+xx, y+yy, 4);
			write_node(g, n-2, (l*0.3), (arg + t2/2.0) + t3*t2/1.0, x+xx, y+yy, 2);
 			write_node(g, n-2, (l*0.3), (arg + t2/2.0) - t3*t2/1.0, x+xx, y+yy, 2);
		}
	}
 }
}