سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

What you are seeing in your first image looks almost like ''miters''. Miters are a way to draw line joins in a path where the two outer borders of the lines that are joined are extended until they intersect and the enclosing area is filled as well.

Is it possible that your geometry contains consecutive points with almost the same coordinates? The following example exhibits the same problem because of the last two points with have almost identical coordinates.

JFrame frame = new JFrame();

frame.setSize(300, 300);
frame.setContentPane(new Container() {
    @Override
    public void paint(Graphics graphics) {
        Graphics2D g2 = (Graphics2D) graphics;
        g2.setStroke(new BasicStroke(5));
        g2.setRenderingHint(KEY_ANTIALIASING, VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(KEY_STROKE_CONTROL, VALUE_STROKE_PURE);

        Path2D.Double path = new Path2D.Double();
        path.moveTo(200, 100);
        path.lineTo(100, 100);
        path.lineTo(101, 100.3);

        g2.draw(path);
    }
});

frame.setVisible(true);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top