仿射变换后的对角线难看

时间:2019-05-02 19:53:18

标签: java awt rendering antialiasing affinetransform

我正在研究低端设备上的图形。我注意到,在进行比例仿射变换后,许多细线(例如1px宽度)变得丑陋。特别是在对角线的情况下(例如,倾斜45度的线)。

我准备了带有最明显示例的演示程序(我提供了样板GUI代码,因此您可以运行示例):

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;

public class DiagonalLinesDemo extends JPanel
{
    public void paintComponent(Graphics g)
    {
        Graphics2D graphics = (Graphics2D) g;
        graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

        drawRemark(graphics, 1F, 20);
        drawDiagonalLine(graphics, 1F, 0);

        drawRemark(graphics, 1.1070F, 80);
        drawDiagonalLine(graphics, 1.1070F, 60);

        drawRemark(graphics, 1.0162F, 160);
        drawDiagonalLine(graphics, 1.0162F, 140);

        drawRemark(graphics, 1.3300F, 250);
        drawDiagonalLine(graphics, 1.3300F, 160);

        drawRemark(graphics, 1.5555F, 330);
        drawDiagonalLine(graphics, 1.5555F, 180);
    }

    private static void drawDiagonalLine(Graphics2D graphics, float scale, int anchor)
    {
        GeneralPath path = new GeneralPath(Path2D.WIND_EVEN_ODD);
        path.moveTo(anchor + 10, 100);
        path.lineTo(anchor + 60, 50);

        AffineTransform upScaleTransform = new AffineTransform(
                scale,
                0F,
                0F,
                scale,
                0F,
                0F);

        graphics.setTransform(upScaleTransform);

        graphics.setColor(Color.BLACK);
        graphics.draw(path);
    }

    private static void drawRemark(Graphics2D graphics, float scale, int anchor)
    {
        graphics.setTransform(new AffineTransform());
        graphics.setColor(Color.BLUE);
        String remark = String.format("%1.4f", scale);
        graphics.drawString(remark, anchor, 50);
    }

    public static void main(String args[])
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Diagonal Lines Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBackground(Color.white);
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

        DiagonalLinesDemo panel = new DiagonalLinesDemo();
        frame.add(panel);
        frame.setVisible(true);
    }

    private static final int PANEL_WIDTH = 400;
    private static final int PANEL_HEIGHT = 200;
    private static final int FRAME_WIDTH = PANEL_WIDTH + 50;
    private static final int FRAME_HEIGHT = PANEL_HEIGHT + 50;
}

结果是:

enter image description here

从屏幕快照中可以看到,转换后(为清楚起见,刻度值标记在线条上方)出现了难看的裂缝。我知道由于没有抗锯齿而导致的骨折是不可避免的(由于客户的限制,我无法启用抗锯齿)。但我希望获得或多或少的漂亮结果,如下所示(我用红色突出显示了所需的形状):

enter image description here

我尝试调整绕线规则,以显示提示。但是没有运气。另外,我知道interpolation。但是这种方法太浪费资源了(正如我之前说的,客户的硬件资源非常有限)。还有其他选择吗?

0 个答案:

没有答案