Java Reference
In-Depth Information
Figur e 11•12. Special ef fects with custom Stroke classes
You should pay particular attention to the ControlPointsStroke and SloppyStroke
implementations. These classes are interesting because they use a PathIterator
object to break a shape down into its component line and curve segments (just the
opposite of what was done in the Spiral class shown in Example 11-14). These
two custom Stroke classes also use the GeneralPath class of java.awt.geom to
build a custom shape out of arbitrary line and curve segments (which shows how
closely linked the GeneralPath class and the PathIterator interface are).
Example 11•15: CustomStrokes.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.font.*;
/** A demonstration of writing custom Stroke classes */
public class CustomStrokes implements GraphicsExample {
static final int WIDTH = 750, HEIGHT = 200; // Size of our example
public String getName() {return "Custom Strokes";} // From GraphicsExample
public int getWidth() { return WIDTH; }
// From GraphicsExample
public int getHeight() { return HEIGHT; }
// From GraphicsExample
// These are the various stroke objects we'll demonstrate
Stroke[] strokes = new Stroke[] {
new BasicStroke(4.0f), // The standard, predefined stroke
new NullStroke(), // A Stroke that does nothing
new DoubleStroke(8.0f, 2.0f), // A Stroke that strokes twice
new ControlPointsStroke(2.0f), // Shows the vertices & control points
new SloppyStroke(2.0f, 3.0f)
// Perturbs the shape before stroking
};
/** Draw the example */
public void draw(Graphics2D g, Component c) {
// Get a shape to work with. Here we'll use the letter B
Font f = new Font("Serif", Font.BOLD, 200);
GlyphVector gv = f.createGlyphVector(g.getFontRenderContext(), "B");
Shape shape = gv.getOutline();
// Set drawing attributes and starting position
g.setColor(Color.black);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.translate(10, 175);
// Draw the shape once with each stroke
for(int i = 0; i < strokes.length; i++) {
Search WWH ::




Custom Search