import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class treeParametro4 extends Applet { Dimension d; Panel p; TextField nTextField, sTextField, aTextField; Button boton1,boton2,boton3; 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 dd, ang; double t; int n; double dx = 0.7; // tamanio(largo) de las ramitas double dy = 0.7; int angle = 30; double z = Math.sqrt((dx*dx+dy*dy)/2.0); int w, h; public void init (){ w = getSize().width; h = getSize().height; L = 180.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("2")); p.add(boton1 = new Button("Change 1")); p.add(new Label("生長する枝")); p.add(sTextField = new TextField("0.7")); p.add(boton2 = new Button("Change 2")); p.add(new Label("枝の角度")); p.add(aTextField = new TextField("20")); p.add(boton3 = new Button("Change 3")); 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()); // 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(); } }); } 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); } public void write_node (Graphics g, int n, double l, double arg, int x, int y){ int xx, yy,i; xx = (int)(l * Math.cos(arg) * dd); yy = (int)(l * Math.sin(arg) * dd); g.setColor(Color.blue); g.drawLine (300+x, 500-y, 300+(x+xx) , 500-(y+yy)); if (n>0){ write_node(g, n-1, l*z, (arg + t2/2.0) + 0.0*t2/1.0, x+xx, y+yy); write_node(g, n-1, l*z, (arg + t2/2.0) + 1.0*t2/1.0, x+xx, y+yy); } } }