Most of the time, when you want to change the lines drawn by a Line, Turtle or framed shape, it is enough just to adjust the line width using their setLineWidth() method. However, you may find yourself someday wanting to draw a wide line or arc with square ends rather than rounded ones, or you may want the corners of a FramedRect to still be clean and rectangular, rather than rounded. In that case, you will want to change the cap and join styles of those strokes, something you can only do by creating your own BasicStroke.

Getting even more complicated, you might someday want to have the outline of an object be dashed, rather than simply being solid. In that case, you could create a special BasicStroke that would tell it to draw whatever pattern of dashes you want.

BasicStroke is in the java.awt package, so you need to import it up at the top of any java file in which you intend to use it:

import objectdraw.*;
import java.awt.BasicStroke;

public class ....

Table of Contents

1. Setting the width, cap, and join styles of a BasicStroke

Your browser doesn't do Java.
To make a BasicStroke with a particular line width, use this constructor:

public BasicStroke(float lineWidth)

The float type is a decimal type ("floating point") like double, but it is more rarely used. If you are writing the decimal number but you want the computer to know that it is really a float, just put an "f" at the end of the number:

shape.setStroke(new BasicStroke(1.5f));

You would rarely do this, since shapes allow you to set the line width without constructing a new BasicStroke. More often you will use BasicStroke because you want to change other characteristics of how the stroke looks. There are two things you can change:The constructor that sets these styles looks like this:

public BasicStroke(float lineWidth, int capStyle, int joinStyle);

You set the capStyle and joinStyle by using constants in the BasicStroke class. Making this kind of strokes would look like this:

stroke = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
stroke = new BasicStroke(4.2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
stroke = new BasicStroke(.3f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER);

That first stroke is the default; the second is wider than it and the third narrower.

2. Creating a dashed BasicStroke

BasicStroke makes it easy to have a shape draw its lines with any pattern of dashes that you want. The constructor you will use for this looks like:

public BasicStroke(float lineWidth, int capStyle, int joinStyle, float miterLimit, float[] dashPattern, float dashPhase);

The miterLimit parameter here determines how "pointy" mitered joins are allowed to be; I always just set it to the default value of 10. The dashPhase can start you at some point other than the start of the dash pattern; I usually just set it to 0. The interesting part is the array of float values that will define the float pattern. If you haven't dealt with arrays yet, just carefully copy the examples below:

stroke = new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, new float[] {10, 10}, 0);
stroke = new BasicStroke(8, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10, new float[] {0, 10, 20, 10}, 0);

The list of number I create in the float[] list a length to draw, then a length to skip, then a length to draw again, and so on. You can list any number of dashes and breaks. So, the top one is a simple pattern of draw ten pixels, then skip ten pixels; the second pattern says to draw 0 pixels (just a dot), then skip 10, then draw 20 pixels, then skip 10. You can see these two patterns in the applet above.

3. Official specification for the BasicStroke class

You can read the official specification for the BasicStroke class here on the java website. I'll warn you that it's not all that helpful:

This page may help you more: