import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Koch4 extends Applet{ int times=1; Choice timesChoice; double xOrig=5.0; double yOrig=0.0; double length; public void init(){ timesChoice=new Choice(); for(int i=1;i<=10;i++){ timesChoice.addItem(""+i); } add(timesChoice); add(new Label("times")); timesChoice.select(times-1); timesChoice.addItemListener(new ItemAdp()); } class ItemAdp implements ItemListener{ public void itemStateChanged(ItemEvent e){ if(e.getSource()==timesChoice){ times=timesChoice.getSelectedIndex()+1; repaint(); } } } public void paint(Graphics g){ double angle=0.0; xOrig=5.0; yOrig=0.0; int width=getSize().width; int height=getSize().height; int pointN=(int)Math.pow(4,times); length=(double)(width-10)/pointN; g.setColor(Color.lightGray); g.fillRect(0,0,width,height); g.setColor(Color.red); drawKoch(g,times,angle); } public void drawKoch(Graphics g,int n,double angle){ double x,y,angleR; if(n<=0){ angleR=Math.PI/180*angle; x=length*Math.cos(angleR)+xOrig; y=length*Math.sin(angleR)+yOrig; g.drawLine((int)xOrig,(int)(200-yOrig),(int)x,(int)(200-y)); xOrig=x; yOrig=y; return; } drawKoch(g,n-1,angle); drawKoch(g,n-1,angle+90); drawKoch(g,n-1,angle); drawKoch(g,n-1,angle-90); drawKoch(g,n-1,angle); } }