/** * Fractal Waffle * a Sierpinski Gasket Java applet * by Jay Tomlin, January 1997 * http://ezinfo.ucs.indiana.edu/~ltomlin * ltomlin@indiana.edu */ import java.applet.*; import java.awt.*; public class FractalWaffle extends Applet { // Define the class variables Panel chalkboard, controls; BorderLayout border; Scrollbar iterations; Button redraw; TextField showIterations; // Define and draw widgets public void init() { chalkboard = new Panel(); // for the gasket controls = new Panel(); // to set the number of iterations iterations = new Scrollbar(Scrollbar.VERTICAL, 3, 2, 0, 6); showIterations = new TextField(2); showIterations.setEditable(false); redraw = new Button("Redraw"); controls.add(new Label("Iterations: ")); controls.add(showIterations); controls.add(iterations); controls.add(redraw); border = new BorderLayout(); setLayout(border); // for the applet itself add("Center", chalkboard); add("South", controls); show(); showIterations.setText("0"); iterations.setValue(0); } // ends init() public void start() { } // other class methods void drawGasket(int iter) { Graphics g = chalkboard.getGraphics(); Rectangle r = chalkboard.bounds(); Double J = new Double(iter); double j = J.doubleValue(); double colwidth, rowheight, rectWidth, rectHeight; try{ colwidth = r.width/(Math.pow(3.0,j));} catch (ArithmeticException e) {colwidth=0;} try{ rowheight = r.height/(Math.pow(3.0,j));} catch (ArithmeticException e) {rowheight=0;} try{ rectWidth = r.width/(Math.pow(3.0,j-1));} catch (ArithmeticException e) {rectWidth=0;} try{ rectHeight = r.height/(Math.pow(3.0,j-1));} catch (ArithmeticException e) {rectHeight=0;} Double Holes; // the number of white squares per row for this iteration try { Holes = new Double(Math.pow(3.0, j-1));} catch (ArithmeticException e) { Holes = new Double(0);} int holes = Holes.intValue(); int x,y; g.setColor(Color.white); for (y=0; y