无法用Java2D画细线
我正在尝试绘制一个笔画为 1 像素的多边形。因为整个多边形缩放了100,所以我将线宽设置为0.01。但由于某种原因,多边形的屏幕线宽看起来是 100 像素而不是 1。
我使用 GeneralPath
作为多边形形状。如果我使用相同的方法绘制 Line2D
形状,确实会绘制出细线。
g2d.scale(100, 100);
g2d.setStroke(new BasicStroke(0.01f));
g2d.draw(theShape);
新信息:如果我删除 setStroke 线,我会正确地得到一条 2 像素线,因为之前在 Graphics2D 对象上设置了 0.02f 的 BasicStroke。
这才是真正的setStroke线
g.setStroke(new BasicStroke((float) (1f / getRoot().scaleX)));
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)));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下代码产生如下所示的输出。您的代码中的其他地方一定有错误。也许您在问题中省略了对
scale
的另一个调用: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:零宽度笔画是否应该绘制细线或什么都不绘制似乎有点争议: https://bugs.openjdk.java.net/browse/JDK-8111947
我对此很幸运。所有转换完成后调用。
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.