3

我使用 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
        ));
    }
}
4

1 回答 1

3

看来我已经找到了我的问题的答案。我将它们发布给其他可能面临同样问题的人。

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

由于四舍五入而移位。当您绘制具有浮点精度坐标的线时,可以在某些平台上以某种方式对其进行归一化。在 Windows 上,至少对于Path2D.Floatand Line2D.Float,它会将坐标四舍五入为整数。我想这同样适用fill(Shape)。Fotrunatelly,您可以通过以下方式禁用笔画归一化:

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

它解决了这个问题:

箭头

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

于 2017-02-12T23:04:34.670 回答