tags:

views:

226

answers:

5

Hi everyone,

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!

A: 

Add -Dfile.encoding=utf-8 to your command line script that's launching the application.

Bozho
Thanks everyone! Fantastic advice!
kakashi
+4  A: 

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.

Michael Borgwardt
Brilliant, thank you!
kakashi
+3  A: 

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

Chris
Awesome! Thank you!
kakashi
+1  A: 

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.

maerics
Thank you very much!
kakashi
A: 

Thanks for all the advice guys! That's a huge help.

To follow on, I was under the impression that UTF-8 doesn't cover all of the Japanese characters, and that there are still some characters (I assume these are fairly rare) which are only covered by JIS X 0213, but the only encoding mentioned above is UTF-8. Is it okay to use a font that is compliant with only UTF-8?

Sorry if I'm asking a dumb question here...

kakashi