Paint refers to the color, pattern or texture that
is used to render a stroke or filling of a shape. For example, in
this code snippet, the paint is first set to a solid color and then
to a gradient that varies from yellow at point (x1,y1)
to green at point (x2,y2):
public
void paintComponent (Graphics g) {
Graphics2D g2 = (Graphics2d) g;
g2.setPaint (Color.RED);
...draw operation ...
Gradient gradient = new Gradient (x1, y1,
Color.YELLOW,
x2, y2, Color.Green,
true);
g2.setPaint (gradient)
... draw operation ...
}
The final boolean parameter to the Gradient
constructor determines whether the gradient repeats (cycles) along
the path away from each point or remains constant beyond those points.
Stroke refers to the characteristics of a line, e.g.
its thickness, color, continuous or dashed, etc.Prior to drawing
a graphic primitive shape such as a line or rectangle, you can define
the stroke with the setStroke()
method.
A class that implements the Stroke
interface describes the features of the outline. For most drawing
requirements you can use the available BasicStroke
class which defines such features as the width of a line, whether
it is solid or dashed, whether the ends are flat or rounded, and
the appearance of the "joins" between two lines such as the corner
of a rectangle.
Rectangle2D
rect = new Rectangle2D.Double (25., 50., 100., 200.);
g2.setPaint (Color.RED);
Stroke stroke =
new BasicStroke (5.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND);
g2.setStroke (stroke);
g2.draw (rect);
This code creates an instance of Rectangle2D
(see Shapes and Areas) and sets the paint
to a solid color. The BasicStroke
constructor sets the width of the line to 5.0. The next two parameters
specify that the end of a line is rounded and that the corners where
two lines meet are rounded. The stroke is set into the Graphics2D
and finally the rectangle is drawn.
See the BasicStroke
class specifications for a listing of the many options for these
and other stroke features. The class includes several constructors
for creating different types of strokes.
Demo of Paint and Stroke
The applet demo here illustrates stroke and paint techniques. It
shows four different displays of an ellipse shape.
- In the first case on the left we draw the ellipse border after
setting the paint to the color black and using setStroke(new
BasicStroke(10)) to set the line width to 10 pixels.
- Then we move the frame of the ellipse to the right (this is
an alternative to the translation transform
used on the previous page.) and set the paint to red.
This time we set the stroke to dashes using
float dash[] = {10.0f, 5.0f};
g2.setStroke (new BasicStroke (8.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER,
10.0f, dash, 0.0f))
The BasicStroke
constructor uses the dash array to determine the dash pattern.
(The CAP_BUTT,
JOIN_MITER tell the stroke how to draw the line
end and corners but these aren't relevant for the ellipse.)
- Another shift to the right is made and then the ellipse is filled
with red and then a blue border drawn.
- Finally, after another right shift, the same red fill and blue
border are drawn but with the anti-aliasing hint set. The edges
are clearly smoother than without the anti-aliasing.
The anti-aliasing operations take longer to perform. So for some
applications, e.g. animations, where speed is important, one may
want not to employ them.
StrokePaintApplet.java
Ellipse2D
drawn with:
- 10 pixel wide stroke and black paint
- red dashed, 8 pixel wide stroke and red paint
- draw with 10 pixel stroke and blue paint, then fill
with red.
- same but use anti-aliasing rendering hints.
|
import
javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** Demonstration of stroke and painting in Java2D. **/
public class StrokePaintApplet extends JApplet
{
public void init () {
Container content_pane = getContentPane
();
// Create an instance of DrawingPanel
StrokePaintPanel stroke_paint_panel
=
new StrokePaintPanel
();
// Add the DrawingPanel to the contentPane.
content_pane.add (stroke_paint_panel);
} // init
} // class StrokePaintApplet |
class
StrokePaintPanel extends JPanel
{
public void paintComponent (Graphics g) {
// First paint background
super.paintComponent (g);
// The context that is passed
is actually
// the Graphics2D sub-class of
Graphics. So
// we can cast it to Graphics2D
and take advantage
// of the additional tools that
it has.
Graphics2D g2 = (Graphics2D)g;
// Java2D shapes can take double
type parameters
double x=20.0,y=30.0,w=72.0,h=72.0;
// Create an ellipse shape object
Ellipse2D shape = new Ellipse2D.Double
(x,y,w,h);
// Stroke with a solid color.
g2.setPaint (Color.black);
// Use a Basic Stroke of width
10
g2.setStroke (new BasicStroke
(10));
// Now draw the shape
g2.draw (shape);
// Move the shape 80 pixels to
the right while keeping
// vertical position and width
and height of its bounding
// frame unchanged.
shape.setFrame (x + 80.0, y, w,
h);
// Stroke with a solid color.
g2.setPaint (Color.red);
// Use a dashed stroke of 8 pixels
width and
// alternating 10 and 5 pixel
long dashes.
float dash[] = {10.0f, 5.0f};
g2.setStroke (new BasicStroke
(8.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f));
g2.draw (shape);
// Shift right by another 80 pixels.
shape.setFrame (x + 160.0, y,
w, h);
// Change the paint and fill the
shape.
g2.setPaint (Color.red);
g2.fill (shape);
// Use a Basic Stroke of width
8
g2.setStroke (new BasicStroke
(8));
g2.setPaint (Color.blue);
g2.draw (shape);
// Options on the rendering, i.e.
the actual pixel settings
// of the drawing, can be chosen
via RenderingHints
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Shift right by another 80 pixels.
shape.setFrame (x + 240.0, y,
w, h);
// Fill the shape.
g2.setPaint (Color.red);
g2.fill (shape);
// Change color and draw the shape,
the
// stroke is unchanged.
g2.setPaint (Color.blue);
g2.draw (shape);
} // paintComponent
} // class StrokePaintPanel
|
Latest update: Oct. 27, 2004
|