import java.lang.*; import java.awt.*; import java.applet.*; public class SnowFlake extends Applet { private int side = 400; private int order = 6; public void init() { order = Integer.valueOf(getParameter("order")).intValue(); if(order < 1) order = 1; if(order > 6) order = 6; resize(side, side); } private void snow(Graphics g, int n) { double x1=0.0, y1=0, x2=0.5, y2=Math.sqrt(3.0)/2, x3=1, y3=0; sline(g, x1, y1, x2, y2, n); sline(g, x2, y2, x3, y3, n); /* curve has three sections */ sline(g, x3, y3, x1, y1, n); /* 1st order curve is a triangle */ } private void sline(Graphics g, double x1, double y1, double x5, double y5, int n) { if(n > 1) /* break line ((x1,y1).(x5,y5)) into 4 segments */ { double x2, y2, x3, y3, x4, y4; x2=(2*x1+ x5)/3; y2=(2*y1+ y5)/3; x4=( x1+2*x5)/3; y4=( y1+2*y5)/3; double sixty = Math.PI / 3; x3=x2+(x2-x1)*Math.cos(sixty)-(y2-y1)*Math.sin(sixty); y3=y2+(x2-x1)*Math.sin(sixty)+(y2-y1)*Math.cos(sixty); sline(g, x1,y1, x2,y2, n-1); sline(g, x2,y2, x3,y3, n-1); /* the kink out */ sline(g, x3,y3, x4,y4, n-1); sline(g, x4,y4, x5,y5, n-1); }else /* n == 0 */ { int s2 = Math.round(5*side/6); double root3by2 = Math.sqrt(3.0)/2; g.drawLine( 5+(int)Math.round(x1*s2), 5+(int)Math.round((root3by2-y1)*s2), 5+(int)Math.round(x5*s2), 5+(int)Math.round((root3by2-y5)*s2) ); } } public void paint(Graphics g) // redraw the applet { g.drawRect(0, 0, side-1, side-1); g.drawString("L.Allison, Computer Science, Monash University. order=" + Integer.toString(order), 1, side-2); snow(g, order); } public boolean mouseUp(Event e, int x, int y) // mouse click { if( x > side/2 ) order += 1; else order -= 1; if( order > 9 ) order = 9; if( order < 1 ) order = 1; repaint(); return true; } } /* L.Allison, Department of Computer Science, Monash University */