import java.awt.*; /* * FILE NAME: CheckerBoardL.java * AUTHOR: Stella * DATE: Sept 16, 2007 * COMMENTS: Uses methods to draw the picture as shown in the lab3 page. * It uses the "L approach", as noted in the lab3 page. * MODIFICATION HISTORY: The idea is based on an earlier version * written by Lyn (?) (when?) * * */ public class CheckerBoardL extends BuggleWorld { public void setup () { this.setDimensions(8,8); } public void run () { CheckerBuggle barb = new CheckerBuggle(); //barb.drawLshape(Color.cyan, Color.magenta); //barb.drawTwoLshapes(Color.cyan, Color.magenta); 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 CheckerBoardL(), "CheckerBoardL"); } } class CheckerBuggle extends Buggle { //Draws a 8x8 square, colored c1 and c2. //Starts and ends with the buggle at the right-bottom cell, //of the 8x 8 square. //Its initial and final heading is the same, i.e. facing east public void draw8x8square(Color c1, Color c2) { System.out.println("Inside draw8x8square"); draw4x4square(c1, c2); this.forward(4); this.draw4x4square(c1, c2); this.backLeftFwRight(4); draw4x4square(c1, c2); this.forward(4); draw4x4square(c1, c2); //move buggle to its initial position this.backRightFwLeft(4); } //Draws a 4x4 square, colored c1 and c2. //Ends with the buggle at the same position and heading //it started the square. //That position is the bottom-left cell of the 4x4 square, //and the heading is EAST. public void draw4x4square(Color c1, Color c2) { System.out.println("Inside draw4x4square"); this.drawTwoLshapes(c1, c2); this.forward(2); //get ready to start the next twoLshapes this.drawTwoLshapes(c1, c2); this.backward(2); //end the buggle in its initial position } //Draws an L shape, 2x3 squares big. The L shape itself is facing to the WEST //If the starting position is (1,1) and the initial facing is EAST, //the final position of the buggle will be 2,4 facing NORTH public void drawLshape(Color c1, Color c2) { System.out.println("Inside drawLshape"); this.brushUp(); this.paintCell(c1); this.forward(); this.left(); this.paintCell(c2); this.forward(); this.paintCell(c1); this.forward(); this.paintCell(c2); this.forward(); } //Draws two L shapes, which make a 2x4 rectangle. //Starting position is on the lower-left square of the shape, and the //initial heading is EAST. //The buggle's final position and facing will be exactly //the same as its starting position and facing. public void drawTwoLshapes(Color c1, Color c2) { System.out.println("Inside drawTwoLshapes"); this.drawLshape(c1, c2); this.left(); this.drawLshape(c1, c2); this.left(); } //Moves the buggle backward, then turns it left, //then moves it forward and then turns it right. public void backLeftFwRight(int steps) { this.backward(steps); this.left(); this.forward(steps); this.right(); } //Moves the buggle backward, then turns it right, //then moves it forward and then turns it left. public void backRightFwLeft(int steps) { this.backward(4); this.right(); this.forward(4); this.left(); } }