无法用Java2D画细线

发布于 2024-09-03 20:09:48 字数 497 浏览 2 评论 0原文

我正在尝试绘制一个笔画为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2024-09-10 20:09:48

以下代码产生如下所示的输出。您的代码中的其他地方一定有错误。也许您在问题中省略了对 scale 的另一个调用:

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);
    }
}

在此处输入图像描述

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

如梦初醒的夏天 2024-09-10 20:09:48

零宽度笔画是否应该绘制细线或什么都不绘制似乎有点争议: https://bugs.openjdk.java.net/browse/JDK-8111947

我对此很幸运。所有转换完成后调用。

// 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文