// // IMPORTANT NOTE: // This code is not meant to serve as an example of good coding style. // In fact, it is an ugly hack of some very old code written for DOS // and ported to Java. // import java.applet.Applet; import java.awt.*; // for Graphics, Color, Image, Point, ... import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.lang.Math; public class SnowflakeCurve extends Applet implements MouseListener { int width, height; Color fgcolor = Color.black, bgcolor = Color.white; int maxGeneration = 6; int generation = 1; double x, y; // current plotting position double s; // segment length Graphics global_g; static final double s0 = 0.8; public void init() { width = getSize().width; height = getSize().height; s = s0 * width; try { maxGeneration = Integer.parseInt( getParameter("max_generation") ); } catch (Exception E) { } try { fgcolor = new Color(Integer.parseInt( getParameter("fgcolor"), 16 )); } catch (Exception E) { } try { bgcolor = new Color(Integer.parseInt( getParameter("bgcolor"), 16 )); } catch (Exception E) { } setBackground( bgcolor ); setForeground( fgcolor ); addMouseListener( this ); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { ++ generation; if ( generation > maxGeneration ) { generation = 1; s = s0 * width; } else s /= 3; repaint(); e.consume(); } public void mousePressed( MouseEvent e ) { } public void mouseReleased( MouseEvent e ) { } void plot( double x_new, double y_new ) { global_g.drawLine( (int)Math.round( x ), (int)Math.round( y ), (int)Math.round( x_new ), (int)Math.round( y_new ) ); x = x_new; y = y_new; } void A( int generation ) { if ( generation > 1 ) { A( generation-1 ); B2( generation-1 ); C2( generation-1 ); A( generation-1 ); } else plot(x+(s*Math.cos(60*Math.PI/180)),y+(s*Math.sin(60*Math.PI/180))); } void B( int generation ) { if (generation > 1) { B( generation-1 ); C2( generation-1 ); A2( generation-1 ); B( generation-1 ); } else plot(x+(s*Math.cos(180*Math.PI/180)),y+(s*Math.sin(180*Math.PI/180))); } void C( int generation ) { if (generation > 1) { C( generation-1 ); A2( generation-1 ); B2( generation-1 ); C( generation-1 ); } else plot(x+(s*Math.cos(300*Math.PI/180)),y+(s*Math.sin(300*Math.PI/180))); } void A2( int generation ) { if (generation > 1) { A2( generation-1 ); B( generation-1 ); C( generation-1 ); A2( generation-1 ); } else plot(x+(s*Math.cos(240*Math.PI/180)),y+(s*Math.sin(240*Math.PI/180))); } void B2( int generation ) { if (generation > 1) { B2( generation-1 ); C( generation-1 ); A( generation-1 ); B2( generation-1 ); } else plot(x+(s*Math.cos(0*Math.PI/180)),y+(s*Math.sin(0*Math.PI/180))); } void C2( int generation ) { if (generation > 1) { C2( generation-1 ); A( generation-1 ); B( generation-1 ); C2( generation-1 ); } else plot(x+(s*Math.cos(120*Math.PI/180)),y+(s*Math.sin(120*Math.PI/180))); } public void paint( Graphics g ) { global_g = g; // this is a hack to avoid having to pass g around x = width / 2; y = (height - (s0*width) * Math.sin(60*Math.PI/180) * 4/3) / 2; A( generation ); B( generation ); C( generation ); } public String getAppletInfo() { return "Written by Michael McGuffin"; } }