/* FILE NAME: LabQuiltWorld.java * AUTHOR: Stella * DATE: Sept 21 2007 * COMMENTS: Uses methods to draw the picture as shown in the lab4 page. * MODIFICATION HISTORY: */ import java.awt.*; import java.applet.Applet; import java.util.*; public class LabQuiltWorld extends PictureWorld { //------------------------------------------------------------------------------ // The following main method is needed to run the applet as an application public static void main (String[] args) { runAsApplication(new LabQuiltWorld(), "LabQuiltWorld"); } //------------------------------------------------------------------------------ public void initializePictureChoices() { // Define your pictures here and in auxiliary methods. // Add your pictures to the menu via "addPictureChoice(String name, Picture pic);" // Some helpful color abbreviations: Color blue = Color.blue; Color red = Color.red; Color green = Color.green; Color yellow = Color.yellow; //addPictureChoice("0: patch(red)", patch(red)); //addPictureChoice("0:triangles(red,blue)", triangles(red,blue)); addPictureChoice("Task 2c 1: diamonds(red,green, blue)", diamonds(red,green, blue)); addPictureChoice("Task 2c 1:-- diamondQuadrant(red,green,blue)", diamondQuadrant(red,green,blue)); addPictureChoice("Task 2c 1:-- oneDiamond(green,blue)", oneDiamond(green,blue)); //addPictureChoice("Task 2c 1:-- oneDiamond(red,blue)", diamond(red,blue)); addPictureChoice("Task 2c 2: pattern1(yellow, blue)", pattern1(yellow, blue)); addPictureChoice("Task 2c 2: pattern2(green, blue, yellow)", pattern2(green, blue, yellow)); addPictureChoice("Task 2c 2: fourColors(yellow,green,blue,red)", fourColors(yellow, green, blue, red)); addPictureChoice("Task 2c 2:-- fourColorQuadrant(yellow,green, blue,red)", fourColorQuadrant(yellow,green,blue,red)); //addPictureChoice("Task 2c 2:---- pattern1(yellow,blue)", pattern1(yellow,blue)); //addPictureChoice("Task 2c 2:---- pattern2(yellow,blue,red)", pattern2(yellow, blue, red)); } //__________________________________________________________ // solving the diamonds picture //produces a 2x2 square of color c2, //with a diamond of color c1 in the middle. public Picture oneDiamond (Color c1, Color c2) { Picture bottom_left = triangles(c2, c1); Picture top_left = clockwise90(bottom_left); Picture top_right = triangles(c1, c2);; Picture bottom_right = clockwise90(top_right); Picture p = fourPics(top_left, top_right, bottom_left, bottom_right); return p; } // produce a square 4x4 basic little squares, // i.e. one quadrant of the "diamonds" pattern, in Task 2c // c1 -- is the color of the one diamond in the bottom-right corner // c2 is the color of the other 3 diamonds // c3 -- is the color of the bg public Picture diamondQuadrant (Color c1, Color c2, Color c3) { //the top-left, top-right and bottom left 2x2 squares: Picture p1 = oneDiamond(c2, c3); Picture p2 = oneDiamond(c1, c3); Picture pic = fourPics(p1, p1, p1, p2); return pic; } //diamonds(): produces the whole pattern of diamonds // c1 -- is the color of the 4 diamonds in the center // c2 is the color of the other diamonds around // c3 -- is the color of the bg public Picture diamonds (Color c1, Color c2, Color c3) { // Delete line below and flesh out this skeleton. Picture top_Left = diamondQuadrant(c1, c2, c3); Picture top_Right = flipHorizontally(top_Left); Picture bottom_Left = flipVertically(top_Left); Picture bottom_Right = flipVertically(top_Right); Picture pattern = fourPics(top_Left, top_Right, bottom_Left, bottom_Right); return pattern; } //____________________________________________________________ // solving the fourColors picture // Produce the 2x2 square to the right of the top-left green one, // in picture "fourColors" // c1 -- is the bg color // c2 -- is the wedge on the right public Picture pattern1 (Color c1, Color c2) { Picture bottom_right = triangles(c1, c2); Picture top_right = clockwise90(bottom_right); Picture top_left = patch(c1); //Notice this is the same as bottom_left Picture p = fourPics(top_left, top_right, top_left, bottom_right); return p; } // Produces the pattern in the 2x2 square diagonally off // the corner solid 2x2 square. // c1 -- the biggest monochromatic area, above the diagonal // c2 -- the wedge that looks to the left (west) // c3 -- the wedge that looks to the bottom (south) public Picture pattern2 (Color c1, Color c2, Color c3) { Picture top_right = patch(c1); Picture top_left = triangles(c2, c1); Picture bottom_left = clockwise90(triangles(c2, c3)); Picture bottom_right = triangles(c3, c1); Picture pic = fourPics(top_left, top_right, bottom_left, bottom_right); return pic; } //Produce a 4x4 square, i.e the quadrant of the "fourColors" picture. //Look at the top-left in "fourColors" picture, in the web page: // c1 -- the mostly used color in the area (yellow) // c2 -- the color of the top-right 2x2 square (green) // c3 -- the rest color in the top-right square, // and some in the bottom-right one (blue). // c4 -- the least used color in the quadrant (red). public Picture fourColorQuadrant (Color c1, Color c2, Color c3, Color c4) { Picture top_left = patch(c2); Picture top_right = pattern1(c1, c3); Picture bottom_left = clockwise90(pattern1(c1, c4)); Picture bottom_right = flipHorizontally(pattern2(c1, c3, c4)); Picture pic = fourPics(top_left, top_right, bottom_left, bottom_right); return pic; } //Produce the whole pattern, for lab 3, task 2c, "fourColors". //In parenthesis the color on the top-left quadrant of "fourColors" picture //is shown. // c1 -- the most colors in the squares, (yellow) // c2 -- the color of the top-right 2x2 square (green). // c3 -- the rest of the right squares (blue) // c4 -- the rest in the bottom squares (red) public Picture fourColors (Color c1, Color c2, Color c3, Color c4) { Picture top_left = fourColorQuadrant(c1, c2, c3, c4); Picture top_right = flipHorizontally(fourColorQuadrant(c2, c4, c3, c1)); Picture bottom_left = flipVertically(fourColorQuadrant(c3, c1, c2, c4)); Picture bottom_right = clockwise180(fourColorQuadrant(c4, c3, c2, c1)); Picture pic = fourPics(top_left, top_right, bottom_left, bottom_right); return pic; } // Auxiliary methods public Picture fourPics (Picture p1, Picture p2, Picture p3, Picture p4) { return above(beside(p1, p2), beside(p3, p4)); } public Picture fourSame (Picture p) { return fourPics(p, p, p, p); } public Picture rotations (Picture p) { return fourPics(clockwise270(p), p, clockwise180(p), clockwise90(p)); } public Picture corner (Picture p1, Picture p2) { return fourPics(p2, p2, p1, p2); } // Methods for generating the primitive pictures for this problem. // You do not have to understand these in order to do the problem set. public Picture patch(Color c) { return overlay (new Rect(0.0, 0.0, 1.0, 1.0, Color.black, false), new Rect(0.0, 0.0, 1.0, 1.0, c, true)); } // added by Elena Feb. 20 2002 public Picture triangle(Color c) { return tri(c, true); } public Picture triangles(Color c1, Color c2) { return overlay (new Rect(0.0, 0.0, 1.0, 1.0, Color.black, false), overlay (new Line(0.0, 1.0, 1.0, 0.0, Color.black), overlay(tri(c1, true), clockwise180(tri(c2, true))))); } public Picture tri(Color c, boolean isFilled) { Poly triPoly = new Poly (c, isFilled); triPoly.addPoint(0.0, 0.0); triPoly.addPoint(0.0, 1.0); triPoly.addPoint(1.0, 0.0); return triPoly; } }