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
12-7. Example 12-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 12-17.)
Figure 12-7. How lines are drawn in Java 2D
Example 12-9. Stroking.java
package je3.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. Shape pentagon = createRegularPolygon(5, 75); // Set up basic drawing attributes g.setColor(Color.black); ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.