dakami
dakami

Reputation: 367

Java: line-stroke with filled shape

This is not really important, but it was bothering me for a little while.

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)?

Upvotes: 2

Views: 2337

Answers (3)

Oleg Mikhailov
Oleg Mikhailov

Reputation: 6081

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

Upvotes: 0

trashgod
trashgod

Reputation: 205875

Once you use createStrokedShape(), note that draw() "strokes the outline of a Shape," while fill() "fills the interior of a Shape."

Upvotes: 3

StanislavL
StanislavL

Reputation: 57421

May be BasicStroke.public Shape createStrokedShape(Shape s) can help if you pass the Line2D there?

Upvotes: 3

Related Questions