Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/BasicStroke.java
38829 views
/*1* Copyright (c) 1997, 2013, 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</code> 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</code>.36* The rendering attributes defined by <code>BasicStroke</code> 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</code>.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</code>76* argument. When a <code>Graphics2D</code> object uses a77* <code>Stroke</code> object to redefine a path during the execution78* of one of its <code>draw</code> methods, the geometry is supplied79* in its original form before the <code>Graphics2D</code> transform80* attribute is applied. Therefore, attributes such as the pen width81* are interpreted in the user space coordinate system of the82* <code>Graphics2D</code> object and are subject to the scaling and83* shearing effects of the user-space-to-device-space transform in that84* particular <code>Graphics2D</code>.85* For example, the width of a rendered shape's outline is determined86* not only by the width attribute of this <code>BasicStroke</code>,87* but also by the transform attribute of the88* <code>Graphics2D</code> object. Consider this code:89* <blockquote><tt>90* // 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* </tt></blockquote>95* Assuming there are no other scaling transforms added to the96* <code>Graphics2D</code> 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</code> 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</code> 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 final static 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 final static 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 final static int JOIN_BEVEL = 2;132133/**134* Ends unclosed subpaths and dash segments with no added135* decoration.136*/137@Native public final static 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 final static 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 final static int CAP_SQUARE = 2;152153float width;154155int join;156int cap;157float miterlimit;158159float dash[];160float dash_phase;161162/**163* Constructs a new <code>BasicStroke</code> with the specified164* attributes.165* @param width the width of this <code>BasicStroke</code>. 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</code>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</code> is negative177* @throws IllegalArgumentException if <code>cap</code> is not either178* CAP_BUTT, CAP_ROUND or CAP_SQUARE179* @throws IllegalArgumentException if <code>miterlimit</code> is less180* than 1 and <code>join</code> is JOIN_MITER181* @throws IllegalArgumentException if <code>join</code> is not182* either JOIN_ROUND, JOIN_BEVEL, or JOIN_MITER183* @throws IllegalArgumentException if <code>dash_phase</code>184* is negative and <code>dash</code> is not <code>null</code>185* @throws IllegalArgumentException if the length of186* <code>dash</code> 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 = (float []) dash.clone();228}229this.dash_phase = dash_phase;230}231232/**233* Constructs a solid <code>BasicStroke</code> with the specified234* attributes.235* @param width the width of the <code>BasicStroke</code>236* @param cap the decoration of the ends of a <code>BasicStroke</code>237* @param join the decoration applied where path segments meet238* @param miterlimit the limit to trim the miter join239* @throws IllegalArgumentException if <code>width</code> is negative240* @throws IllegalArgumentException if <code>cap</code> is not either241* CAP_BUTT, CAP_ROUND or CAP_SQUARE242* @throws IllegalArgumentException if <code>miterlimit</code> is less243* than 1 and <code>join</code> is JOIN_MITER244* @throws IllegalArgumentException if <code>join</code> 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</code> with the specified253* attributes. The <code>miterlimit</code> 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</code>257* @param cap the decoration of the ends of a <code>BasicStroke</code>258* @param join the decoration applied where path segments meet259* @throws IllegalArgumentException if <code>width</code> is negative260* @throws IllegalArgumentException if <code>cap</code> is not either261* CAP_BUTT, CAP_ROUND or CAP_SQUARE262* @throws IllegalArgumentException if <code>join</code> 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</code> with the specified271* line width and with default values for the cap and join272* styles.273* @param width the width of the <code>BasicStroke</code>274* @throws IllegalArgumentException if <code>width</code> 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</code> 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</code> whose interior defines the293* stroked outline of a specified <code>Shape</code>.294* @param s the <code>Shape</code> boundary be stroked295* @return the <code>Shape</code> of the stroked outline.296*/297public Shape createStrokedShape(Shape s) {298sun.java2d.pipe.RenderingEngine re =299sun.java2d.pipe.RenderingEngine.getInstance();300return re.createStrokedShape(s, width, cap, join, miterlimit,301dash, dash_phase);302}303304/**305* Returns the line width. Line width is represented in user space,306* which is the default-coordinate space used by Java 2D. See the307* <code>Graphics2D</code> class comments for more information on308* the user space coordinate system.309* @return the line width of this <code>BasicStroke</code>.310* @see Graphics2D311*/312public float getLineWidth() {313return width;314}315316/**317* Returns the end cap style.318* @return the end cap style of this <code>BasicStroke</code> as one319* of the static <code>int</code> values that define possible end cap320* styles.321*/322public int getEndCap() {323return cap;324}325326/**327* Returns the line join style.328* @return the line join style of the <code>BasicStroke</code> as one329* of the static <code>int</code> values that define possible line330* join styles.331*/332public int getLineJoin() {333return join;334}335336/**337* Returns the limit of miter joins.338* @return the limit of miter joins of the <code>BasicStroke</code>.339*/340public float getMiterLimit() {341return miterlimit;342}343344/**345* Returns the array representing the lengths of the dash segments.346* Alternate entries in the array represent the user space lengths347* of the opaque and transparent segments of the dashes.348* As the pen moves along the outline of the <code>Shape</code>349* to be stroked, the user space350* distance that the pen travels is accumulated. The distance351* value is used to index into the dash array.352* The pen is opaque when its current cumulative distance maps353* to an even element of the dash array and transparent otherwise.354* @return the dash array.355*/356public float[] getDashArray() {357if (dash == null) {358return null;359}360361return (float[]) dash.clone();362}363364/**365* Returns the current dash phase.366* The dash phase is a distance specified in user coordinates that367* represents an offset into the dashing pattern. In other words, the dash368* phase defines the point in the dashing pattern that will correspond to369* the beginning of the stroke.370* @return the dash phase as a <code>float</code> value.371*/372public float getDashPhase() {373return dash_phase;374}375376/**377* Returns the hashcode for this stroke.378* @return a hash code for this stroke.379*/380public int hashCode() {381int hash = Float.floatToIntBits(width);382hash = hash * 31 + join;383hash = hash * 31 + cap;384hash = hash * 31 + Float.floatToIntBits(miterlimit);385if (dash != null) {386hash = hash * 31 + Float.floatToIntBits(dash_phase);387for (int i = 0; i < dash.length; i++) {388hash = hash * 31 + Float.floatToIntBits(dash[i]);389}390}391return hash;392}393394/**395* Returns true if this BasicStroke represents the same396* stroking operation as the given argument.397*/398/**399* Tests if a specified object is equal to this <code>BasicStroke</code>400* by first testing if it is a <code>BasicStroke</code> and then comparing401* its width, join, cap, miter limit, dash, and dash phase attributes with402* those of this <code>BasicStroke</code>.403* @param obj the specified object to compare to this404* <code>BasicStroke</code>405* @return <code>true</code> if the width, join, cap, miter limit, dash, and406* dash phase are the same for both objects;407* <code>false</code> otherwise.408*/409public boolean equals(Object obj) {410if (!(obj instanceof BasicStroke)) {411return false;412}413414BasicStroke bs = (BasicStroke) obj;415if (width != bs.width) {416return false;417}418419if (join != bs.join) {420return false;421}422423if (cap != bs.cap) {424return false;425}426427if (miterlimit != bs.miterlimit) {428return false;429}430431if (dash != null) {432if (dash_phase != bs.dash_phase) {433return false;434}435436if (!java.util.Arrays.equals(dash, bs.dash)) {437return false;438}439}440else if (bs.dash != null) {441return false;442}443444return true;445}446}447448449