// Recursive Figure/ import java.applet.* ; import java.awt.* ; import java.awt.event.* ; public class demo01 extends Applet implements ActionListener { double rRatio = 2 ; int nCircles = 2 ; int maxDepth = 5 ; TextField box1 = new TextField(5) ; TextField box2 = new TextField(5) ; TextField box3 = new TextField(5) ; Button ok = new Button("描画") ; void drawCircle(Graphics g,double x,double y,double r) { g.drawOval((int)(320+x-r),(int)(240-y-r),(int)(r*2),(int)(r*2)) ; } // 自己再帰型の図形描画 void drawRecFig(Graphics g, double x,double y,double r, int depth) { double th = Math.PI*2/(double)nCircles ; int k ; double x1,y1,r1 ; drawCircle(g,x,y,r) ; if( depth < maxDepth ) { for( k=0 ; k<nCircles ; k++ ) { r1 = r/rRatio ; x1 = r1*(rRatio-1)*Math.cos(th*(k-1))+x ; y1 = r1*(rRatio-1)*Math.sin(th*(k-1))+y ; // 再帰的に自分自身を呼ぶ drawRecFig(g,x1,y1,r1,depth+1) ; } } } public void init () { box1.setText( Double.toString(rRatio) ) ; box2.setText( Integer.toString(nCircles) ) ; box3.setText( Integer.toString(maxDepth) ) ; add(new Label("半径の比率:",Label.RIGHT)) ; add(box1) ; add(new Label("円の数:", Label.RIGHT)) ; add(box2) ; add(new Label("深さ:", Label.RIGHT)) ; add(box3) ; add(ok) ; ok.addActionListener(this) ; } // パラメータ値をテキストフィールドから取得して再描画 public void actionPerformed(ActionEvent e) { if( e.getSource() == ok ) { String p ; p = box1.getText() ; if( p != null ) rRatio = Double.valueOf(p).doubleValue() ; p = box2.getText() ; if( p != null ) nCircles = Integer.parseInt(p) ; p = box3.getText() ; if( p != null ) maxDepth = Integer.parseInt(p) ; repaint() ; // 再描画 } } public void paint (Graphics g) { drawRecFig(g,0.0,0.0,195.0,0) ; } }