/* SierpinskiApplet.java */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class SierpinskiApplet extends Applet implements ActionListener { Panel topRow = new Panel(); TextField in = new TextField("1"); Sierpinski waclaw; public void init() { setLayout(new BorderLayout()); topRow.add(new Label("Desired Order:")); topRow.add(in); in.addActionListener(this); add("North", topRow); waclaw = new Sierpinski(new Point(20,50), getBackground()); } public void paint (Graphics g) { Dimension d = this.getSize(); waclaw.paint(g, d); } public void actionPerformed (ActionEvent event) { int order = Integer.parseInt(in.getText().trim()); waclaw.setOrder(order); repaint(); } } class Sierpinski { // Draw Sierpinski curves // Author: R. H. Kamin, June 30, 1997 int order; Graphics theWindow; double scaleFactor; Point nwOfWindow; // drawing area starts here Color background; int windowSize; // minimum of height and width Sierpinski (Point p, Color c) { nwOfWindow = p; background = c; order = 1; } void setOrder (int o) { // Negative or zero order leads to infinite loop order = Math.max(o, 1); } int curvesize (int ord) { return Power.power(2, ord+2) - 2; } public void paint (Graphics g, Dimension d) { theWindow = g; windowSize = Math.min(d.width-nwOfWindow.x, d.height-nwOfWindow.y) - 10; scaleFactor = (windowSize*1.0) / curvesize(order); sierp(order, new Point(0,0)); } Point origin, lastpoint; // used to simplify drawing void drawTo (int x, int y) { Point newpt = PointOps.add(origin, x, y); drawLine(lastpoint, newpt); lastpoint = newpt; } void sierp (int ord, Point nw) { // draw Sierpinski curve of order ord, with // northwest point at nw if (ord == 1) { origin = nw; lastpoint = PointOps.add(nw, 1, 0); drawTo(2,1); drawTo(4,1); drawTo(5,0); drawTo(6,1); drawTo(5,2); drawTo(5,4); drawTo(6,5); drawTo(5,6); drawTo(4,5); drawTo(2,5); drawTo(1,6); drawTo(0,5); drawTo(1,4); drawTo(1,2); drawTo(0,1); drawTo(1,0); } else { int size = curvesize(ord-1); sierp(ord-1, nw); sierp(ord-1, PointOps.add(nw, size+2, 0)); sierp(ord-1, PointOps.add(nw, 0, size+2)); sierp(ord-1, PointOps.add(nw, size+2, size+2)); Point a = PointOps.add(nw, size-1, size), b = PointOps.add(a, 1, -1), c = PointOps.add(b, 2, 0), d = PointOps.add(c, 1, 1), e = PointOps.add(d, 0, 2), f = PointOps.add(e, -1, 1), g = PointOps.add(f, -2, 0), h = PointOps.add(g, -1, -1); undraw(a,b); undraw(c,d); undraw(e,f); undraw(g,h); drawLine(b,c); drawLine(d,e); drawLine(f,g); drawLine(h,a); } } void undraw (Point p1, Point p2) { theWindow.setColor(background); drawLine(p1, p2); theWindow.setColor(Color.black); } void drawLine (Point p1, Point p2) { Point p1s = PointOps.add( PointOps.scale(p1, scaleFactor), nwOfWindow.x, nwOfWindow.y), p2s = PointOps.add( PointOps.scale(p2, scaleFactor), nwOfWindow.x, nwOfWindow.y); theWindow.drawLine(p1s.x, p1s.y, p2s.x, p2s.y); } } class PointOps { static Point add (Point p, int dx, int dy) { return new Point(p.x+dx, p.y+dy); } static Point scale (Point p, double s) { return new Point((int)(p.x*s), (int)(p.y*s)); } } class Power { static int power (int k, int n) { // Raise k to the power n. if (n == 0) return 1; else { int t = power(k, n/2); if ((n % 2) == 0) return t*t; else return k*t*t; } } }