Fill and stroke
The previous example showed how to draw a filled rectangle. However, you may also want to stroke the rectangle, i.e., draw its outline.
Stroking a rectangle is very similar to filling it; however, instead of the FillRectangle
method, you need to use the StrokeRectangle
method:
The StrokeRectangle
method takes a number of optional arguments that are not used by FillRectangle
. These are:
-
A
double
determining the line thickness. -
A
LineCaps
enumeration that determines the line cap used to draw the rectangle. This parameter is useful when stroking open paths (i.e., lines where the start point does not coincide with the end point). Since rectangles are closed, this parameter does not actually affect the plot.If the path were open, a value of
LineCaps.Butt
would specify a stroke that starts and ends abruply at the start and end points of the path. A value ofLineCaps.Round
uses specifies rounded line endings. A value ofLineCaps.Square
specifies line endings that extend beyond the start and end point, by a value corresponding to half the line thickness. -
A
LineJoins
enumeration that determines the way the lines are joined at the sides.If this is
LineJoins.Miter
, a sharp corner is used. If this isLineJoins.Round
, a round corner is used. If this isLineJoins.Bevel
, a bevelled corner is used. -
A
LineDash
object, specifying how the line is dashed. ALineDash
object is created by specifying how many units the line should be “on” (i.e., drawn) and “off” (i.e. not drawn). A third parameter to theLineDash
constructor specifies the “phase” of the dashing pattern with respect to the start point.A
LineDash
where the units on and off are both 0 corresponds to a solid line.Additional overloads of the
LineDash
struct accept adouble[]
parameter, which can be used to specify complex dash patterns.
The following code strokes a rectangle with a thick outline, rounded corners, and a dashed line.
More complex dash patterns can be created by using one of the LineDash
constructors that accept a double[]
parameter. For example:
If you wish to both fill and stroke a rectangle, you will have to call both FillRectangle
and StrokeRectangle
. This also gives you the flexibility to choose whether the stroke is applied on top or below the fill.