/** Constructor for objects of class CoffeeCup */ public CoffeeCup(double x, double y, double width, double height) { // Specify the upper left corner, and the // width and height of the original points used to // plot the *hard-coded* coffee cup final double ORIG_ULX = 100.0; final double ORIG_ULY = 100.0; final double ORIG_HEIGHT = 300.0; final double ORIG_WIDTH = 400.0; GeneralPath leftSide = new GeneralPath(); // left side of cup leftSide.moveTo(200, 400); leftSide.lineTo(160, 360); leftSide.lineTo(130, 300); leftSide.lineTo(100, 200); leftSide.lineTo(100, 100); GeneralPath topAndBottom = new GeneralPath(); topAndBottom.moveTo(100, 100); topAndBottom.lineTo(500, 100); // top of cup topAndBottom.moveTo(200, 400); topAndBottom.lineTo(400, 400); // bottom of cup Shape rightSide = ShapeTransforms.horizontallyFlippedCopyOf(leftSide); // after flipping around the upper left hand corner of the // bounding box, we move this over to the right by 400 pixels rightSide = ShapeTransforms.translatedCopyOf(rightSide, 400.0, 0.0); // now we put the whole thing together ino a single path. GeneralPath wholeCup = new GeneralPath(); wholeCup.append(topAndBottom, false); wholeCup.append(leftSide, false); wholeCup.append(rightSide, false); // translate to the origin by subtracting the original upper left x and y // then translate to (x,y) by adding x and y Shape s = ShapeTransforms.translatedCopyOf(wholeCup, -ORIG_ULX + x, -ORIG_ULY + y); // scale to correct height and width s = ShapeTransforms.scaledCopyOf(s, width / ORIG_WIDTH, height / ORIG_HEIGHT); // Use the GeneralPath constructor that takes a shape and returns // it as a general path to set our instance variable cup this.set(new GeneralPath(s)); }
/** Draw a picture with a few houses */ public static void drawPicture1(Graphics2D g2) { Pizza p1 = new Pizza(100, 250, 50, 75); g2.setColor(Color.CYAN); g2.draw(p1); // Make a black house that's half the size, // and moved over 150 pixels in x direction Shape h2 = ShapeTransforms.scaledCopyOfLL(p1, 0.5, 0.5); h2 = ShapeTransforms.translatedCopyOf(h2, 150, 0); g2.setColor(Color.BLACK); g2.draw(h2); // Here's a house that's 4x as big (2x the original) // and moved over 150 more pixels to right. h2 = ShapeTransforms.scaledCopyOfLL(h2, 4, 4); h2 = ShapeTransforms.translatedCopyOf(h2, 150, 0); // We'll draw this with a thicker stroke Stroke thick = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); Stroke orig = g2.getStroke(); g2.setStroke(thick); g2.setColor(new Color(0x002FA7)); g2.draw(h2); // Draw two pizzas with toppings PizzaWithToppings pt1 = new PizzaWithToppings(50, 350, 40, 75); PizzaWithToppings pt2 = new PizzaWithToppings(200, 350, 200, 300); g2.draw(pt1); g2.setColor(new Color(0x8F00FF)); g2.draw(pt2); // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING g2.setStroke(orig); g2.setColor(Color.BLACK); g2.drawString("A few slices of Pizza by John Lau", 20, 20); }
/** Draw a different picture with some Birthday Cakes */ public static void drawPicture3(Graphics2D g2) { Cake large = new BirthdayCake(200, 123, 421, 123); Cake small = new BirthdayCake(50, 50, 50, 50); Cake medium = new BirthdayCake(100, 75, 100, 75); g2.setColor(Color.RED); Shape different3 = ShapeTransforms.scaledCopyOfLL(large, 0.5, 0.5); g2.draw(different3); Shape different = ShapeTransforms.translatedCopyOf(medium, 0, 150); g2.setColor(Color.GREEN); g2.draw(different); g2.setColor(Color.BLUE); Shape different2 = ShapeTransforms.rotatedCopyOf(medium, Math.PI / 4.0); g2.draw(different2); g2.setColor(Color.BLACK); g2.drawString("A Mix of Birthday Cakes by George Lieu", 20, 20); }
/** Draw a picture with some cakes */ public static void drawPicture2(Graphics2D g2) { Cake large = new Cake(50, 50, 200, 200); Cake small = new Cake(75, 75, 100, 100); Cake medium = new Cake(100, 100, 150, 150); g2.setColor(Color.CYAN); Shape different3 = ShapeTransforms.scaledCopyOfLL(large, .75, .75); g2.draw(different3); Shape different = ShapeTransforms.translatedCopyOf(medium, 100, 33); g2.setColor(Color.BLACK); g2.draw(different); g2.setColor(Color.GREEN); Stroke thick = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); g2.setStroke(thick); Shape different2 = ShapeTransforms.rotatedCopyOf(medium, Math.PI); g2.draw(different2); g2.setColor(Color.BLUE); g2.drawString("A Mix of Cakes by George Lieu", 20, 100); }
/** Draw a picture with two cakes and a Birthday cake */ public static void drawPicture1(Graphics2D g2) { Cake large = new Cake(300, 300, 300, 300); Cake small = new BirthdayCake(100, 100, 100, 100); Cake medium = new Cake(200, 200, 200, 200); g2.setColor(Color.RED); Shape different3 = ShapeTransforms.scaledCopyOfLL(large, 0.5, 0.5); g2.draw(different3); Shape different = ShapeTransforms.translatedCopyOf(medium, 100, 25); g2.setColor(Color.ORANGE); g2.draw(different); g2.setColor(Color.BLUE); Stroke thick = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); g2.setStroke(thick); Shape different2 = ShapeTransforms.rotatedCopyOf(small, Math.PI / 2.0); g2.draw(different2); g2.setColor(Color.YELLOW); g2.drawString("A Mix of Cakes by George Lieu", 20, 20); }
/** Draw a picture with a few houses and coffee cups */ public static void drawPicture2(Graphics2D g2) { // Draw some pizza PizzaWithToppings large = new PizzaWithToppings(100, 50, 225, 150); Pizza smallCC = new Pizza(20, 50, 40, 30); Pizza tallSkinny = new Pizza(20, 150, 20, 40); Pizza shortFat = new Pizza(20, 250, 40, 70); g2.setColor(Color.RED); g2.draw(large); g2.setColor(Color.GREEN); g2.draw(smallCC); g2.setColor(Color.BLUE); g2.draw(tallSkinny); g2.setColor(Color.MAGENTA); g2.draw(shortFat); Pizza h1 = new Pizza(100, 250, 50, 75); g2.setColor(Color.CYAN); g2.draw(h1); // Make a red pizza that's half the size, // and moved over 150 pixels in x direction Shape h2 = ShapeTransforms.scaledCopyOfLL(h1, 0.5, 0.5); h2 = ShapeTransforms.translatedCopyOf(h2, 150, 0); g2.setColor(Color.RED); g2.draw(h2); // Here's a house that's 4x as big (2x the original) // and moved over 150 more pixels to right. h2 = ShapeTransforms.scaledCopyOfLL(h2, 4, 4); h2 = ShapeTransforms.translatedCopyOf(h2, 150, 0); // We'll draw this with a thicker stroke Stroke thick = new BasicStroke(4.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL); // for hex colors, see (e.g.) http://en.wikipedia.org/wiki/List_of_colors // #002FA7 is "International Klein Blue" according to Wikipedia // In HTML we use #, but in Java (and C/C++) its 0x Stroke orig = g2.getStroke(); g2.setStroke(thick); g2.setColor(new Color(0x002FA7)); g2.draw(h2); // Draw two pizzas with toppings PizzaWithToppings pt1 = new PizzaWithToppings(50, 350, 100, 100); PizzaWithToppings pt2 = new PizzaWithToppings(200, 350, 200, 100); g2.draw(pt1); g2.setColor(new Color(0x8F00FE)); // Rotate the second house 45 degrees around its center. Shape pt3 = ShapeTransforms.rotatedCopyOf(pt2, Math.PI / 4.0); g2.draw(pt3); // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING g2.setStroke(orig); g2.setColor(Color.BLACK); g2.drawString("A bunch of Pizza slices and a few with toppings on them by John Lau", 20, 20); }