// Subclass of Picture World for Lyn's Fall '06 Lecture 5 (Tue, Sep. 18) import java.awt.*; import java.applet.Applet; import java.util.*; public class SimplePictureWorld extends PictureWorld { public void initializePictureChoices() { // Define your pictures here and in auxiliary methods. // Add your pictures to the menu via "addPictureChoice(String name, Picture pic);" // Note: any method invocation "this.()" // can be abbreviated as "()" Picture rp = this.patch(Color.red); // red patch Picture bp = this.patch(Color.blue); // blue patch Picture gw = this.wedge(Color.green); // green wedge Picture mark = this.checkmark(Color.red, Color.blue); Picture leaves = this.twoLeaves(Color.green); this.addPictureChoice("red patch", rp); this.addPictureChoice("blue patch", bp); this.addPictureChoice("wedge", gw); this.addPictureChoice("mark", mark); this.addPictureChoice("leaves", leaves); // Below are some of the examples that we will study in class. // Note that the "this." can be omitted in all the operations; we do this // to enhance readability. /* // Tests of clockwise90, clockwise180, clockwise270, // flipHorizontally, flipVertically, and flipDiagonally. addPictureChoice("wedgeClockwise90", clockwise90(gw)); addPictureChoice("wedgeClockwise180", clockwise180(gw)); addPictureChoice("wedgeClockwise270", clockwise270(gw)); addPictureChoice("wedgeFlipHorizontally", flipHorizontally(gw)); addPictureChoice("wedgeFlipVertically", flipVertically(gw)); addPictureChoice("wedgeFlipDiagonally", flipDiagonally(gw)); addPictureChoice("markClockwise90", clockwise90(mark)); addPictureChoice("markClockwise180", clockwise180(mark)); addPictureChoice("markClockwise270", clockwise270(mark)); addPictureChoice("markFlipHorizontally", flipHorizontally(mark)); addPictureChoice("markFlipVertically", flipVertically(mark)); addPictureChoice("markFlipDiagonally", flipDiagonally(mark)); addPictureChoice("leavesClockwise90", clockwise90(leaves)); addPictureChoice("leavesClockwise180", clockwise180(leaves)); addPictureChoice("leavesClockwise270", clockwise270(leaves)); addPictureChoice("leavesFlipHorizontally", flipHorizontally(leaves)); addPictureChoice("leavesFlipVertically", flipVertically(leaves)); addPictureChoice("leavesFlipDiagonally", flipDiagonally(leaves)); */ /* // Tests of above, beside, overlay addPictureChoice("redBesideBlue", beside(rp, bp)); addPictureChoice("redAboveBlue", above(rp, bp)); addPictureChoice("redBesideBlue2", beside(rp, bp, 0.75)); addPictureChoice("redAboveBlue2", above(rp, bp, 0.75)); addPictureChoice("wedgeBesideMark", beside(gw, mark)); addPictureChoice("markBesideWedge", beside(mark, gw)); addPictureChoice("wedgeAboveMark", above(gw, mark)); addPictureChoice("markAboveWedge", above(mark, gw)); addPictureChoice("wedgeBesideRed", beside(gw, rp)); addPictureChoice("wedgeBesideRed2", beside(gw, rp, 0.25)); addPictureChoice("wedgeBesideRed3", beside(gw, rp, 0.75)); addPictureChoice("wedgeAboveRed", above(gw, rp)); addPictureChoice("wedgeAboveRed2", above(gw, rp, 0.25)); addPictureChoice("wedgeAboveRed3", above(gw, rp, 0.75)); addPictureChoice("markOverLeaves", overlay(mark, leaves)); addPictureChoice("leavesOverMark", overlay(leaves,mark)); */ // Tests of fourPics and fourSame Picture chex = fourPics(rp, bp, bp, rp); /* addPictureChoice("blueWedgeMarkRed", fourPics(bp, gw, mark, rp)); addPictureChoice("twoLeaves", beside(leaves, leaves)); addPictureChoice("fourLeaves", fourSame(leaves)); addPictureChoice("chex", chex); addPictureChoice("chex4", fourSame(chex)); addPictureChoice("chex16", fourSame(fourSame(chex))); addPictureChoice("chex64", fourSame(fourSame(fourSame(chex)))); addPictureChoice("chex256", fourSame(fourSame(fourSame(fourSame(chex))))); addPictureChoice("wedge4", fourSame(gw)); addPictureChoice("mark4", fourSame(mark)); addPictureChoice("leaves on red grid", overlay(leaves, fourSame(fourSame(fourSame(rp))))); */ /* // Tests of rotations addPictureChoice("wedgeRotations", rotations(gw)); addPictureChoice("markRotations", rotations(mark)); addPictureChoice("chexRotations", rotations(chex)); addPictureChoice("leavesRotations", rotations(leaves)); // Tests of rotations2 addPictureChoice("wedgeRotations2", rotations2(gw)); addPictureChoice("markRotations2", rotations2(mark)); addPictureChoice("chexRotations2", rotations2(chex)); addPictureChoice("leavesRotations2", rotations2(leaves)); */ /* // Tests of tiling addPictureChoice("wedgeTiling", tiling(gw)); addPictureChoice("markTiling", tiling(mark)); addPictureChoice("chexTiling", tiling(chex)); addPictureChoice("leavesTiling", tiling(leaves)); // Tests of wallpaper addPictureChoice("wedgeWallpaper", wallpaper(gw)); addPictureChoice("markWallpaper", wallpaper(mark)); addPictureChoice("markClockwise90Wallpaper", wallpaper(clockwise90(mark))); addPictureChoice("chexWallpaper", wallpaper(chex)); addPictureChoice("leavesWallpaper", wallpaper(leaves)); // Tests of design addPictureChoice("wedgeDesign", design(gw)); addPictureChoice("markDesign", design(mark)); addPictureChoice("markClockwise90Design", design(clockwise90(mark))); addPictureChoice("chexDesign", design(chex)); addPictureChoice("leavesDesign", design(leaves)); */ } // end of initializePictureChoices // Some helpful auxiliary methods that we will study in class today: 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 rotations2 (Picture p) { return fourPics(p, clockwise90(p), clockwise180(p), clockwise270(p)); } public Picture tiling (Picture p) { return fourSame(fourSame(fourSame(fourSame(p)))); } public Picture wallpaper (Picture p) { return rotations(rotations(rotations(rotations(p)))); } public Picture design (Picture p) { return rotations2(rotations2(rotations2(rotations2(p)))); } // Below are methods for generating the pictures used above. 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)); } public Picture wedge (Color c) { Poly w = new Poly(c, true); w.addPoint(0.0, 0.0); w.addPoint(1.0, 0.0); w.addPoint(1.0, 0.5); return w; } public Picture checkmark (Color c1, Color c2) { return overlay (new Line(0.0, 0.5, 0.5, 0.0, c1), new Line(0.5, 0.0, 1.0, 1.0, c2)); } public Picture tri (Color c) { Poly t = new Poly(c, true); t.addPoint(0.0, 0.0); t.addPoint(0.0, 1.0); t.addPoint(1.0, 0.0); return t; } public Picture twoLeaves (Color c) { Line branch = new Line(0.0, 0.0, 0.5, 0.5, c); Poly leaf1 = new Poly(c, true); // Add points in counterclockwise fashion. // Two consecutive points will be connected by a line. leaf1.addPoint(0.0, 0.0); leaf1.addPoint(0.5, 0.0); leaf1.addPoint(1.0, 0.25); leaf1.addPoint(0.5, 0.25); Poly leaf2 = new Poly(c, true); // Add points in counterclockwise fashion. // Two consecutive points will be connected by a line. leaf2.addPoint(0.25, 0.25); leaf2.addPoint(0.5, 0.625); leaf2.addPoint(0.5, 1.0); leaf2.addPoint(0.25, 0.625); // Challenge: instead of defining leaf1 and leaf2 from scratch, // it is possible to define them both in terms of a single leaf picture. // Can you figure out how? return overlay(leaf1, overlay(leaf2, branch)); } }