import java.awt.*; /* * FILE NAME: LabRugWorld * AUTHOR: Stella * DATE: Sept 14 & 17, 2007 * COMMENTS: Uses methods to draw the picture as shown in the lab3 page. * MODIFICATION HISTORY: * * */ public class LabRugWorld extends BuggleWorld { public void setup () { this.setDimensions(18, 18); } public void run () { RugBuggle weaver = new RugBuggle (); //Test the four basic methods, for which code is given to you, //so you become familiar with what they do. //weaver.drawPattern1(Color.yellow); //weaver.drawPattern2(Color.red); //weaver.drawPattern3(Color.red); //weaver.drawPattern4(Color.blue); //Test those methods, one at a time //weaver.drawOneOuterSide(Color.pink); // weaver.drawOuterRing(Color.cyan); //weaver.drawInnerRing(Color.cyan); //weaver.drawCenterArea(Color.yellow); weaver.makeRug(Color.red, Color.blue, Color.yellow); } //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new LabRugWorld(), "LabRugWorld"); } } //Draws the whole rug. //The three rings of it will be colored c1, c2 and c3, //from the outside to the inside class RugBuggle extends Buggle { public void makeRug (Color c1, Color c2, Color c3) { System.out.println("Inside makeRug"); this.drawOuterRing(c1); this.fwLeft(3); this.fwRight(3); this.drawInnerRing(c2); this.fwLeft(3); this.fwRight(3); this.drawCenterArea(c3); } //Draws the center area. The color will be c. //The final position and heading of the buggle //will be the same as its initial position and heading public void drawCenterArea(Color c) { System.out.println("drawCenterArea"); //draw the bottom two "X"s this.drawPattern4(c); this.forward(3); this.drawPattern4(c); //position to start the top 2 "X"s this.brushUp(); this.left(); this.fwRight(3); this.backward(3); //draw the top two "X"s this.drawPattern4(c); this.forward(3); this.drawPattern4(c); //position the buggle to its initial position, and finish this.brushUp(); this.backward(3); this.right(); this.fwLeft(3); } //Draws the inner ring (do not confuse this with the center ring!) //The ring will have color c. //The final position and heading of the buggle //will be the same as its initial position and heading //The brush will be Up, at the end public void drawInnerRing(Color c) { //System.out.println("drawInnerRing"); this.drawPattern3(c); this.drawPattern3(c); this.drawPattern3(c); this.fwLeft(2); //makes a left turn, //and positions the buggle to start the next side of this ring this.drawPattern3(c); this.drawPattern3(c); this.drawPattern3(c); this.fwLeft(2); //makes a left turn, //and positions the buggle to start the next side of this ring this.drawPattern3(c); this.drawPattern3(c); this.drawPattern3(c); this.fwLeft(2); //makes a left turn, //and positions the buggle to start the next side of this ring this.drawPattern3(c); this.drawPattern3(c); this.drawPattern3(c); //position the buggle in its initial position this.fwLeft(2); } //Draws the outter ring. The ring will be of color c. //Buggle finishes at the same position and with the same heading //as it starts. Its brush will be up, at the end. //Notice that all four sides of the ring have the same length, //although it this is not clear in the pictures. public void drawOuterRing(Color c) { //System.out.println("drawOuterRing"); this.drawOneOuterSide(c); // Draw the bottom part (South) of the outer ring this.left(); //position to start the right part (West) of the outer ring this.drawOneOuterSide(c); // Draw the bottom part of the outer ring this.left(); //position to start the top part (North) of the outer ring this.drawOneOuterSide(c); // Draw the bottom part of the outer ring this.left(); //position to start the left part (West) of the outer ring this.drawOneOuterSide(c); // Draw the left part of the outer ring this.left(); } //Draws a line of 17 squares, colored c, //each one of them containing a baggel. //It ends 17 blocks forward from the starting position //The initial and final facing are the same. public void drawOneOuterSide(Color c){ //System.out.println("drawOneOuterSide"); this.drop5Bagels(c); this.drop5Bagels(c); this.drop5Bagels(c); this.drop2Bagels(c); } //Moves the buggle forward n steps, then it turns it left. public void fwLeft(int steps) { this.brushUp(); this.forward(steps); this.left(); } //Moves the buggle forward n steps, then it turns it right. public void fwRight(int steps) { this.brushUp(); this.forward(steps); this.right(); } //draws a line of 5 squares, colored c, each one containing a bagel //Ends 5 squares ahead of its starting position, with the same heading public void drop5Bagels(Color c) { System.out.println("drop5Bagels"); this.setColor(c); brushDown(); dropBagel(); forward(); dropBagel(); forward(); dropBagel(); forward(); dropBagel(); forward(); dropBagel(); forward(); } //draws a line of 2 squares, colored c, each one containing a bagel //Ends 2 squares ahead of its starting position, //with the same heading and its brush down public void drop2Bagels(Color c) { //draws a line of 2 bagels System.out.println("Inside drop2Bagels"); this.setColor(c); this.brushDown(); this.dropBagel(); this.forward(); this.dropBagel(); this.forward(); } //This method is not called anywhere //draws a corner 3x3 of squares, colored c, each one containing a bagel //Ends with its heading and position the same as the initial heading //and position public void drawPattern1(Color c) { //System.out.println("Inside drawPattern1"); this.setColor(c); this.drawPattern2(c); this.brushUp(); this.backward(3); this.left(); this.brushDown(); this.forward(); this.dropBagel(); this.forward(); this.dropBagel(); this.brushUp(); this.backward(2); this.right(); } //This method is not called anywhere //draws a line of 3 squares, colored c, each one containing a bagel //Ends 3 squares ahead of its starting position, //with the same heading and its brush down public void drawPattern2(Color c) { //System.out.println("Inside drawPattern2"); this.setColor(c); this.brushDown(); this.dropBagel(); this.forward(); this.dropBagel(); this.forward(); this.dropBagel(); this.forward(); } //draws pattern 3, as shown in the Lab 3 web page //Buggle finishes 3 blocks forward from its initial position, //with the same heading and its brush down public void drawPattern3(Color c) { //System.out.println("Inside drawPattern3"); this.setColor(c); brushUp(); paintCell(c); dropBagel(); forward(2); left(); forward(); paintCell(c); dropBagel(); forward(); left(); forward(2); paintCell(c); dropBagel(); left(); forward(); forward(); left(); forward(3); brushDown(); } //draws pattern 4, as shown in the Lab 3 web page: //a shape of an "X" made of 3x3 bagels //Buggle finishes in its initial position, //with the same heading as it started, and its brush up public void drawPattern4(Color c) { //System.out.println("Inside drawPattern4"); this.setColor(c); brushUp(); paintCell(c); dropBagel(); forward(2); paintCell(c); dropBagel(); left(); forward(); left(); forward(); paintCell(c); dropBagel(); forward(); right(); forward(); dropBagel(); paintCell(c); right(); forward(2); paintCell(c); dropBagel(); right(); forward(2); left(); backward(2); //System.out.println("exiting drawPattern4"); } }