Path: blob/master/src/java.desktop/share/classes/java/awt/BasicStroke.java
41152 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.beans.ConstructorProperties;2829import java.lang.annotation.Native;3031/**32* The {@code BasicStroke} class defines a basic set of rendering33* attributes for the outlines of graphics primitives, which are rendered34* with a {@link Graphics2D} object that has its Stroke attribute set to35* this {@code BasicStroke}.36* The rendering attributes defined by {@code BasicStroke} describe37* the shape of the mark made by a pen drawn along the outline of a38* {@link Shape} and the decorations applied at the ends and joins of39* path segments of the {@code Shape}.40* These rendering attributes include:41* <dl>42* <dt><i>width</i>43* <dd>The pen width, measured perpendicularly to the pen trajectory.44* <dt><i>end caps</i>45* <dd>The decoration applied to the ends of unclosed subpaths and46* dash segments. Subpaths that start and end on the same point are47* still considered unclosed if they do not have a CLOSE segment.48* See {@link java.awt.geom.PathIterator#SEG_CLOSE SEG_CLOSE}49* for more information on the CLOSE segment.50* The three different decorations are: {@link #CAP_BUTT},51* {@link #CAP_ROUND}, and {@link #CAP_SQUARE}.52* <dt><i>line joins</i>53* <dd>The decoration applied at the intersection of two path segments54* and at the intersection of the endpoints of a subpath that is closed55* using {@link java.awt.geom.PathIterator#SEG_CLOSE SEG_CLOSE}.56* The three different decorations are: {@link #JOIN_BEVEL},57* {@link #JOIN_MITER}, and {@link #JOIN_ROUND}.58* <dt><i>miter limit</i>59* <dd>The limit to trim a line join that has a JOIN_MITER decoration.60* A line join is trimmed when the ratio of miter length to stroke61* width is greater than the miterlimit value. The miter length is62* the diagonal length of the miter, which is the distance between63* the inside corner and the outside corner of the intersection.64* The smaller the angle formed by two line segments, the longer65* the miter length and the sharper the angle of intersection. The66* default miterlimit value of 10.0f causes all angles less than67* 11 degrees to be trimmed. Trimming miters converts68* the decoration of the line join to bevel.69* <dt><i>dash attributes</i>70* <dd>The definition of how to make a dash pattern by alternating71* between opaque and transparent sections.72* </dl>73* All attributes that specify measurements and distances controlling74* the shape of the returned outline are measured in the same75* coordinate system as the original unstroked {@code Shape}76* argument. When a {@code Graphics2D} object uses a77* {@code Stroke} object to redefine a path during the execution78* of one of its {@code draw} methods, the geometry is supplied79* in its original form before the {@code Graphics2D} transform80* attribute is applied. Therefore, attributes such as the pen width81* are interpreted in the user space coordinate system of the82* {@code Graphics2D} object and are subject to the scaling and83* shearing effects of the user-space-to-device-space transform in that84* particular {@code Graphics2D}.85* For example, the width of a rendered shape's outline is determined86* not only by the width attribute of this {@code BasicStroke},87* but also by the transform attribute of the88* {@code Graphics2D} object. Consider this code:89* <blockquote><pre>{@code90* // sets the Graphics2D object's Transform attribute91* g2d.scale(10, 10);92* // sets the Graphics2D object's Stroke attribute93* g2d.setStroke(new BasicStroke(1.5f));94* }</pre></blockquote>95* Assuming there are no other scaling transforms added to the96* {@code Graphics2D} object, the resulting line97* will be approximately 15 pixels wide.98* As the example code demonstrates, a floating-point line99* offers better precision, especially when large transforms are100* used with a {@code Graphics2D} object.101* When a line is diagonal, the exact width depends on how the102* rendering pipeline chooses which pixels to fill as it traces the103* theoretical widened outline. The choice of which pixels to turn104* on is affected by the antialiasing attribute because the105* antialiasing rendering pipeline can choose to color106* partially-covered pixels.107* <p>108* For more information on the user space coordinate system and the109* rendering process, see the {@code Graphics2D} class comments.110* @see Graphics2D111* @author Jim Graham112*/113public class BasicStroke implements Stroke {114115/**116* Joins path segments by extending their outside edges until117* they meet.118*/119@Native public static final int JOIN_MITER = 0;120121/**122* Joins path segments by rounding off the corner at a radius123* of half the line width.124*/125@Native public static final int JOIN_ROUND = 1;126127/**128* Joins path segments by connecting the outer corners of their129* wide outlines with a straight segment.130*/131@Native public static final int JOIN_BEVEL = 2;132133/**134* Ends unclosed subpaths and dash segments with no added135* decoration.136*/137@Native public static final int CAP_BUTT = 0;138139/**140* Ends unclosed subpaths and dash segments with a round141* decoration that has a radius equal to half of the width142* of the pen.143*/144@Native public static final int CAP_ROUND = 1;145146/**147* Ends unclosed subpaths and dash segments with a square148* projection that extends beyond the end of the segment149* to a distance equal to half of the line width.150*/151@Native public static final int CAP_SQUARE = 2;152153float width;154155int join;156int cap;157float miterlimit;158159float[] dash;160float dash_phase;161162/**163* Constructs a new {@code BasicStroke} with the specified164* attributes.165* @param width the width of this {@code BasicStroke}. The166* width must be greater than or equal to 0.0f. If width is167* set to 0.0f, the stroke is rendered as the thinnest168* possible line for the target device and the antialias169* hint setting.170* @param cap the decoration of the ends of a {@code BasicStroke}171* @param join the decoration applied where path segments meet172* @param miterlimit the limit to trim the miter join. The miterlimit173* must be greater than or equal to 1.0f.174* @param dash the array representing the dashing pattern175* @param dash_phase the offset to start the dashing pattern176* @throws IllegalArgumentException if {@code width} is negative177* @throws IllegalArgumentException if {@code cap} is not either178* CAP_BUTT, CAP_ROUND or CAP_SQUARE179* @throws IllegalArgumentException if {@code miterlimit} is less180* than 1 and {@code join} is JOIN_MITER181* @throws IllegalArgumentException if {@code join} is not182* either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER183* @throws IllegalArgumentException if {@code dash_phase}184* is negative and {@code dash} is not {@code null}185* @throws IllegalArgumentException if the length of186* {@code dash} is zero187* @throws IllegalArgumentException if dash lengths are all zero.188*/189@ConstructorProperties({ "lineWidth", "endCap", "lineJoin", "miterLimit", "dashArray", "dashPhase" })190public BasicStroke(float width, int cap, int join, float miterlimit,191float[] dash, float dash_phase) {192if (width < 0.0f) {193throw new IllegalArgumentException("negative width");194}195if (cap != CAP_BUTT && cap != CAP_ROUND && cap != CAP_SQUARE) {196throw new IllegalArgumentException("illegal end cap value");197}198if (join == JOIN_MITER) {199if (miterlimit < 1.0f) {200throw new IllegalArgumentException("miter limit < 1");201}202} else if (join != JOIN_ROUND && join != JOIN_BEVEL) {203throw new IllegalArgumentException("illegal line join value");204}205if (dash != null) {206if (dash_phase < 0.0f) {207throw new IllegalArgumentException("negative dash phase");208}209boolean allzero = true;210for (int i = 0; i < dash.length; i++) {211float d = dash[i];212if (d > 0.0) {213allzero = false;214} else if (d < 0.0) {215throw new IllegalArgumentException("negative dash length");216}217}218if (allzero) {219throw new IllegalArgumentException("dash lengths all zero");220}221}222this.width = width;223this.cap = cap;224this.join = join;225this.miterlimit = miterlimit;226if (dash != null) {227this.dash = dash.clone();228}229this.dash_phase = dash_phase;230}231232/**233* Constructs a solid {@code BasicStroke} with the specified234* attributes.235* @param width the width of the {@code BasicStroke}236* @param cap the decoration of the ends of a {@code BasicStroke}237* @param join the decoration applied where path segments meet238* @param miterlimit the limit to trim the miter join239* @throws IllegalArgumentException if {@code width} is negative240* @throws IllegalArgumentException if {@code cap} is not either241* CAP_BUTT, CAP_ROUND or CAP_SQUARE242* @throws IllegalArgumentException if {@code miterlimit} is less243* than 1 and {@code join} is JOIN_MITER244* @throws IllegalArgumentException if {@code join} is not245* either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER246*/247public BasicStroke(float width, int cap, int join, float miterlimit) {248this(width, cap, join, miterlimit, null, 0.0f);249}250251/**252* Constructs a solid {@code BasicStroke} with the specified253* attributes. The {@code miterlimit} parameter is254* unnecessary in cases where the default is allowable or the255* line joins are not specified as JOIN_MITER.256* @param width the width of the {@code BasicStroke}257* @param cap the decoration of the ends of a {@code BasicStroke}258* @param join the decoration applied where path segments meet259* @throws IllegalArgumentException if {@code width} is negative260* @throws IllegalArgumentException if {@code cap} is not either261* CAP_BUTT, CAP_ROUND or CAP_SQUARE262* @throws IllegalArgumentException if {@code join} is not263* either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER264*/265public BasicStroke(float width, int cap, int join) {266this(width, cap, join, 10.0f, null, 0.0f);267}268269/**270* Constructs a solid {@code BasicStroke} with the specified271* line width and with default values for the cap and join272* styles.273* @param width the width of the {@code BasicStroke}274* @throws IllegalArgumentException if {@code width} is negative275*/276public BasicStroke(float width) {277this(width, CAP_SQUARE, JOIN_MITER, 10.0f, null, 0.0f);278}279280/**281* Constructs a new {@code BasicStroke} with defaults for all282* attributes.283* The default attributes are a solid line of width 1.0, CAP_SQUARE,284* JOIN_MITER, a miter limit of 10.0.285*/286public BasicStroke() {287this(1.0f, CAP_SQUARE, JOIN_MITER, 10.0f, null, 0.0f);288}289290291/**292* Returns a {@code Shape} whose interior defines the293* stroked outline of a specified {@code Shape}.294* @param s the {@code Shape} boundary be stroked295* @return the {@code Shape} of the stroked outline.296* @throws NullPointerException if {@code s} is {@code null}297*/298public Shape createStrokedShape(Shape s) {299sun.java2d.pipe.RenderingEngine re =300sun.java2d.pipe.RenderingEngine.getInstance();301return re.createStrokedShape(s, width, cap, join, miterlimit,302dash, dash_phase);303}304305/**306* Returns the line width. Line width is represented in user space,307* which is the default-coordinate space used by Java 2D. See the308* {@code Graphics2D} class comments for more information on309* the user space coordinate system.310* @return the line width of this {@code BasicStroke}.311* @see Graphics2D312*/313public float getLineWidth() {314return width;315}316317/**318* Returns the end cap style.319* @return the end cap style of this {@code BasicStroke} as one320* of the static {@code int} values that define possible end cap321* styles.322*/323public int getEndCap() {324return cap;325}326327/**328* Returns the line join style.329* @return the line join style of the {@code BasicStroke} as one330* of the static {@code int} values that define possible line331* join styles.332*/333public int getLineJoin() {334return join;335}336337/**338* Returns the limit of miter joins.339* @return the limit of miter joins of the {@code BasicStroke}.340*/341public float getMiterLimit() {342return miterlimit;343}344345/**346* Returns the array representing the lengths of the dash segments.347* Alternate entries in the array represent the user space lengths348* of the opaque and transparent segments of the dashes.349* As the pen moves along the outline of the {@code Shape}350* to be stroked, the user space351* distance that the pen travels is accumulated. The distance352* value is used to index into the dash array.353* The pen is opaque when its current cumulative distance maps354* to an even element of the dash array and transparent otherwise.355* @return the dash array.356*/357public float[] getDashArray() {358if (dash == null) {359return null;360}361362return dash.clone();363}364365/**366* Returns the current dash phase.367* The dash phase is a distance specified in user coordinates that368* represents an offset into the dashing pattern. In other words, the dash369* phase defines the point in the dashing pattern that will correspond to370* the beginning of the stroke.371* @return the dash phase as a {@code float} value.372*/373public float getDashPhase() {374return dash_phase;375}376377/**378* Returns the hashcode for this stroke.379* @return a hash code for this stroke.380*/381public int hashCode() {382int hash = Float.floatToIntBits(width);383hash = hash * 31 + join;384hash = hash * 31 + cap;385hash = hash * 31 + Float.floatToIntBits(miterlimit);386if (dash != null) {387hash = hash * 31 + Float.floatToIntBits(dash_phase);388for (int i = 0; i < dash.length; i++) {389hash = hash * 31 + Float.floatToIntBits(dash[i]);390}391}392return hash;393}394395/**396* Returns true if this BasicStroke represents the same397* stroking operation as the given argument.398*/399/**400* Tests if a specified object is equal to this {@code BasicStroke}401* by first testing if it is a {@code BasicStroke} and then comparing402* its width, join, cap, miter limit, dash, and dash phase attributes with403* those of this {@code BasicStroke}.404* @param obj the specified object to compare to this405* {@code BasicStroke}406* @return {@code true} if the width, join, cap, miter limit, dash, and407* dash phase are the same for both objects;408* {@code false} otherwise.409*/410public boolean equals(Object obj) {411if (!(obj instanceof BasicStroke)) {412return false;413}414415BasicStroke bs = (BasicStroke) obj;416if (width != bs.width) {417return false;418}419420if (join != bs.join) {421return false;422}423424if (cap != bs.cap) {425return false;426}427428if (miterlimit != bs.miterlimit) {429return false;430}431432if (dash != null) {433if (dash_phase != bs.dash_phase) {434return false;435}436437if (!java.util.Arrays.equals(dash, bs.dash)) {438return false;439}440}441else if (bs.dash != null) {442return false;443}444445return true;446}447}448449450