简体   繁体   English

如何在任意Java应用程序中启用抗锯齿?

[英]How do you enable anti aliasing in arbitrary Java apps?

Many Java Apps don't use anti-aliased fonts by default, despite the capability of Swing to provide them. 尽管Swing能够提供抗锯齿功能,但许多Java应用程序默认情况下不使用抗锯齿字体。 How can you coerce an arbitrary java application to use AA fonts? 如何强制使用任意java应用程序来使用AA字体? (both for applications I'm running, and applications I'm developing) (对于我正在运行的应用程序和我正在开发的应用程序)

If you have access to the source, you can do this in the main method: 如果您有权访问源,可以在main方法中执行此操作:

  // enable anti-aliased text:
  System.setProperty("awt.useSystemAAFontSettings","on");

or, (and if you do not have access to the source, or if this is easier) you can simply pass the system properties above into the jvm by adding these options to the command line: 或者,(如果您无权访问源,或者这样更容易),您可以通过将以下选项添加到命令行,将上面的系统属性传递到jvm:

-Dawt.useSystemAAFontSettings=on

Swing controls in the latest versions of Java 6 / 7 should automatically abide by system-wide preferences. 最新版本的Java 6/7中的Swing控件应自动遵循系统范围的首选项。 (If you're using the Windows L&F on a Windows OS then text should render using ClearType if you have that setting enabled system-wide.) So perhaps one solution could simply be: enable the native Look and Feel? (如果您在Windows操作系统上使用Windows L&F,那么如果您在系统范围内启用了该设置,则应使用ClearType呈现文本。)因此,也许一个解决方案可能只是:启用原生外观?

In applications you're developing, if you render your own text directly, you also need to do something like this (at some point before calling Graphics.drawText or friends): 在你正在开发的应用程序中,如果你直接渲染自己的文本,你还需要做这样的事情(在调用Graphics.drawText或朋友之前的某个时候):

if (desktopHints == null) { 
    Toolkit tk = Toolkit.getDefaultToolkit(); 
    desktopHints = (Map) (tk.getDesktopProperty("awt.font.desktophints")); 
}
if (desktopHints != null) { 
    g2d.addRenderingHints(desktopHints); 
} 

Reference: http://weblogs.java.net/blog/chet/archive/2007/01/font_hints_for.html 参考: http//weblogs.java.net/blog/chet/archive/2007/01/font_hints_for.html

For the record, I have found out that in my windows 7 machine, 为了记录,我发现在我的Windows 7机器中,

  • If I don't use this in my code, I get the nice ClearType subpixel rendering. 如果我不在代码中使用它,我会得到漂亮的ClearType子像素渲染。
  • But if I use this, I get the classical black-and-white antialiasing which is much uglier. 但是,如果我使用它,我会得到经典的黑白抗锯齿,这是非常丑陋的。

So this code should be used carefully. 因此应谨慎使用此代码。 I guess it will stop being needed at all when all Linux users have updated to the versions of OpenJDK that handle aliasing well by default. 我想当所有Linux用户都更新到默认情况下处理别名的OpenJDK版本时,它将完全不再需要。

thanks for the info. 谢谢(你的)信息。 I was wondering about this myself. 我自己也在想这个。 I use SoapUI(www.eviware.com) and it does NOT, by default, use AA text. 我使用SoapUI(www.eviware.com),默认情况下它不使用AA文本。 I added -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true to the batch file that launches it BUT that did NOT make a difference. 我将-Dawt.useSystemAAFontSettings = on -Dswing.aatext = true添加到启动它的批处理文件但是没有什么区别。 Guess, I have to ask in their forum. 猜猜看,我必须在他们的论坛中提问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM