简体   繁体   English

Graphics2D线条和形状绘图问题(在错误的位置渲染)

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

I've drawn three arrows using Graphics2D. 我已经使用Graphics2D绘制了三个箭头。

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

Here's what it looks like enlarged: 这是放大后的样子:

绘制的箭头

I cannot understand two things: 我不明白两件事:

  1. Why is the filled one smaller and shifted? 为什么填充的小些并移位?
  2. Secondly, why do arrows 1. and 3. look different? 其次,为什么箭头1.和3.看起来不同? Both consist of 3 antialiased lines. 两者均由3条抗锯齿线组成。 Shouldn't they (potentially) differ only in vertices? 它们(可能)仅在顶点上不同吗?

Here's the whole code: 这是整个代码:

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

It seems I have found the answers to my question. 看来我已经找到了问题的答案。 I post them for other poeple who may face the same problem. 我将它们发布给可能面临相同问题的其他人。

Smaller , because, as MadProgrammer said in a comment below the question, stokes are drawn along the edges through the middle, so a 1px stroke edges will be 0.5px to the each side of the shape edges. 较小 ,因为正如MadProgrammer在问题下方的评论中所述,笔画沿着中间的边缘绘制,因此1px笔触边缘将与形状边缘的每一边相距0.5px。

Shifted because of rounding. 由于舍入而移位 When you draw a line with a float-precision coordinates it can be normalized in some way on some platforms. 当您绘制带有浮点精度坐标的线时,可以在某些平台上以某种方式对其进行标准化。 On Windows, at least for Path2D.Float and Line2D.Float , it rounds coords to integer numbers. 在Windows上,至少对于Path2D.FloatLine2D.Float ,它会将坐标四舍五入为整数。 I guess that the same goes for fill(Shape) . 我想fill(Shape) Fotrunatelly, you can disable stroke normalization by: 可以通过以下方式禁用笔划归一化:

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

It solves the problem: 它解决了这个问题:

箭头

Different , because of different rendering algorithms. 不同 ,因为渲染算法不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM