我一直在尝试增强用Java编写的GUI系统以使用子像素抗锯齿并且已经成功,除了一个剩余的异常。这是my other question from yesterday的后续内容。
剩下的问题是,将设置渲染提示KEY_ANTIALIASING设置为VALUE_ANTIALIAS_ON会导致KEY_TEXT_ANTIALIASING在设置为LCD(子像素)AA值时被忽略。任何人都可以对此有所了解吗?目前我在渲染文本之前被迫使用VALUE_ANTIALIAS_OFF,并在渲染文本后将其重新打开(以便其他绘画,如圆圈等,是AA)。
下面的独立测试程序证明了这个问题。正如您所看到的,如果您运行它,当字体不是时,圆圈会涂上AA,反之亦然。将AA预配置为所有绘画工作会很好。
import java.awt.*;
import java.awt.event.*;
public class AwtTestFrame1c extends Panel {
AwtTestFrame1c() {
setBackground(SystemColor.control);
}
public void paint(Graphics gc) {
Graphics2D g2d = (Graphics2D)gc;
int py=0;
py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,true );
py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,false);
}
private int paintText(Graphics2D dgc, int py, Object val, boolean aa) {
char[] txt=("The quick brown fox jumped over the lazy dog ("+val+", General AA: "+aa+")").toCharArray();
if(aa ) { dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON ); }
else { dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); }
if(val !=null) { dgc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,val); }
dgc.setFont(font);
dgc.drawOval(5,py+5,15,15);
dgc.drawChars(txt,0,txt.length,30,py+line-5);
return (py+line);
}
static private final Font font=new Font("SansSerif",Font.PLAIN,16);
static private final int line=25;
static public void main(String[] args) {
Frame wnd=new Frame("AWT Antialiased Text Sample");
wnd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
wnd.add(new AwtTestFrame1c());
wnd.setSize(new Dimension(1000, 300));
wnd.setVisible(true);
}
}