Home : Course Map : Chapter 6 : Java : Supplements :
Java2D: Paint and Stroke
Course Map
Chapter 6

  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Text Draw 
  Demo 10
  Demo 11

  Demo 1
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
  Demo 4
  Demo 5   Demo 6
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Topic Index
     Course Guide
     What's New

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,
    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.

  1. 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.

  2. 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.)

  3. Another shift to the right is made and then the ellipse is filled with red and then a blue border drawn.

  4. 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.


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,

    // 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

Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3

Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
22 23 24

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.