// The copyright of this program is reserved by NL. /* Turtle Graphics : Koch Ȑ() */ /* */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ex061013a extends JApplet implements ActionListener{ dTurtle t; boolean flag=false; double length; double theta=0.0; int order; JComboBox cb1,cb2; JButton bt; String cbl1[]={"n=1","n=2","n=3","n=4","n=5","n=6","n=7","n=8"}; String cbl2[]={"=15","=30","=45","=60","=75","=80","=85","=90"}; double ang[]={15.0,30.0,45.0,60.0,75.0,80.0,85.0,90.0,120.0}; public void init(){ Container cp = getContentPane(); JPanel pl = new JPanel(); pl.setBackground(Color.black); t = new dTurtle(); pl.setLayout(null); cb1 = new JComboBox(cbl1); cb1.setBounds(0,0,60,20); cb1.setSelectedIndex(0); cb1.addActionListener(this); pl.add(cb1); cb2 = new JComboBox(cbl2); cb2.setBounds(70,0,60,20); cb2.setSelectedIndex(0); cb2.addActionListener(this); pl.add(cb2); bt = new JButton("`"); bt.setBounds(350,0,60,20); bt.addActionListener(this); pl.add(bt); cp.add(pl); } public void actionPerformed(ActionEvent event){ order = cb1.getSelectedIndex()+1; length = 400.0/(Math.pow(2.0,order)*Math.pow((1.0+Math.cos(theta/180.0*Math.PI)),order)); theta = ang[cb2.getSelectedIndex()]; t.setColor(Color.yellow); t.setPos(5.0,205.0); t.setAng(0.0); if(event.getSource()==bt){ flag=true; repaint(); } } public void paint(Graphics g){ super.paint(g); if(flag==true) koch(order,length,g); } public void koch(int order, double length,Graphics g){ if(order==0) t.move(length,g); else{ koch(order-1,length,g); t.turn(theta); koch(order-1,length,g); t.turn(-2*theta); koch(order-1,length,g); t.turn(theta); koch(order-1,length,g); } } }