在paintComponent() 方法中抗锯齿 [英] Anti-aliasing in paintComponent() method
本文介绍了在paintComponent() 方法中抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 paintComponent(..)
方法打印一些文本.
I want to print some text using the paintComponent(..)
method.
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.drawString("Hello world", 10, 10);
}
但是文字有点锯齿.在这种方法中,您如何强制使用 [抗锯齿] 进行文本绘制?
But the text is somewhat jaggy. How could you force text drawing with [anti-aliasing] in this method?
谢谢.
推荐答案
您可以通过以下方式设置双缓冲:
You can set double buffering by:
class MyPanel extends JPanel {
public MyPanel() {
super(true);//set Double buffering for JPanel
}
}
或者直接调用<代码>JComponent#setDoubleBuffered(..).
or simply call JComponent#setDoubleBuffered(..)
.
您还可以将 Graphics2D
对象的 RenderingHint
s 设置为 anti-aliasing 和 text anti-aliasing通过以下方式提高 Swing 绘画质量:
You can also set RenderingHint
s for Graphics2D
objects like anti-aliasing and text anti-aliasing to improve Swing painting quality by:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D) g;
//Set anti-alias!
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Set anti-alias for text
graphics2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
graphics2D.setColor(Color.red);
graphics2D.drawString("Hello world", 10, 10);
}
这篇关于在paintComponent() 方法中抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文