// ---------------------------------------------------------------------- // Lyn's Divide/Conquer/Glue example from CS111 Lec 03 // Thu. Feb 2, 2012 // ---------------------------------------------------------------------- import java.awt.*; public class BoxWorld extends BuggleWorld { public void setup () { setDimensions(24, 24); //set up 16 column x 16 row grid } public void run () { BoxBuggle bob = new BoxBuggle(); bob.design1(); // bob.design2(); } //--------------------------------------------------------------------- // The following main() method allows this applet to run as an application public static void main (String[] args) { runAsApplication(new BoxWorld(), "BoxWorld"); } } // class BoxWorld class BoxBuggle extends Buggle { public void design1 () { this.nestedBoxes(0, 0, 8, Color.red, Color.blue); this.nestedBoxes(16, 16, 8, Color.blue, Color.red); this.nestedBoxes(4, 4, 16, Color.black, Color.yellow); this.nestedBoxes(12, 0, 12, Color.magenta, Color.cyan); this.nestedBoxes(0, 12, 12, Color.cyan, Color.magenta); } public void design2 () { this.nestedBoxes(0, 0, 12, Color.red, Color.blue); this.nestedBoxes(0, 12, 12, Color.cyan, Color.magenta); this.nestedBoxes(12, 12, 12, Color.blue, Color.red); this.nestedBoxes(12, 0, 12, Color.magenta, Color.cyan); this.nestedBoxes(4, 4, 16, Color.black, Color.yellow); } // Draw four nested boxes with outermost side length outerSide that // alternate between colors c1 and c2. The starting corner of // the outermost box should be at a location that is fd units forward // and lt units to the left of the buggle's current position. // The buggle's final position and heading are the same as its initial // position and heading, but its final brush state is down and final color is c2. public void nestedBoxes (int fd, int lt, int outerSide, Color c1, Color c2) { this.jump(fd,lt); this.box(outerSide, c1); this.jump(1,1); this.box(outerSide-2, c2); this.jump(1,1); this.box(outerSide-4, c1); this.jump(1,1); this.box(outerSide-6, c2); this.jump(-3-fd, -3-lt); // bring buggle back to initial position } // Assume this buggle's brush is down. Draw a box with side length n and color c. // The final state of the buggle is the same as its initial state except that // its final color is c. public void box (int n, Color c) { this.setColor(c); this.forward(n-1); this.left(); this.forward(n-1); this.left(); this.forward(n-1); this.left(); this.forward(n-1); this.left(); } // "Jump" to a position fd units to front and lt units to left. // These units may be negative. Brush is up while jumping. // Works regardless of what direction buggle is facing. // Doesn't change the color or heading of the buggle, but changes its position // and leaves the brush down. public void jump (int fd, int lt) { this.brushUp(); this.forward(fd); this.left(); this.forward(lt); this.right(); this.brushDown(); } }