tags:

views:

53

answers:

3

I'm trying to draw a polygon with a stroke of 1 pixel. Because the entire polygon is scaled by 100, I set the line width to 0.01. For some reason though, the polygon gets drawn with an on-screen line width of what looks to be 100 pixels instead of 1.

I'm using GeneralPath as the polygon shape. Thin lines do get drawn if I use the same approach for drawing Line2D shapes.

g2d.scale(100, 100);
g2d.setStroke(new BasicStroke(0.01f));
g2d.draw(theShape);

New info: If I remove the setStroke line I correctly get a 2 pixel line, since a BasicStroke of 0.02f was set on the Graphics2D object earlier.

This is the real setStroke line

g.setStroke(new BasicStroke((float) (1f / getRoot().scaleX)));
+1  A: 
aioobe
That's strange. I don't think there's a forgotten scale. The size of the shape is ok, just the strokes are too wide.
Bart van Heukelom
Are you setting the stroke on the correct graphics object? Care to post more of your code?
aioobe
Yeah I'm setting it on the right object. Anyway, new info added to question. If that doesn't help I'll post more.
Bart van Heukelom
I changed something, don't know what, but it works now. If I can reproduce the error again I'll let know here. Accepting this answer because you put still put good effort in trying to help.
Bart van Heukelom
A: 

The Problem coulb be that you are applying the scaling to the complete Graphics2D context. youll have to use an AffineTransform which is set to scale and then apply the transform to the shape only, not the hole context. something like:

AffineTransform trans=new AffineTransform();
trans.setToScale(100,100);
Shape scaledShape=trans.createTransformedShape(theShape);
g2d.draw(scaledShape);

hope that helps..

smeg4brains
I really don't think that's it because it works fine with lines, or if I remove the setStroke (see above). I couldn't do it that way anyway, because the shape is in a tree and the scaling is done in one of its ancestors. Thanks anyway
Bart van Heukelom
A: 

I don't know why, but it works now.

Bart van Heukelom
I usually blame cosmic radiation in these cases.
aioobe