Graphics2D线条和形状绘图问题(在错误的位置渲染) [英] Graphics2D line and shape drawing issues (rendered in wrong place)

查看:135
本文介绍了Graphics2D线条和形状绘图问题(在错误的位置渲染)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Graphics2D绘制了三个箭头.

I've drawn three arrows using Graphics2D.

  1. 三个drawLine s
  2. draw(Shape)
  3. fill(Shape)
  1. three drawLines
  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
        ));
    }
}

推荐答案

似乎我已经找到了问题的答案.我将它们发布给可能遇到相同问题的其他人.

It seems I have found the answers to my question. I post them for other poeple who may face the same problem.

更小,因为正如 MadProgrammer 在问题下方的评论中所说,斯托克斯是沿边缘穿过中间绘制,因此1px的笔触边缘将与形状边缘的每一边相距0.5px.

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.

移位是由于四舍五入.当您绘制带有浮点精度坐标的线时,可以在某些平台上以某种方式对其进行标准化.在Windows上,至少对于Path2D.FloatLine2D.Float,它将四舍五入为整数.我想fill(Shape)也是如此.可以通过以下方式禁用笔划归一化:

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. I guess that the same goes for fill(Shape). Fotrunatelly, you can disable stroke normalization by:

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

它解决了这个问题:

不同,因为呈现算法不同.

Different, because of different rendering algorithms.

这篇关于Graphics2D线条和形状绘图问题(在错误的位置渲染)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆