BasicStrokepublic class BasicStroke extends Object implements StrokeThe BasicStroke class defines a basic set of rendering
attributes for the outlines of graphics primitives, which are rendered
with a {@link Graphics2D} object that has its Stroke attribute set to
this BasicStroke .
The rendering attributes defined by BasicStroke describe
the shape of the mark made by a pen drawn along the outline of a
{@link Shape} and the decorations applied at the ends and joins of
path segments of the Shape .
These rendering attributes include:
- width
- The pen width, measured perpendicularly to the pen trajectory.
- end caps
- The decoration applied to the ends of unclosed subpaths and
dash segments. Subpaths that start and end on the same point are
still considered unclosed if they do not have a CLOSE segment.
See {@link java.awt.geom.PathIterator#SEG_CLOSE SEG_CLOSE}
for more information on the CLOSE segment.
The three different decorations are: {@link #CAP_BUTT},
{@link #CAP_ROUND}, and {@link #CAP_SQUARE}.
- line joins
- The decoration applied at the intersection of two path segments
and at the intersection of the endpoints of a subpath that is closed
using {@link java.awt.geom.PathIterator#SEG_CLOSE SEG_CLOSE}.
The three different decorations are: {@link #JOIN_BEVEL},
{@link #JOIN_MITER}, and {@link #JOIN_ROUND}.
- miter limit
- The limit to trim a line join that has a JOIN_MITER decoration.
A line join is trimmed when the ratio of miter length to stroke
width is greater than the miterlimit value. The miter length is
the diagonal length of the miter, which is the distance between
the inside corner and the outside corner of the intersection.
The smaller the angle formed by two line segments, the longer
the miter length and the sharper the angle of intersection. The
default miterlimit value of 10.0f causes all angles less than
11 degrees to be trimmed. Trimming miters converts
the decoration of the line join to bevel.
- dash attributes
- The definition of how to make a dash pattern by alternating
between opaque and transparent sections.
All attributes that specify measurements and distances controlling
the shape of the returned outline are measured in the same
coordinate system as the original unstroked Shape
argument. When a Graphics2D object uses a
Stroke object to redefine a path during the execution
of one of its draw methods, the geometry is supplied
in its original form before the Graphics2D transform
attribute is applied. Therefore, attributes such as the pen width
are interpreted in the user space coordinate system of the
Graphics2D object and are subject to the scaling and
shearing effects of the user-space-to-device-space transform in that
particular Graphics2D .
For example, the width of a rendered shape's outline is determined
not only by the width attribute of this BasicStroke ,
but also by the transform attribute of the
Graphics2D object. Consider this code:
// sets the Graphics2D object's Tranform attribute
g2d.scale(10, 10);
// sets the Graphics2D object's Stroke attribute
g2d.setStroke(new BasicStroke(1.5f));
Assuming there are no other scaling transforms added to the
Graphics2D object, the resulting line
will be approximately 15 pixels wide.
As the example code demonstrates, a floating-point line
offers better precision, especially when large transforms are
used with a Graphics2D object.
When a line is diagonal, the exact width depends on how the
rendering pipeline chooses which pixels to fill as it traces the
theoretical widened outline. The choice of which pixels to turn
on is affected by the antialiasing attribute because the
antialiasing rendering pipeline can choose to color
partially-covered pixels.
For more information on the user space coordinate system and the
rendering process, see the Graphics2D class comments. |
Fields Summary |
---|
public static final int | JOIN_MITERJoins path segments by extending their outside edges until
they meet. | public static final int | JOIN_ROUNDJoins path segments by rounding off the corner at a radius
of half the line width. | public static final int | JOIN_BEVELJoins path segments by connecting the outer corners of their
wide outlines with a straight segment. | public static final int | CAP_BUTTEnds unclosed subpaths and dash segments with no added
decoration. | public static final int | CAP_ROUNDEnds unclosed subpaths and dash segments with a round
decoration that has a radius equal to half of the width
of the pen. | public static final int | CAP_SQUAREEnds unclosed subpaths and dash segments with a square
projection that extends beyond the end of the segment
to a distance equal to half of the line width. | float | width | int | join | int | cap | float | miterlimit | float[] | dash | float | dash_phase | private static final int[] | RasterizerCaps | private static final int[] | RasterizerCorners |
Constructors Summary |
---|
public BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase)Constructs a new BasicStroke with the specified
attributes.
if (width < 0.0f) {
throw new IllegalArgumentException("negative width");
}
if (cap != CAP_BUTT && cap != CAP_ROUND && cap != CAP_SQUARE) {
throw new IllegalArgumentException("illegal end cap value");
}
if (join == JOIN_MITER) {
if (miterlimit < 1.0f) {
throw new IllegalArgumentException("miter limit < 1");
}
} else if (join != JOIN_ROUND && join != JOIN_BEVEL) {
throw new IllegalArgumentException("illegal line join value");
}
if (dash != null) {
if (dash_phase < 0.0f) {
throw new IllegalArgumentException("negative dash phase");
}
boolean allzero = true;
for (int i = 0; i < dash.length; i++) {
float d = dash[i];
if (d > 0.0) {
allzero = false;
} else if (d < 0.0) {
throw new IllegalArgumentException("negative dash length");
}
}
if (allzero) {
throw new IllegalArgumentException("dash lengths all zero");
}
}
this.width = width;
this.cap = cap;
this.join = join;
this.miterlimit = miterlimit;
if (dash != null) {
this.dash = (float []) dash.clone();
}
this.dash_phase = dash_phase;
| public BasicStroke(float width, int cap, int join, float miterlimit)Constructs a solid BasicStroke with the specified
attributes.
this(width, cap, join, miterlimit, null, 0.0f);
| public BasicStroke(float width, int cap, int join)Constructs a solid BasicStroke with the specified
attributes. The miterlimit parameter is
unnecessary in cases where the default is allowable or the
line joins are not specified as JOIN_MITER.
this(width, cap, join, 10.0f, null, 0.0f);
| public BasicStroke(float width)Constructs a solid BasicStroke with the specified
line width and with default values for the cap and join
styles.
this(width, CAP_SQUARE, JOIN_MITER, 10.0f, null, 0.0f);
| public BasicStroke()Constructs a new BasicStroke with defaults for all
attributes.
The default attributes are a solid line of width 1.0, CAP_SQUARE,
JOIN_MITER, a miter limit of 10.0.
this(1.0f, CAP_SQUARE, JOIN_MITER, 10.0f, null, 0.0f);
|
Methods Summary |
---|
public java.awt.Shape | createStrokedShape(java.awt.Shape s)Returns a Shape whose interior defines the
stroked outline of a specified Shape .
FillAdapter filler = new FillAdapter();
PathStroker stroker = new PathStroker(filler);
PathDasher dasher = null;
try {
PathConsumer consumer;
stroker.setPenDiameter(width);
stroker.setPenT4(null);
stroker.setCaps(RasterizerCaps[cap]);
stroker.setCorners(RasterizerCorners[join], miterlimit);
if (dash != null) {
dasher = new PathDasher(stroker);
dasher.setDash(dash, dash_phase);
dasher.setDashT4(null);
consumer = dasher;
} else {
consumer = stroker;
}
feedConsumer(consumer, s.getPathIterator(null));
} finally {
stroker.dispose();
if (dasher != null) {
dasher.dispose();
}
}
return filler.getShape();
| public boolean | equals(java.lang.Object obj)Tests if a specified object is equal to this BasicStroke
by first testing if it is a BasicStroke and then comparing
its width, join, cap, miter limit, dash, and dash phase attributes with
those of this BasicStroke .
if (!(obj instanceof BasicStroke)) {
return false;
}
BasicStroke bs = (BasicStroke) obj;
if (width != bs.width) {
return false;
}
if (join != bs.join) {
return false;
}
if (cap != bs.cap) {
return false;
}
if (miterlimit != bs.miterlimit) {
return false;
}
if (dash != null) {
if (dash_phase != bs.dash_phase) {
return false;
}
if (!java.util.Arrays.equals(dash, bs.dash)) {
return false;
}
}
else if (bs.dash != null) {
return false;
}
return true;
| private void | feedConsumer(sun.dc.path.PathConsumer consumer, java.awt.geom.PathIterator pi)
try {
consumer.beginPath();
boolean pathClosed = false;
float mx = 0.0f;
float my = 0.0f;
float point[] = new float[6];
while (!pi.isDone()) {
int type = pi.currentSegment(point);
if (pathClosed == true) {
pathClosed = false;
if (type != PathIterator.SEG_MOVETO) {
// Force current point back to last moveto point
consumer.beginSubpath(mx, my);
}
}
switch (type) {
case PathIterator.SEG_MOVETO:
mx = point[0];
my = point[1];
consumer.beginSubpath(point[0], point[1]);
break;
case PathIterator.SEG_LINETO:
consumer.appendLine(point[0], point[1]);
break;
case PathIterator.SEG_QUADTO:
// Quadratic curves take two points
consumer.appendQuadratic(point[0], point[1],
point[2], point[3]);
break;
case PathIterator.SEG_CUBICTO:
// Cubic curves take three points
consumer.appendCubic(point[0], point[1],
point[2], point[3],
point[4], point[5]);
break;
case PathIterator.SEG_CLOSE:
consumer.closedSubpath();
pathClosed = true;
break;
}
pi.next();
}
consumer.endPath();
} catch (PathException e) {
throw new InternalError("Unable to Stroke shape ("+
e.getMessage()+")");
}
| public float[] | getDashArray()Returns the array representing the lengths of the dash segments.
Alternate entries in the array represent the user space lengths
of the opaque and transparent segments of the dashes.
As the pen moves along the outline of the Shape
to be stroked, the user space
distance that the pen travels is accumulated. The distance
value is used to index into the dash array.
The pen is opaque when its current cumulative distance maps
to an even element of the dash array and transparent otherwise.
if (dash == null) {
return null;
}
return (float[]) dash.clone();
| public float | getDashPhase()Returns the current dash phase.
The dash phase is a distance specified in user coordinates that
represents an offset into the dashing pattern. In other words, the dash
phase defines the point in the dashing pattern that will correspond to
the beginning of the stroke.
return dash_phase;
| public int | getEndCap()Returns the end cap style.
return cap;
| public int | getLineJoin()Returns the line join style.
return join;
| public float | getLineWidth()Returns the line width. Line width is represented in user space,
which is the default-coordinate space used by Java 2D. See the
Graphics2D class comments for more information on
the user space coordinate system.
return width;
| public float | getMiterLimit()Returns the limit of miter joins.
return miterlimit;
| public int | hashCode()Returns the hashcode for this stroke.
int hash = Float.floatToIntBits(width);
hash = hash * 31 + join;
hash = hash * 31 + cap;
hash = hash * 31 + Float.floatToIntBits(miterlimit);
if (dash != null) {
hash = hash * 31 + Float.floatToIntBits(dash_phase);
for (int i = 0; i < dash.length; i++) {
hash = hash * 31 + Float.floatToIntBits(dash[i]);
}
}
return hash;
|
|