当我在Java 6下运行swing GUI应用程序时,它们会自动使用我为所有字体配置的子像素抗锯齿设置。与标准AA选项相比,结果有了很大改进。
但是当我绘制到图像时,我无法初始化图形上下文以使用系统的AA配置。尝试使用Java的不同AA提示是一个失败的原因,因为没有子像素方法将适用于所有用户。
有没有办法继承给定图形上下文的系统AA设置,而不必选择一个并明确设置提示?目前我必须使用GASP AA来避免标准AA用小字体给出的可怕结果。我试过没有为文本AA设置任何内容,也没有设置任何AA提示。
更新2010-01-05
我想我已经把它钉死了;当直接绘制到AWT图形上下文时,子像素AA提示似乎只被尊重;当我画到双缓冲图像时,它只是标准AA;但是当我绕过双缓冲图像时,完成了子像素AA。
否则,The_Fire的答案将适用于具有Swing可用的JVM(但不适用于J2ME JVM);请注意,The_Fire的答案不能使用AWT组件(使用新的Label()而不是新的JLabel()失败),可能是因为在将组件实现到显示器之前无法提取FontRenderContext。
我目前为目标图像获取图形上下文的代码如下所示:
try {
if((dbImage=dctRoot.createImage(wid,hgt,1))!=null) { // if createImage returns null or throws an exception the component is not yet displayable
dbGraphics=(Graphics2D)dbImage.getGraphics();
if(dctRoot.properties.getBoolean("Antialias",true)) {
try {
// set AA on overall
dbGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING ,RenderingHints.VALUE_ANTIALIAS_ON);
// set text AA to platform/impl default
dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
// try to override platform/impl AA with font-specified AA (Java 6+)
try { dbGraphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.class.getField("VALUE_TEXT_ANTIALIAS_GASP").get(null)); } catch(Throwable thr) {;} // yes, ignore exception
}
catch(Throwable thr) {
dctRoot.log.println("Antialiasing not supported on this JVM ("+thr+").");
dctRoot.setProperty("Antialias","False"); // turn off AA for subsequent painting
}
}
}
}
catch(Throwable thr) {
dbImage=null;
dbGraphics=null;
}
创建图像的代码使用底层的AWT组件,它构成了我所有绘画的背景 - 组件是一个Panel,因为我需要能够执行setFocusCycleRoot,因此它可以很好地与其他AWT组件一起使用。创建图像代码如下:
public DctImage createImage(int wid, int hgt, float accpty) {
GraphicsConfiguration cfg=awtComponent.getGraphicsConfiguration();
Image img=null;
if(transparentImages) {
//y { img=new BufferedImage(wid,hgt,BufferedImage.TYPE_INT_ARGB); } // NB: J2ME CDC/PP 1.1 does not have the BufferedImage constructors (one day I may discover a way to create a BufferedImage via another API!!)
try { img=cfg.createCompatibleImage(wid,hgt,Transparency.TRANSLUCENT); }// NB: J2ME CDC/PP 1.1 does not have this API, but prefer to use GraphicsConfiguration over new BufferImage(...)
catch(NoClassDefFoundError thr) { transparentImages=false; createImage(wid,hgt,accpty); } // try again with transparency disabled
catch(NoSuchMethodError thr) { transparentImages=false; createImage(wid,hgt,accpty); } // try again with transparency disabled
catch(NoSuchFieldError thr) { transparentImages=false; createImage(wid,hgt,accpty); } // try again with transparency disabled
}
else {
img=cfg.createCompatibleImage(wid,hgt);
}
if(accpty>0 && SET_ACCELERATION_PRIORITY!=null) {
try { SET_ACCELERATION_PRIORITY.invoke(img,new Object[]{new Float(accpty)}); } catch(Throwable thr) {;}
}
return (img==null ? null : new DctImage(img));
}