Java Reference
In-Depth Information
Example 11•8: LineStyles.java (continued)
// Highlight the location of the vertexes of the shape
// This accentuates the cap and join styles we're demonstrating
for(int j = 0; j < xpoints.length; j++)
g.fillRect(xpoints[j]-2, ypoints[j]-2, 5, 5);
g.drawString(capNames[i], 5, 105); // Label the cap style
g.drawString(joinNames[i], 5, 120); // Label the join style
g.translate(150, 0); // Move over to the right before looping again
}
}
}
Stroking Lines
The BasicStroke class is an implementation of the Stroke interface. This interface
is responsible for defining how lines are drawn, or stroked, in Java 2D. Filling
arbitrary shapes is the fundamental graphics operation defined by Java 2D. The
Stroke interface defines the API by which line drawing operations are transformed
into area-filling operations, as illustrated in Figure 11-7. Example 11-9 shows the
code used to produce this figure. (Once you understand how the Stroke interface
converts a shape to a stroked shape suitable for filling, you can define interesting
custom Stroke implementations, as we'll do later in this chapter, in Example
11-15).
Figure 11•7. How lines are drawn in Java 2D
Example 11•9: Stroking.java
package com.davidflanagan.examples.graphics;
import java.awt.*;
import java.awt.geom.*;
/** A demonstration of how Stroke objects work */
public class Stroking implements GraphicsExample {
static final int WIDTH = 725, HEIGHT = 250; // Size of our example
public String getName() {return "Stroking";} // From GraphicsExample
public int getWidth() { return WIDTH; }
// From GraphicsExample
public int getHeight() { return HEIGHT; }
// From GraphicsExample
/** Draw the example */
public void draw(Graphics2D g, Component c) {
// Create the shape we'll work with. See convenience method below.
Search WWH ::




Custom Search