52

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)

4 Answers 4

79

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
5
9

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

6

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.

2
  • 7
    Try System.setProperty("awt.useSystemAAFontSettings", "lcd"); and you should get ClearType.
    – Domchi
    May 3, 2011 at 0:55
  • As a linux and windows user I do not like "cleartype". I purposely disable it.
    – Wyatt Ward
    Dec 5, 2014 at 17:25
0

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.

5
  • it may be interpreting the arguments as pertaining to the application instead of the jvm -- you may need to dig into the bat file a bit deeper. (or it may not work for all apps... if so, please let me know / vote my answer down..)
    – rcreswick
    Oct 7, 2008 at 20:05
  • 1
    I was able to get this to work by editing the .sh launch file so that the last line read: java -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true -Dsoapui.properties=soapui.properties -Dgroovy.source.encoding=iso-8859-1 -cp $SOAPUI_CLASSPATH com.eviware.soapui.SoapUIPro $*
    – rcreswick
    Oct 7, 2008 at 20:14
  • 1
    I believe you could do the same with the soapui bat file by adding the -D.... params listed here to the JAVA_OPTS line, or just specifying JAVA_OPTS=-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true in the surrounding environment
    – rcreswick
    Oct 7, 2008 at 20:16
  • For SoapUI on Windows, I edited the SoapUI-5.0.0.vmoptions file in the "C:\Program Files (x86)\SmartBear\SoapUI-5.0.0\bin" folder and added -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true as the last line.
    – Aidan
    Jun 26, 2014 at 23:52
  • Also, the order of arguments can make a difference, you need to add the arguments before any -jar mycooljar.jar
    – bmurauer
    Oct 2, 2017 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.