Java: line-stroke with filled shape
This is not really important, but it was bothering me for a little w开发者_开发技巧hile.
Problem description:
Given: a line (Line2D) Wanted: drawing the line as a wedge (Filled GeneralPath)Surely this can be done by creating the wedge as General Path and then filling it by the Graphics (my solution).
My first approach would have been a wedge-stroked line, because I didn't want to change the line object for some reason, also I just wanted to draw the line object and not think about it any more. Creating the wedge-stroke was no problem (Some calculations and then creating the General Path) - but I was not able to fill it (easily)
Apparently it seems the fill of Graphics2D only fills the shape it gets - and does not handle the filling of the stroke (that behavior makes sense if one thinks about it).
Question: Is there any way to fill a shape of a Stroke (filling a shape - more specifically: a GeneralPath - somehow before drawing it)?
May be BasicStroke.public Shape createStrokedShape(Shape s)
can help if you pass the Line2D there?
Once you use createStrokedShape()
, note that draw()
"strokes the outline of a Shape
," while fill()
"fills the interior of a Shape
."
import java.awt.*;
public static Shape strokeToShape(Shape path, float strokeWidth)
return new BasicStroke(strokeWidth).createStrokedShape(path);
}
You may also specify cap
and join
parameters of BasicStroke
精彩评论