Java2D 图形抗锯齿

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4285464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:23:30  来源:igfitidea点击:

Java2D Graphics anti-aliased

javagraphicsjava-2dantialiasing

提问by Prakash

I am new to Java and trying to use Java2D Graphics to create a Image. But the output is coming as anti-aliased. I tried many ways to rectify it but doesn't work. The characters are getting distorted or jagged.

我是 Java 新手并尝试使用 Java2D 图形来创建图像。但是输出是抗锯齿的。我尝试了很多方法来纠正它,但没有奏效。字符变得扭曲或锯齿状。

public BufferedImage createNameOnButton(String label) {
    int messageWidth = 0;
    Font font = new Font("Arial", Font.PLAIN, 11);

    BufferedImage bi = new BufferedImage(
        10, 10, BufferedImage.TYPE_INT_RGB);

    Graphics2D g2d = (Graphics2D) bi.getGraphics();
    g2d.setRenderingHint(
        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(
        RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g2d.setRenderingHint(
        RenderingHints.KEY_FRACTIONALMETRICS,
        RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setFont(font);

    bi = g2d.getDeviceConfiguration()
        .createCompatibleImage(500, 30, Transparency.BITMASK);
    FontMetrics fm = bi.getGraphics().getFontMetrics(font);
    int messageHeight = fm.getHeight() - fm.getDescent();
    for (char ch : label.toCharArray()) {
        messageWidth += fm.charWidth(ch);
    }

    bi = bi.getSubimage(50, 0, messageWidth + 10, fm.getHeight());
    Graphics g = bi.getGraphics();
    g.setColor(Color.black);
    AttributedString as = new AttributedString(label);
    as.addAttribute(TextAttribute.FONT, font);
    g.drawString(as.getIterator(), 5, messageHeight);
    g2d.dispose();
    g.dispose();
    return bi;
}

Can anyone please help me to rectify the error?

任何人都可以帮我纠正错误吗?

回答by Jim

As Traroth mentioned it is more than likely due to

正如 Traroth 提到的,这很可能是由于

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

Instead this should be

相反,这应该是

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

回答by trashgod

Assuming you actually want smooth (non-aliased) text, TextLayoutmay make this easier. The FontRenderContextconstructor can manage the anti-aliasing and fractional metrics settings.

假设您实际上想要平滑(无锯齿)文本,TextLayout可能会使这更容易。该FontRenderContext构造可以管理抗锯齿和小数规格设置。

Addendum: Using g2d.setColor(Color.blue)seems to produce the expected effect.

附录:使用g2d.setColor(Color.blue)似乎产生了预期的效果。

Addendum: On Mac OS X, the Pixieapplication in /Developer/Applications/Graphics Tools/is convenient for examining the anti-alias pixels. On other platforms, Zoommay be used.

附录:在 Mac OS X 上,Pixie应用程序/Developer/Applications/Graphics Tools/可以方便地检查抗锯齿像素。在其他平台上,Zoom可能会用到。

alt text

替代文字

/** @see https://stackoverflow.com/questions/4285464 */
public class BITest extends JPanel {

    private BufferedImage image = createNameOnButton("Sample");

    public BITest() {
        this.setPreferredSize(new Dimension(
            image.getWidth(), image.getHeight()));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }

    public BufferedImage createNameOnButton(String label) {
        Font font = new Font("Arial", Font.PLAIN, 64);
        FontRenderContext frc = new FontRenderContext(null, true, true);
        TextLayout layout = new TextLayout(label, font, frc);
        Rectangle r = layout.getPixelBounds(null, 0, 0);
        System.out.println(r);
        BufferedImage bi = new BufferedImage(
            r.width + 1, r.height + 1,
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = (Graphics2D) bi.getGraphics();
        g2d.setColor(Color.blue);
        layout.draw(g2d, 0, -r.y);
        g2d.dispose();
        return bi;
    }

    private void display() {
        JFrame f = new JFrame("BITest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setUndecorated(true);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new BITest().display();
            }
        });
    }
}

回答by Stefan Reich

I found that just this linedoes the trick:

我发现只有这一行才能解决问题:

  g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

After that, just use drawString().

之后,只需使用drawString()