Java Reference
In-Depth Information
Example 11•15: CustomStrokes.java (continued)
g.setStroke(strokes[i]); // set the stroke
g.draw(shape);
// draw the shape
g.translate(140,0);
// move to the right
}
}
}
/**
* This Stroke implementation does nothing. Its createStrokedShape()
* method returns an unmodified shape. Thus, drawing a shape with
* this Stroke is the same as filling that shape!
**/
class NullStroke implements Stroke {
public Shape createStrokedShape(Shape s) { return s; }
}
/**
* This Stroke implementation applies a BasicStroke to a shape twice.
* If you draw with this Stroke, then instead of outlining the shape,
* you're outlining the outline of the shape.
**/
class DoubleStroke implements Stroke {
BasicStroke stroke1, stroke2; // the two strokes to use
public DoubleStroke(float width1, float width2) {
stroke1 = new BasicStroke(width1); // Constructor arguments specify
stroke2 = new BasicStroke(width2); // the line widths for the strokes
}
public Shape createStrokedShape(Shape s) {
// Use the first stroke to create an outline of the shape
Shape outline = stroke1.createStrokedShape(s);
// Use the second stroke to create an outline of that outline.
// It is this outline of the outline that will be filled in
return stroke2.createStrokedShape(outline);
}
}
/**
* This Stroke implementation strokes the shape using a thin line, and
* also displays the end points and Bezier curve control points of all
* the line and curve segments that make up the shape. The radius
* argument to the constructor specifies the size of the control point
* markers. Note the use of PathIterator to break the shape down into
* its segments, and of GeneralPath to build up the stroked shape.
**/
class ControlPointsStroke implements Stroke {
float radius; // how big the control point markers should be
public ControlPointsStroke(float radius) { this.radius = radius; }
public Shape createStrokedShape(Shape shape) {
// Start off by stroking the shape with a thin line. Store the
// resulting shape in a GeneralPath object so we can add to it.
GeneralPath strokedShape =
new GeneralPath(new BasicStroke(1.0f).createStrokedShape(shape));
// Use a PathIterator object to iterate through each of the line and
// curve segments of the shape. For each one, mark the endpoint and
// control points (if any) by adding a rectangle to the GeneralPath
Search WWH ::




Custom Search