Beispiel #1
0
  /** 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 glasses */
  public static void drawPicture1(Graphics2D g2) {

    Glass h1 = new Glass(100, 250, 50, 75);
    g2.setColor(Color.CYAN);
    g2.draw(h1);

    // Make a black house 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.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);

    // 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 houses with Windows

    FullGlass hw1 = new FullGlass(50, 350, 40, 75);
    FullGlass hw2 = new FullGlass(200, 350, 200, 100);

    g2.draw(hw1);
    g2.setColor(new Color(0x8F00FF));
    g2.draw(hw2);

    // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING

    g2.setStroke(orig);
    g2.setColor(Color.BLACK);
    g2.drawString("A few glasses by Julian Gee", 20, 20);
  }
  /** Draw a picture with a few glasses & full glasses */
  public static void drawPicture2(Graphics2D g2) {

    // Draw some coffee cups.

    Glass large = new Glass(100, 50, 225, 150);
    Glass smallCC = new Glass(20, 50, 40, 30);
    Glass tallSkinny = new Glass(20, 150, 20, 40);
    Glass shortFat = new Glass(20, 250, 40, 20);

    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);

    Glass h1 = new Glass(100, 250, 50, 75);
    g2.setColor(Color.CYAN);
    g2.draw(h1);

    // Make a black house 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.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);

    // 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 houses with Windows

    FullGlass hw1 = new FullGlass(50, 350, 40, 75);
    FullGlass hw2 = new FullGlass(200, 350, 200, 100);

    g2.draw(hw1);
    g2.setColor(new Color(0x8F00FF));

    // Rotate the second house 45 degrees around its center.
    Shape hw3 = ShapeTransforms.rotatedCopyOf(hw2, Math.PI / 4.0);

    g2.draw(hw3);

    // @@@ FINALLY, SIGN AND LABEL YOUR DRAWING

    g2.setStroke(orig);
    g2.setColor(Color.BLACK);
    g2.drawString("A bunch of glasses and full glasses by Julian Gee", 20, 20);
  }