Graphics2D 线条和形状绘制问题(在错误的位置呈现)

Graphics2D line and shape drawing issues (rendered in wrong place)

我使用 Graphics2D 绘制了三个箭头。

  1. 三个drawLine
  2. draw(Shape)
  3. fill(Shape)

这是放大后的样子:

我不明白两件事:

  1. 为什么填满的变小了而且偏移了?
  2. 其次,为什么箭头1.和3.看起来不一样?两者都包含 3 条抗锯齿线。它们不应该(可能)只在顶点上不同吗?

完整代码如下:

import javax.swing.*;
import java.awt.*;

public class ShapeTest extends JPanel
{
    public static void main(String [] args)
    {
        JFrame frame = new JFrame();
        frame.setSize(new Dimension(220, 200));
        frame.add(new ShapeTest());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics graphics)
    {
        super.paintComponent(graphics);

        Graphics2D graphics2D = (Graphics2D)graphics;

        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, this.getWidth(), this.getHeight());

        graphics2D.setColor(Color.black);
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.drawLine(100, 40, 103, 46);
        graphics2D.drawLine(103, 46, 106, 40);
        graphics2D.drawLine(100, 40, 106, 40);

        graphics2D.fill(new Polygon(
                new int[]{100, 103, 106},
                new int[]{50, 56, 50},
                3
        ));

        graphics2D.draw(new Polygon(
                new int[]{100, 103, 106},
                new int[]{60, 66, 60},
                3
        ));
    }
}

看来我找到了问题的答案。我 post 他们是为了其他可能面临同样问题的人。

Smaller,因为,正如 MadProgrammer 在问题下方的评论中所说,描边是沿着中间的边缘绘制的,因此 1px 的描边边缘将是形状边缘的每一侧 0.5px。

移位 因为四舍五入。当您使用 float-precision 坐标绘制一条线时,可以在某些平台上以某种方式对其进行归一化。在 Windows 上,至少对于 Path2D.FloatLine2D.Float,它将坐标舍入为整数。我想 fill(Shape) 也是如此。 Fotrunatelly,您可以通过以下方式禁用笔划归一化:

g2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

解决问题:

不同,因为不同的渲染算法。