不能用Java2D绘制细线

时间:2022-09-05 21:52:27

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.

我尝试画一个有1个像素点的多边形。因为整个多边形的比例是100,所以我将线宽设为0。01。由于某些原因,这个多边形在屏幕上的线宽看起来是100像素而不是1。

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

我用一般路径作为多边形形状。如果我使用相同的方法来绘制线2d形状,会得到细线。

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.

新信息:如果我删除了setStroke line,我就会得到一个2像素的线条,因为之前在Graphics2D对象上设置了0.02f的BasicStroke。

This is the real setStroke line

这是真正的setStroke行

g.setStroke(new BasicStroke((float) (1f / getRoot().scaleX)));

3 个解决方案

#1


3  

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

不能用Java2D绘制细线

#2


1  

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

我不知道为什么,但它现在起作用了。

#3


0  

This also set 1px width for lines:

这也为线条设置了1px的宽度:

graphics2D.setStroke(new BasicStroke(0));

#1


3  

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

不能用Java2D绘制细线

#2


1  

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

我不知道为什么,但它现在起作用了。

#3


0  

This also set 1px width for lines:

这也为线条设置了1px的宽度:

graphics2D.setStroke(new BasicStroke(0));