0N/A/*
2965N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.awt;
0N/A
2965N/Aimport java.beans.ConstructorProperties;
2965N/A
0N/A/**
0N/A * The <code>BasicStroke</code> class defines a basic set of rendering
0N/A * attributes for the outlines of graphics primitives, which are rendered
0N/A * with a {@link Graphics2D} object that has its Stroke attribute set to
0N/A * this <code>BasicStroke</code>.
0N/A * The rendering attributes defined by <code>BasicStroke</code> describe
0N/A * the shape of the mark made by a pen drawn along the outline of a
0N/A * {@link Shape} and the decorations applied at the ends and joins of
0N/A * path segments of the <code>Shape</code>.
0N/A * These rendering attributes include:
0N/A * <dl compact>
0N/A * <dt><i>width</i>
0N/A * <dd>The pen width, measured perpendicularly to the pen trajectory.
0N/A * <dt><i>end caps</i>
0N/A * <dd>The decoration applied to the ends of unclosed subpaths and
0N/A * dash segments. Subpaths that start and end on the same point are
0N/A * still considered unclosed if they do not have a CLOSE segment.
0N/A * See {@link java.awt.geom.PathIterator#SEG_CLOSE SEG_CLOSE}
0N/A * for more information on the CLOSE segment.
0N/A * The three different decorations are: {@link #CAP_BUTT},
0N/A * {@link #CAP_ROUND}, and {@link #CAP_SQUARE}.
0N/A * <dt><i>line joins</i>
0N/A * <dd>The decoration applied at the intersection of two path segments
0N/A * and at the intersection of the endpoints of a subpath that is closed
0N/A * using {@link java.awt.geom.PathIterator#SEG_CLOSE SEG_CLOSE}.
0N/A * The three different decorations are: {@link #JOIN_BEVEL},
0N/A * {@link #JOIN_MITER}, and {@link #JOIN_ROUND}.
0N/A * <dt><i>miter limit</i>
0N/A * <dd>The limit to trim a line join that has a JOIN_MITER decoration.
0N/A * A line join is trimmed when the ratio of miter length to stroke
0N/A * width is greater than the miterlimit value. The miter length is
0N/A * the diagonal length of the miter, which is the distance between
0N/A * the inside corner and the outside corner of the intersection.
0N/A * The smaller the angle formed by two line segments, the longer
0N/A * the miter length and the sharper the angle of intersection. The
0N/A * default miterlimit value of 10.0f causes all angles less than
0N/A * 11 degrees to be trimmed. Trimming miters converts
0N/A * the decoration of the line join to bevel.
0N/A * <dt><i>dash attributes</i>
0N/A * <dd>The definition of how to make a dash pattern by alternating
0N/A * between opaque and transparent sections.
0N/A * </dl>
0N/A * All attributes that specify measurements and distances controlling
0N/A * the shape of the returned outline are measured in the same
0N/A * coordinate system as the original unstroked <code>Shape</code>
0N/A * argument. When a <code>Graphics2D</code> object uses a
0N/A * <code>Stroke</code> object to redefine a path during the execution
0N/A * of one of its <code>draw</code> methods, the geometry is supplied
0N/A * in its original form before the <code>Graphics2D</code> transform
0N/A * attribute is applied. Therefore, attributes such as the pen width
0N/A * are interpreted in the user space coordinate system of the
0N/A * <code>Graphics2D</code> object and are subject to the scaling and
0N/A * shearing effects of the user-space-to-device-space transform in that
0N/A * particular <code>Graphics2D</code>.
0N/A * For example, the width of a rendered shape's outline is determined
0N/A * not only by the width attribute of this <code>BasicStroke</code>,
0N/A * but also by the transform attribute of the
0N/A * <code>Graphics2D</code> object. Consider this code:
0N/A * <blockquote><tt>
0N/A * // sets the Graphics2D object's Tranform attribute
0N/A * g2d.scale(10, 10);
0N/A * // sets the Graphics2D object's Stroke attribute
0N/A * g2d.setStroke(new BasicStroke(1.5f));
0N/A * </tt></blockquote>
0N/A * Assuming there are no other scaling transforms added to the
0N/A * <code>Graphics2D</code> object, the resulting line
0N/A * will be approximately 15 pixels wide.
0N/A * As the example code demonstrates, a floating-point line
0N/A * offers better precision, especially when large transforms are
0N/A * used with a <code>Graphics2D</code> object.
0N/A * When a line is diagonal, the exact width depends on how the
0N/A * rendering pipeline chooses which pixels to fill as it traces the
0N/A * theoretical widened outline. The choice of which pixels to turn
0N/A * on is affected by the antialiasing attribute because the
0N/A * antialiasing rendering pipeline can choose to color
0N/A * partially-covered pixels.
0N/A * <p>
0N/A * For more information on the user space coordinate system and the
0N/A * rendering process, see the <code>Graphics2D</code> class comments.
0N/A * @see Graphics2D
0N/A * @author Jim Graham
0N/A */
0N/Apublic class BasicStroke implements Stroke {
0N/A
0N/A /**
0N/A * Joins path segments by extending their outside edges until
0N/A * they meet.
0N/A */
0N/A public final static int JOIN_MITER = 0;
0N/A
0N/A /**
0N/A * Joins path segments by rounding off the corner at a radius
0N/A * of half the line width.
0N/A */
0N/A public final static int JOIN_ROUND = 1;
0N/A
0N/A /**
0N/A * Joins path segments by connecting the outer corners of their
0N/A * wide outlines with a straight segment.
0N/A */
0N/A public final static int JOIN_BEVEL = 2;
0N/A
0N/A /**
0N/A * Ends unclosed subpaths and dash segments with no added
0N/A * decoration.
0N/A */
0N/A public final static int CAP_BUTT = 0;
0N/A
0N/A /**
0N/A * Ends unclosed subpaths and dash segments with a round
0N/A * decoration that has a radius equal to half of the width
0N/A * of the pen.
0N/A */
0N/A public final static int CAP_ROUND = 1;
0N/A
0N/A /**
0N/A * Ends unclosed subpaths and dash segments with a square
0N/A * projection that extends beyond the end of the segment
0N/A * to a distance equal to half of the line width.
0N/A */
0N/A public final static int CAP_SQUARE = 2;
0N/A
0N/A float width;
0N/A
0N/A int join;
0N/A int cap;
0N/A float miterlimit;
0N/A
0N/A float dash[];
0N/A float dash_phase;
0N/A
0N/A /**
0N/A * Constructs a new <code>BasicStroke</code> with the specified
0N/A * attributes.
0N/A * @param width the width of this <code>BasicStroke</code>. The
0N/A * width must be greater than or equal to 0.0f. If width is
0N/A * set to 0.0f, the stroke is rendered as the thinnest
0N/A * possible line for the target device and the antialias
0N/A * hint setting.
0N/A * @param cap the decoration of the ends of a <code>BasicStroke</code>
0N/A * @param join the decoration applied where path segments meet
0N/A * @param miterlimit the limit to trim the miter join. The miterlimit
0N/A * must be greater than or equal to 1.0f.
0N/A * @param dash the array representing the dashing pattern
0N/A * @param dash_phase the offset to start the dashing pattern
0N/A * @throws IllegalArgumentException if <code>width</code> is negative
0N/A * @throws IllegalArgumentException if <code>cap</code> is not either
0N/A * CAP_BUTT, CAP_ROUND or CAP_SQUARE
0N/A * @throws IllegalArgumentException if <code>miterlimit</code> is less
0N/A * than 1 and <code>join</code> is JOIN_MITER
0N/A * @throws IllegalArgumentException if <code>join</code> is not
0N/A * either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER
0N/A * @throws IllegalArgumentException if <code>dash_phase</code>
0N/A * is negative and <code>dash</code> is not <code>null</code>
0N/A * @throws IllegalArgumentException if the length of
0N/A * <code>dash</code> is zero
0N/A * @throws IllegalArgumentException if dash lengths are all zero.
0N/A */
2965N/A @ConstructorProperties({ "lineWidth", "endCap", "lineJoin", "miterLimit", "dashArray", "dashPhase" })
0N/A public BasicStroke(float width, int cap, int join, float miterlimit,
0N/A float dash[], float dash_phase) {
0N/A if (width < 0.0f) {
0N/A throw new IllegalArgumentException("negative width");
0N/A }
0N/A if (cap != CAP_BUTT && cap != CAP_ROUND && cap != CAP_SQUARE) {
0N/A throw new IllegalArgumentException("illegal end cap value");
0N/A }
0N/A if (join == JOIN_MITER) {
0N/A if (miterlimit < 1.0f) {
0N/A throw new IllegalArgumentException("miter limit < 1");
0N/A }
0N/A } else if (join != JOIN_ROUND && join != JOIN_BEVEL) {
0N/A throw new IllegalArgumentException("illegal line join value");
0N/A }
0N/A if (dash != null) {
0N/A if (dash_phase < 0.0f) {
0N/A throw new IllegalArgumentException("negative dash phase");
0N/A }
0N/A boolean allzero = true;
0N/A for (int i = 0; i < dash.length; i++) {
0N/A float d = dash[i];
0N/A if (d > 0.0) {
0N/A allzero = false;
0N/A } else if (d < 0.0) {
0N/A throw new IllegalArgumentException("negative dash length");
0N/A }
0N/A }
0N/A if (allzero) {
0N/A throw new IllegalArgumentException("dash lengths all zero");
0N/A }
0N/A }
0N/A this.width = width;
0N/A this.cap = cap;
0N/A this.join = join;
0N/A this.miterlimit = miterlimit;
0N/A if (dash != null) {
0N/A this.dash = (float []) dash.clone();
0N/A }
0N/A this.dash_phase = dash_phase;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a solid <code>BasicStroke</code> with the specified
0N/A * attributes.
0N/A * @param width the width of the <code>BasicStroke</code>
0N/A * @param cap the decoration of the ends of a <code>BasicStroke</code>
0N/A * @param join the decoration applied where path segments meet
0N/A * @param miterlimit the limit to trim the miter join
0N/A * @throws IllegalArgumentException if <code>width</code> is negative
0N/A * @throws IllegalArgumentException if <code>cap</code> is not either
0N/A * CAP_BUTT, CAP_ROUND or CAP_SQUARE
0N/A * @throws IllegalArgumentException if <code>miterlimit</code> is less
0N/A * than 1 and <code>join</code> is JOIN_MITER
0N/A * @throws IllegalArgumentException if <code>join</code> is not
0N/A * either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER
0N/A */
0N/A public BasicStroke(float width, int cap, int join, float miterlimit) {
0N/A this(width, cap, join, miterlimit, null, 0.0f);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a solid <code>BasicStroke</code> with the specified
0N/A * attributes. The <code>miterlimit</code> parameter is
0N/A * unnecessary in cases where the default is allowable or the
0N/A * line joins are not specified as JOIN_MITER.
0N/A * @param width the width of the <code>BasicStroke</code>
0N/A * @param cap the decoration of the ends of a <code>BasicStroke</code>
0N/A * @param join the decoration applied where path segments meet
0N/A * @throws IllegalArgumentException if <code>width</code> is negative
0N/A * @throws IllegalArgumentException if <code>cap</code> is not either
0N/A * CAP_BUTT, CAP_ROUND or CAP_SQUARE
0N/A * @throws IllegalArgumentException if <code>join</code> is not
0N/A * either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER
0N/A */
0N/A public BasicStroke(float width, int cap, int join) {
0N/A this(width, cap, join, 10.0f, null, 0.0f);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a solid <code>BasicStroke</code> with the specified
0N/A * line width and with default values for the cap and join
0N/A * styles.
0N/A * @param width the width of the <code>BasicStroke</code>
0N/A * @throws IllegalArgumentException if <code>width</code> is negative
0N/A */
0N/A public BasicStroke(float width) {
0N/A this(width, CAP_SQUARE, JOIN_MITER, 10.0f, null, 0.0f);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a new <code>BasicStroke</code> with defaults for all
0N/A * attributes.
0N/A * The default attributes are a solid line of width 1.0, CAP_SQUARE,
0N/A * JOIN_MITER, a miter limit of 10.0.
0N/A */
0N/A public BasicStroke() {
0N/A this(1.0f, CAP_SQUARE, JOIN_MITER, 10.0f, null, 0.0f);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a <code>Shape</code> whose interior defines the
0N/A * stroked outline of a specified <code>Shape</code>.
0N/A * @param s the <code>Shape</code> boundary be stroked
0N/A * @return the <code>Shape</code> of the stroked outline.
0N/A */
0N/A public Shape createStrokedShape(Shape s) {
0N/A sun.java2d.pipe.RenderingEngine re =
0N/A sun.java2d.pipe.RenderingEngine.getInstance();
0N/A return re.createStrokedShape(s, width, cap, join, miterlimit,
0N/A dash, dash_phase);
0N/A }
0N/A
0N/A /**
0N/A * Returns the line width. Line width is represented in user space,
0N/A * which is the default-coordinate space used by Java 2D. See the
0N/A * <code>Graphics2D</code> class comments for more information on
0N/A * the user space coordinate system.
0N/A * @return the line width of this <code>BasicStroke</code>.
0N/A * @see Graphics2D
0N/A */
0N/A public float getLineWidth() {
0N/A return width;
0N/A }
0N/A
0N/A /**
0N/A * Returns the end cap style.
0N/A * @return the end cap style of this <code>BasicStroke</code> as one
0N/A * of the static <code>int</code> values that define possible end cap
0N/A * styles.
0N/A */
0N/A public int getEndCap() {
0N/A return cap;
0N/A }
0N/A
0N/A /**
0N/A * Returns the line join style.
0N/A * @return the line join style of the <code>BasicStroke</code> as one
0N/A * of the static <code>int</code> values that define possible line
0N/A * join styles.
0N/A */
0N/A public int getLineJoin() {
0N/A return join;
0N/A }
0N/A
0N/A /**
0N/A * Returns the limit of miter joins.
0N/A * @return the limit of miter joins of the <code>BasicStroke</code>.
0N/A */
0N/A public float getMiterLimit() {
0N/A return miterlimit;
0N/A }
0N/A
0N/A /**
0N/A * Returns the array representing the lengths of the dash segments.
0N/A * Alternate entries in the array represent the user space lengths
0N/A * of the opaque and transparent segments of the dashes.
0N/A * As the pen moves along the outline of the <code>Shape</code>
0N/A * to be stroked, the user space
0N/A * distance that the pen travels is accumulated. The distance
0N/A * value is used to index into the dash array.
0N/A * The pen is opaque when its current cumulative distance maps
0N/A * to an even element of the dash array and transparent otherwise.
0N/A * @return the dash array.
0N/A */
0N/A public float[] getDashArray() {
0N/A if (dash == null) {
0N/A return null;
0N/A }
0N/A
0N/A return (float[]) dash.clone();
0N/A }
0N/A
0N/A /**
0N/A * Returns the current dash phase.
0N/A * The dash phase is a distance specified in user coordinates that
0N/A * represents an offset into the dashing pattern. In other words, the dash
0N/A * phase defines the point in the dashing pattern that will correspond to
0N/A * the beginning of the stroke.
0N/A * @return the dash phase as a <code>float</code> value.
0N/A */
0N/A public float getDashPhase() {
0N/A return dash_phase;
0N/A }
0N/A
0N/A /**
0N/A * Returns the hashcode for this stroke.
0N/A * @return a hash code for this stroke.
0N/A */
0N/A public int hashCode() {
0N/A int hash = Float.floatToIntBits(width);
0N/A hash = hash * 31 + join;
0N/A hash = hash * 31 + cap;
0N/A hash = hash * 31 + Float.floatToIntBits(miterlimit);
0N/A if (dash != null) {
0N/A hash = hash * 31 + Float.floatToIntBits(dash_phase);
0N/A for (int i = 0; i < dash.length; i++) {
0N/A hash = hash * 31 + Float.floatToIntBits(dash[i]);
0N/A }
0N/A }
0N/A return hash;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if this BasicStroke represents the same
0N/A * stroking operation as the given argument.
0N/A */
0N/A /**
0N/A * Tests if a specified object is equal to this <code>BasicStroke</code>
0N/A * by first testing if it is a <code>BasicStroke</code> and then comparing
0N/A * its width, join, cap, miter limit, dash, and dash phase attributes with
0N/A * those of this <code>BasicStroke</code>.
0N/A * @param obj the specified object to compare to this
0N/A * <code>BasicStroke</code>
0N/A * @return <code>true</code> if the width, join, cap, miter limit, dash, and
0N/A * dash phase are the same for both objects;
0N/A * <code>false</code> otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (!(obj instanceof BasicStroke)) {
0N/A return false;
0N/A }
0N/A
0N/A BasicStroke bs = (BasicStroke) obj;
0N/A if (width != bs.width) {
0N/A return false;
0N/A }
0N/A
0N/A if (join != bs.join) {
0N/A return false;
0N/A }
0N/A
0N/A if (cap != bs.cap) {
0N/A return false;
0N/A }
0N/A
0N/A if (miterlimit != bs.miterlimit) {
0N/A return false;
0N/A }
0N/A
0N/A if (dash != null) {
0N/A if (dash_phase != bs.dash_phase) {
0N/A return false;
0N/A }
0N/A
0N/A if (!java.util.Arrays.equals(dash, bs.dash)) {
0N/A return false;
0N/A }
0N/A }
0N/A else if (bs.dash != null) {
0N/A return false;
0N/A }
0N/A
0N/A return true;
0N/A }
0N/A}