// The copyright of this program is reserved by “N”L. /* Turtle Graphics : Sierpinski‹Èü */ /* */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Ex061014a extends JApplet implements ActionListener{ dTurtle t; boolean flag=false; double length; double theta=0.0; int order; JComboBox cb1; String cbl1[]={"n=1","n=2","n=3","n=4","n=5","n=6","n=7","n=8"}; 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); cp.add(pl); } public void actionPerformed(ActionEvent event){ order = cb1.getSelectedIndex()+1; length = 400.0/(Math.pow(2.0,order)-1)/(1.0+Math.cos(Math.PI/4.0)); t.setColor(Color.yellow); t.setPos(10.0,10.0); t.setAng(0.0); flag=true; repaint(); } public void paint(Graphics g){ super.paint(g); if(flag==true) Sierpin(order,length,g); } void Sierpin(int n,double len,Graphics g) { if (n==0) { t.turn(-90); }else{ Sierpin(n-1,len,g); t.move(len,g); Sierpin(n-1,len,g); t.turn(135); t.move(len,g); t.turn(135); Sierpin(n-1,len,g); t.move(len,g); Sierpin(n-1,len,g); } } }