开发者

Beautiful (Anti-alias) Chinese Character Display

开发者 https://www.devze.com 2023-01-03 20:23 出处:网络
(source: google.com) Re开发者_如何学Pythoncently, I realize the Chinese Character displayed are rather ugly in my application.

Beautiful (Anti-alias) Chinese Character Display

(source: google.com)

Re开发者_如何学Pythoncently, 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.


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.


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;";

}
0

精彩评论

暂无评论...
验证码 换一张
取 消