Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Japanese fonts in a Java GUI

Is it possible to write a GUI in Java that would display Japanese fonts correctly regardless of the language settings of the OS it's being run on?

I'd like to write a program which is able to do this but I'm not sure how to start going about this. Any advice would be a huge help!

like image 241
kakashi Avatar asked Apr 21 '10 08:04

kakashi


3 Answers

The language settings aren't the problem here. Fonts are.

Java certainly can handle Japanese text, no matter what the system settings are, if the developer doesn't fall into the trap of depending on the platform default encoding by using things like FileReader, or new String(byte[]).

But to actually display the Japanese text, you need a font that has Japanese characters, and fonts are not part of Java. Depending on the OS, Japanese-capable fonts may be part of the default installation, or the user may be prompted to install them as needed, or they may have to be installed manually.

like image 179
Michael Borgwardt Avatar answered Nov 20 '22 06:11

Michael Borgwardt


Java can easily display Japanese regardless of if the OS has the fonts installed or not, but this is only for Swing applications. Anything using the console window requires fonts installed in the OS.

Steps:

1) Download one of the truetype fonts from here : http://www.wazu.jp/gallery/Fonts_Japanese2.html

2) Use the following code to allow your swing clients to use your fonts:

InputStream fontStream = getClass().getResourceAsStream("/locationoffontonclasspath/myfontname.ttf");
Font japaneseEnabledFont = null;
boolean japaneseDisplayEnabled = false;
try {
    japaneseEnabledFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
    GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(japaneseEnabledFont);
    japaneseDisplayEnabled = true;
} catch (Exception e) {
    // handle exceptions here
} finally {
    if (fontStream != null) {
        try {fontStream.close();} catch (Exception e1) {}
    }
}

if (japaneseDisplayEnabled) {
   .....
}

Also, if you wish to use Japanese literals in your sourcecode you have to compile with -Dfile.encoding=utf-8. If using an IDE to compile then you can change the settings on the following screen (right click the project and select properties to get this window): screenshot

More information is available at this page

like image 23
Chris Avatar answered Nov 20 '22 07:11

Chris


You can package a TrueType font with your program and load and use it in Swing components like so:

// Create a label in Japanese.
String message = "かつ、尊厳と権利とについて平等である。";
JLabel label = new JLabel(message);

// Load the TrueType font with Japanese characters and apply it.
File file = new File("msmincho.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
font = font.deriveFont(Font.PLAIN, 14f);
label.setFont(font);

Be careful to use the correct charset. If compiling Japanese characters in the code you should compile with javac -encoding utf-8 Foo.java. Also be sure to use the charset explicitly when using Readers.

like image 3
maerics Avatar answered Nov 20 '22 07:11

maerics