jdk/test/sun/pisces/StrokeShapeTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2698 8b8a4fba727c
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2698
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2698
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2698
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     4
 *
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     8
 *
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    13
 * accompanied this code).
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    14
 *
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2698
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2698
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2698
diff changeset
    21
 * questions.
2698
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    22
 */
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    23
import java.awt.*;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    24
import java.awt.geom.Ellipse2D;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    25
import java.awt.geom.GeneralPath;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    26
import java.awt.image.BufferedImage;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    27
import java.io.File;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    28
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    29
import javax.imageio.ImageIO;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    30
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    31
/**
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    32
 * @author chrisn@google.com (Chris Nokleberg)
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    33
 * @author yamauchi@google.com (Hiroshi Yamauchi)
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    34
 */
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    35
public class StrokeShapeTest {
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    36
  public static void main(String[] args) throws Exception {
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    37
    BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    38
    Graphics2D g = image.createGraphics();
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    39
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    40
    g.setPaint(Color.WHITE);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    41
    g.fill(new Rectangle(image.getWidth(), image.getHeight()));
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    42
    g.translate(25, 100);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    43
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    44
    Stroke stroke = new BasicStroke(200, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    45
    Shape shape = new Polygon(new int[] {0, 1500, 0}, new int[] {750, 0, -750}, 3);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    46
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    47
    g.scale(.1, .1);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    48
    g.setPaint(Color.BLACK);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    49
    g.setStroke(stroke);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    50
    g.draw(shape);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    51
    g.setPaint(Color.RED);
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    52
    g.fill(stroke.createStrokedShape(shape));
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    53
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    54
    // To visually check it
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    55
    //ImageIO.write(image, "PNG", new File(args[0]));
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    56
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    57
    boolean blackPixelFound = false;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    58
    outer:
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    59
    for (int x = 0; x < 200; ++x) {
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    60
      for (int y = 0; y < 200; ++y) {
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    61
        if (image.getRGB(x, y) == Color.BLACK.getRGB()) {
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    62
          blackPixelFound = true;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    63
          break outer;
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    64
        }
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    65
      }
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    66
    }
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    67
    if (blackPixelFound) {
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    68
      throw new RuntimeException("The shape hasn't been filled in red.");
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    69
    }
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    70
  }
8b8a4fba727c 6829678: StrokeShapeTest: createStrokedShape() behaves differently
jgodinez
parents:
diff changeset
    71
}