views:

67

answers:

2

alt text

Recently, I realize the Chinese Character displayed are rather ugly in my application.

I thought I should make them to "anti-alias". But, how can I do that in Java?

FYI, I didn't explicitly choose the font I want to use in my GUI application. I solely let the system decide their own during startup. I however, do explicitly set the locale, before show up the GUI.

Locale.setDefault(locale);

The system will always choose

javax.swing.plaf.FontUIResource[family=Tahoma,name=Tahoma,style=plain,size=11]

no matter I am in English or Chinese locale.

+1  A: 

Anti-aliasing considered harmful: http://www.joelonsoftware.com/articles/fog0000000041.html

The point is, that beauty of characters is not necessarily the user interface goal. It is not everything. What you should look for, is readability of text. When your Chinese characters look not smooth, it may be exactly what helps human eye's control loop to know that it is in focus and stop blaming the eye muscules for blurriness. Really, don't fall in this pitfal.

Pavel Radzivilovsky
OK. I manage to produce an anti-alias version. However, I did some survey by interacting with China community through forum by letting them comparing 2 version. It seems that they prefer the version without anti-alias.
Yan Cheng CHEOK
+1  A: 

Here's a method to read a truetype font from the classpath and register it with the graphics environment:

private static Font readFont(String name) {
    InputStream in = Fonts.class.getResourceAsStream(name + ".ttf");
    if (in == null) {
        throw new IllegalArgumentException(name);
    }
    try {
        Font retval = Font.createFont(Font.TRUETYPE_FONT, in);
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(retval);
        return retval;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

You can then use this font object to derive characters of different sizes, or you could try applying this font using Swing CSS. In this case, the value you would put in the "font-family" attribute is the value returned by Font.getName().

For example:

static {

    Font font = readFont("VeraMono");
    if (font != null) {
        font = font.deriveFont(14f);
    } else {
        throw new IllegalStateException();
    }

    MONOSPACED_TEXT_FONT = font;
    MONOSPACED_TEXT_FONT_STYLE = "font-family: " + font.getName() + "; font-size: 14pt; font-weight: normal;";

}
bgould