import java.awt.*; /* * FILE NAME: CheckerBoardSquare.java * AUTHOR: Stella * DATE: Sept 16, 2007 * COMMENTS: Uses methods to draw the picture as shown in the lab3 page. * It uses the "Square approach", as noted in the lab3 page. * * MODIFICATION HISTORY: The idea is based on an earlier version * written by Lyn (?) (when?) * * */ public class CheckerBoardSquare extends BuggleWorld { public void setup () { this.setDimensions(8,8); } public void run () { CheckerBuggle barb = new CheckerBuggle(); barb.draw8x8square(Color.red, Color.blue); barb.draw8x8square(Color.green, Color.pink); barb.draw8x8square(Color.cyan, Color.magenta); } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new CheckerBoardSquare(), "CheckerBoardSquare"); } } class CheckerBuggle extends Buggle { //Draws a 8x8 square, colored c1 and c2. //Ends at the starting position public void draw8x8square(Color c1, Color c2) { System.out.println("Inside make8x8square"); this.draw4x4square(c1, c2); //square 1,1 this.forward(4); this.draw4x4square(c1, c2); //square 2,1 this.backLeftFwRight(4); this.draw4x4square(c1, c2); //square 2,1 this.forward(4); this.draw4x4square(c1, c2); //square 2,2 this.backRightFwLeft(4); //come back to the starting position } //Draws a 4x4 square, colored c1 and c2. //Ends with the buggle at the left-bottom cell, //i.e. the same position it started the square, \//and her brush Up public void draw4x4square(Color c1, Color c2) { System.out.println("Inside draw4x4square"); this.draw2x2square(c1, c2); //square 1,1 this.forward(2); this.draw2x2square(c1, c2); //square 2,1 this.backLeftFwRight(2); this.draw2x2square(c1, c2); //square 2,1 this.forward(2); this.draw2x2square(c1, c2); //square 2,2 this.backRightFwLeft(2); } //Draws a 2x2 square, colored c1 and c2. //Ends with the buggle at the left-bottom cell, //i.e. the same as the starting position, // and her brush Up. public void draw2x2square(Color c1, Color c2) { System.out.println("Inside draw2x2square"); this.brushUp(); this.paintCell(c1); this.left(); this.forward(); this.paintCell(c2); this.right(); this.forward(); this.paintCell(c1); this.right(); this.forward(); this.paintCell(c2); this.left(); this.backward(); } //Moves the buggle backward, then turns it right, then forward and then left public void backRightFwLeft(int steps) { this.backward(steps); this.right(); this.forward(steps); this.left(); } //Moves the buggle backward, then turns it left, then forward and then right public void backLeftFwRight(int steps) { this.backward(steps); this.left(); this.forward(steps); this.right(); } }