5

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)));
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Bart van Heukelom
  • 43,244
  • 59
  • 186
  • 301

2 Answers2

5

The following code produces the output show below. You must have an error elsewhere in your code. Perhaps another call to scale that you have omitted in your question:

import java.awt.*;

public class FrameTest {
    public static void main(String[] args) throws InterruptedException {

        JFrame f = new JFrame("Demo");
        f.getContentPane().setLayout(new BorderLayout());    
        f.add(new JComponent() {
            public void paintComponent(Graphics g) { 
                Graphics2D g2d = (Graphics2D) g;

                GeneralPath theShape = new GeneralPath();
                theShape.moveTo(0, 0);
                theShape.lineTo(2, 1);
                theShape.lineTo(1, 0);
                theShape.closePath();

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

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setVisible(true);
    }
}

enter image description here

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 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 Jun 04 '10 at 13:02
  • Are you setting the stroke on the correct graphics object? Care to post more of your code? – aioobe Jun 04 '10 at 13:03
  • 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 Jun 04 '10 at 13:07
  • 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 Jun 04 '10 at 13:46
0

It seems to be a little controversial whether a zero width stroke should draw a hairline or nothing at all: https://bugs.openjdk.java.net/browse/JDK-8111947

I've had good luck with this. Call after all of your transforms are in place.

// Make a 2 pixel wide stroke
Stroke stroke = new BasicStroke(2/(float)Math.min(Math.abs(g2d.getTransform().getScaleX()),
                                                  Math.abs(g2d.getTransform().getScaleY())));
g2d.setStroke(stroke);
g2d.draw(shapeForDrawing); // convert center to upper left corner as required by Ellipse2D.Double
Ames
  • 91
  • 1
  • 4