How do you enable anti aliasing in arbitrary Java apps?

JavaAntialiasing

Java Problem Overview


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)

Java Solutions


Solution 1 - Java

If you have access to the source, you can do this in the main method:

  // 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:

-Dawt.useSystemAAFontSettings=on

Solution 2 - Java

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?

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):

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

Solution 3 - Java

For the record, I have found out that in my windows 7 machine,

  • 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.

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrcreswickView Question on Stackoverflow
Solution 1 - JavarcreswickView Answer on Stackoverflow
Solution 2 - JavaLuke UsherwoodView Answer on Stackoverflow
Solution 3 - JavaAl-KhwarizmiView Answer on Stackoverflow
Solution 4 - JavaanjanbView Answer on Stackoverflow