javadrawingjpaneljava-2dpath-2d

Java Path2D.Double on JPanel are painted with a 'tail'


I have a strange behaviour when drawing a Path2D on a JPanel.


Some of the shapes get kind of a tail as you can see on this screenshot:

enter image description here

When I change the type to Line2D.Double, it is as I'd expect it:

enter image description here

Here's the code that draws the path / line:

Path2D.Double path = new Path2D.Double();
Graphics2D g = (Graphics2D)this.getGraphics();
for(int i=0; i<geom.size(); i++)
{
    double x = ddGeom.getX(geom.get(i));
    double y = ddGeom.getY(geom.get(i));
    if(i==0)
        path.moveTo(x-draw_center.x, y-draw_center.y);
    path.lineTo(x-draw_center.x, y-draw_center.y);
}
g.draw(path);


Do you have an idea where the 'tails' in Screenshot1 come from? I use SDK Version 6.

Thank you very much for your help




Edit: When changing the code snippet to

if(i==0)
     path.moveTo(x-draw_center.x, y-draw_center.y);
else
     path.lineTo(x-draw_center.x, y-draw_center.y);

most (maybe 75%) of the tails disappear. Any idea why this happens?


Solution

  • I finally got it. Thanks to HovercraftFullOfEels hint 'strange Stroke' I played around with my strokes. Original stroke:

    BasicStroke stroke = new BasicStroke(2.0f);
    

    Changed to:

    BasicStroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);
    

    With the new Stroke all the 'tails' disappeared. I'm still not understanding why this happens, but if someone has the same problem, this workaround could help.

    I'd still be very interested in an explanation for this behaviour.

    Thank you for your great help