Java2D Graphics消除锯齿

Pra*_*ash 6 java graphics java-2d antialiasing

我是Java的新手,并尝试使用Java2D Graphics来创建Image.但输出是反锯齿的.我尝试了很多方法来纠正它,但是没有用.角色变得扭曲或锯齿状.

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;
}
Run Code Online (Sandbox Code Playgroud)

有谁可以帮我纠正错误?

tra*_*god 11

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

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

附录:在Mac OS X上,Pixie应用程序/Developer/Applications/Graphics Tools/便于检查抗锯齿像素.在其他平台上,Zoom可以使用.

替代文字

/** @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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


Jim*_*Jim 5

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

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
Run Code Online (Sandbox Code Playgroud)

相反,这应该是

g2d.setRenderingHint(
    RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.setRenderingHint(
    RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
Run Code Online (Sandbox Code Playgroud)

  • 您似乎不明白抗锯齿的含义。抗锯齿=平滑。 (8认同)