Java2D图形消除锯齿 [英] Java2D Graphics anti-aliased

查看:198
本文介绍了Java2D图形消除锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手,尝试使用Java2D Graphics创建图像。但是输出结果是反锯齿的。我尝试了很多方法来纠正它,但不起作用。

  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());
图形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();
返回bi;
}

任何人都可以帮我解决错误吗?

解决方案

假设您确实需要平滑(无别名)文本, TextLayout 可能会使这更简单。 FontRenderContext code> 构造函数可以管理反锯齿和小数指标设置。



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



附录:在Mac OS X上, Pixie / Developer / Applications / Graphics Tools / 中的c $ c>应用程序可以方便地检查anti-alias像素。在其他平台上,可以使用 Zoom





< pre $ / ** @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();
返回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(){
$ b $ @Override
public void run(){
new BITest()。display();
}
});
}
}


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.

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?

解决方案

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

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

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

/** @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();
            }
        });
    }
}

这篇关于Java2D图形消除锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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