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)

raven
  • 18,004
  • 16
  • 81
  • 112
rcreswick
  • 16,483
  • 15
  • 59
  • 70

4 Answers4

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
rcreswick
  • 16,483
  • 15
  • 59
  • 70
  • this method does not work for Substance look and feel, do you have idea about this ? – Rodrigo Asensio Mar 09 '12 at 17:46
  • This doesn't work for `Graphics#paintString(String, int, int)` – Ky - Dec 11 '12 at 04:00
  • If you're working with the Graphics apis, this may help: http://docs.oracle.com/javase/tutorial/2d/text/renderinghints.html – rcreswick Mar 16 '14 at 20:16
  • 9
    Setting `awt.useSystemAAFontSettings` to `on` means *use anti-aliasing **without sub-pixel rendering*** which is most of the time (when using LCD display) **not** preferred. Instead use `lcd` value. See http://wiki.netbeans.org/FaqFontRendering – Piotr Dobrogost Nov 26 '14 at 14:30
  • 1
    The first setting alone works. That second setting does not need to be used. It's an undocumented, private setting, that has been removed from the JRE. See details: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6391267 – clemep Nov 19 '18 at 16:22
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

Luke Usherwood
  • 3,082
  • 1
  • 28
  • 35
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.

Al-Khwarizmi
  • 453
  • 5
  • 8
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.

anjanb
  • 12,999
  • 18
  • 77
  • 106
  • 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 07 '08 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 07 '08 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 07 '08 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 '14 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 02 '17 at 07:09