import java.awt.*; import java.util.Enumeration; import jds.Deque; import jds.collection.LinkedList; import jds.collection.IndexedDeque; import jds.util.CloseQuit; class Line { Line (Point s, Point e) { start = s; stop = e; } public Point start, stop; public void draw (Graphics g) { g.drawLine(start.x, start.y, stop.x, stop.y); } } class Snowflake extends Frame { public static void main (String [ ] args) { Snowflake world = new Snowflake(); world.show(); world.run(); } public Snowflake () { setTitle("Fractal Snowflake"); setSize(400, 400); addWindowListener(new CloseQuit()); } public void run () { // insert first lines into snowflake Point a = new Point(50, 140); Point b = new Point(350, 140); Point c = new Point(200, 360); lines.addFirst(new Line(a, b)); lines.addFirst(new Line(b, c)); lines.addFirst(new Line(c, a)); // now make the snowflake while (! lines.isEmpty()) { Line lne = (Line) lines.getFirst(); lines.removeFirst(); processLine (lne); try { Thread.sleep(100); } catch(Exception e) { } repaint(); } } private void processLine (Line lne) { // first compute line lengths int dx = (lne.stop.x - lne.start.x)/3; int dy = (lne.stop.y - lne.start.y)/3; if ((dx * dx + dy * dy) < 10) { done.addFirst(lne); // line is too small } else { Point a = new Point (lne.start.x + dx, lne.start.y+dy); Point b = new Point (lne.start.x + 3*dx/2 + dy, lne.start.y + 3*dy/2 - dx); Point c = new Point (lne.start.x+2*dx, lne.start.y+2*dy); lines.addFirst(new Line(lne.start, a)); lines.addFirst(new Line(a, b)); lines.addFirst(new Line(b, c)); lines.addFirst(new Line(c, lne.stop)); } } public void paint (Graphics g) { Enumeration e = lines.elements(); while (e.hasMoreElements()) { Line lne = (Line) e.nextElement(); lne.draw(g); } e = done.elements(); while (e.hasMoreElements()) { Line lne = (Line) e.nextElement(); lne.draw(g); } } private Deque lines = new IndexedDeque(); private Deque done = new IndexedDeque(); }