java抗锯齿_请教一下Java2D图形抗锯齿

本文介绍了如何在Java2D中实现文本的平滑显示,特别是抗锯齿效果。通过使用`TextLayout`和`FontRenderContext`,可以创建平滑的文本布局,并调整抗锯齿和小数规格设置。此外,提到了在Mac OS X上使用特定工具检查抗锯齿效果的方法,以及一个用于演示的`BITest`类的代码示例,该类创建并显示带有平滑文本的`BufferedImage`。
摘要由CSDN通过智能技术生成

假设您实际上想要的是平滑(非锯齿)的文本,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();

}

});

}

}

分享编辑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值