4

I am developing a Java swing Application with a Synth look and feel. The application has a background image that contains some text, and I need some 'active' text (text in a JLabel) above this background layer. However, the font I have chosen (Verdana), renders terribly in Swing for some reason, and as such, the font of the active text does not match the font of the background. Does anyone know of a way to make the text a little smoother-looking in Swing? I have found similar problems here and here, but I'm unsure as to what effect AA has on the rest of the applications images, or whether to use AWT desktop Properties or the -Dswing.aatext=true flag. Any tips would be much appreciated. Thanks!

EDIT: I have tested this in OSX, as well as Windows 7, both exhibit the same effect.

Community
  • 1
  • 1
SuperTron
  • 4,203
  • 6
  • 35
  • 62
  • 2
    don't do custom text drawing, instead configure a component (f.i. JLabel) with the text and render the label on top of the graphics – kleopatra Dec 06 '11 at 16:09
  • Sorry I shoulda been more specific, the text in the JLabels isnt rendering nicely. – SuperTron Dec 06 '11 at 16:13
  • @camickr - we disagree on that, as we know :-) Using the desktop properties is incredibly difficult without any guarantee to get it right everywhere. The "best" result is what the laf can do, deeply hidden inside the sun packages, that's why I recommend to _always_ use a JLabel ... – kleopatra Dec 06 '11 at 16:14
  • hmmm ... the JLabel is looking bad? That's strange, haven't seen much of aliasing problems since 6u10. Time for an sscce. – kleopatra Dec 06 '11 at 16:15
  • 2
    Post your [sscce](http://sscce.org/) and a picture of what you see for comparison, [e.g.](http://stackoverflow.com/questions/2018102/java-font-rendering/2018197#2018197) – trashgod Dec 06 '11 at 16:20
  • Ok, thanks for taking a look, I will post a SSCCE later tonight. – SuperTron Dec 06 '11 at 16:27
  • Well, it seems I can't reproduce the problem any more... Guess it's something hidden away in my application somewhere. I will update if I find anything else, thanks again. – SuperTron Dec 07 '11 at 00:51

2 Answers2

3

This is a problem with Swing itself I believe. Look at the related questions.

Using FontForge to disabling hinting can help in my experience.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • 1
    See the problem with that is that I can't go through that process on every users computer :O – SuperTron Dec 06 '11 at 19:38
  • SWT does a much better job of font rendering if you can switch. Otherwise maybe there are font rendering libraries you can find. – Garrett Hall Dec 06 '11 at 19:44
  • Linux' hinting phobia :-) curious: what happens with setting awt.desktophints (as described http://java.sun.com/javase/6/docs/api/java/awt/doc-files/DesktopProperties.html) to off? – kleopatra Dec 07 '11 at 08:50
0

I was able to find a solution to this problem. The main idea is to activate antialiasing, which must be done for both Swing and AWT. These properties are: swing.aatext and awt.useSystemAAFontSettings, and must be activated at the operating system level.

One way to do this is to use a shell batch program to do the settings for you, but I very much prefer to have a single executable when possible.

So, the solution I got to was to use System.setProperty(String key, String value). swing.aatext must be set to true, while awt.useSystemAAFontSettings must be set to on. Of course, this must be done as soon as possible when booting your app.

Follows an example of a typical Swing app:

public class App {
    public static void main(String[] args)
    {
        JFrame mainWindow = null;

        try {
            System.setProperty( "swing.aatext", "true" );
            System.setProperty( "awt.useSystemAAFontSettings", "on" );
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName()
            );
        } catch (Exception e) { }

        // Make sure we have nice window decorations.
        JFrame.setDefaultLookAndFeelDecorated( true );
        JDialog.setDefaultLookAndFeelDecorated( true );

        try {
            mainWindow = new MainWindow();
            mainWindow.setVisible( true );
        } catch(Exception e) {
            e.printStackTrace();
            System.err.println( e.getLocalizedMessage() );
        }
    }
}
Baltasarq
  • 12,014
  • 3
  • 38
  • 57