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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/179955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:58:40  来源:igfitidea点击:

How do you enable anti aliasing in arbitrary Java apps?

javaantialiasing

提问by rcreswick

Many Java Apps don't use anti-aliased fonts by default, despite the capability of Swing to provide them. How can you coerce an arbitrary java application to use AA fonts? (both for applications I'm running, and applications I'm developing)

尽管 Swing 能够提供抗锯齿字体,但许多 Java 应用程序默认不使用抗锯齿字体。如何强制任意 Java 应用程序使用 AA 字体?(对于我正在运行的应用程序和我正在开发的应用程序)

采纳答案by rcreswick

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

回答by anjanb

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. I added -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true to the batch file that launches it BUT that did NOT make a difference. Guess, I have to ask in their forum.

谢谢(你的)信息。我自己也在想这个。我使用 SoapUI(www.eviware.com),默认情况下它不使用 AA 文本。我将 -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true 添加到启动它的批处理文件中,但没有任何区别。猜猜,我必须在他们的论坛上问。

回答by Al-Khwarizmi

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.
  • But if I use this, I get the classical black-and-white antialiasing which is much uglier.
  • 如果我不在我的代码中使用它,我会得到漂亮的 ClearType 子像素渲染。
  • 但是如果我使用它,我会得到经典的黑白抗锯齿,这要丑得多。

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 版本时,它就不再需要了。

回答by Luke Usherwood

Swing controls in the latest versions of Java 6 / 7 should automatically abide by system-wide preferences. (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?

最新版本的 Java 6 / 7 中的 Swing 控件应自动遵守系统范围的首选项。(如果您在 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.drawTextor 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