子像素渲染

时间:2016-09-22 18:25:08

标签: java graphics 2d subpixel

我正在尝试并且失败,使用子像素定位在Java中渲染2d形状。下面的尝试失败(暂时忽略scaleAffineTransform):

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class TestFrame extends Frame {
    private static final int D = 64;

    public static void main(String args[]) {
        new TestFrame(D, D);
    }

    private final Insets ins;
    private final double scale = 1;

    TestFrame(int w, int h) {
        ins = getInsets();
        final Dimension dim = new Dimension(w + ins.left + ins.right, h + ins.top + ins.bottom);

        pack();

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });

        setSize(dim);
        setLocationRelativeTo(null);
        setBackground(Color.BLACK);
        setVisible(true);
        repaint();
    }

    @Override
    public void paint(Graphics gfx) {
        super.paint(gfx);
        final Graphics2D g2d = (Graphics2D)gfx;
        g2d.setTransform(new AffineTransform(1.0/scale, 0.0, 0.0, 1.0/scale, ins.left, ins.top));
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke((float)scale));
        g2d.setColor(Color.WHITE);

        for (double deg = 0; deg < 90.0; deg += 2.0) {
            final double rad = deg / 180.0 * Math.PI;
            final double x1 = Math.cos(rad) * D * scale;
            final double y1 = Math.sin(rad) * D * scale;
            final double x2 = Math.cos(rad) * (D - 4.0) * scale;
            final double y2 = Math.sin(rad) * (D - 4.0) * scale;
            g2d.draw(new Line2D.Double(x1, y1, x2, y2));
        }
    }
}

这会产生以下输出:

Rendering

线条应均匀分布,但正如您所见,它们不是。我的猜测是开始点和结束点四舍五入到最近的像素。

使用Line2D.Double(我已经在做,显然不起作用)或使用AffineTransform缩小比例提出SO的建议解决方案。如果您将代码示例中的scale变量更改为100,则会以更高的比例绘制线条,然后根据建议使用变换将其缩小。这对结果图像也没有影响。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

解决方案设置了一个渲染提示:

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

引用Javadocs

  

笔划归一化控制提示值 - 几何应保持不变,并以亚像素精度渲染。