Sign in to follow this  
Horatius83

[java] Unicode support and the great lie

Recommended Posts

Ok, I've invested quite a bit of time in learning Java because I have attempted on several occasions to program a Kanji memorization program in Win32 and both the compiler and OS have a hissy fit every time I make an attempt at it.'_T(" ") it's just that easy!" Anyway, all the books say how Java strings are 16 bit and natively support Unicode, oh how wonderful. So far I'm not seeing it, I get the same garbage I get in Win32, little boxes. I go to a number of websites, and they say I have to edit a file called font.properties in the lib directory. The problem, there are about eight of the things in there, all with slightly different endings. Truthfully I expected a little more out of this grand new "platform-agnostic" programming language, I shouldn't have to edit some obscure system file (which Sun provides a tutorial on editing the thing, but says they don't support it) I have Japanese support on WindowsXP and the IME, so that's not an issue, I've gone to a number of websites and they can get Kanji to display. My question: is there a way to get Java to support UTF-16, or is there some other language I can learn that doesn't require an elaborate voodoo ritual to get Unicode support.

Share this post


Link to post
Share on other sites
I'm not an expert on displaying Unicode in Java or anywhere else, but perhaps the standard font of the text field does not support Japanese characters? Have you tried loading a font which definately supports Kanji (like Mincho) and manually setting it for that specific text field? What about Graphics.drawString when you have selected one such font?

Share this post


Link to post
Share on other sites
<-- WHS. If Windows' fonts don't support the unicode characters you're trying to display Java doesn't have any chance either. Windows will fall back to displaying the box for missing glyphs.

Cas :)

Share this post


Link to post
Share on other sites

You might want to use encoding of the Strings, the String class has these methods, as well as the java.net package:


// URL encoding / decoding are examples
java.net.URLEncoder.encode(String s, String enc);
java.net.URLDecoder.decode(String s, String enc);


Where enc can be one of "UTF-16", "UTF-8", "ISO-8859-1", etc...

Quote:

If Windows' fonts don't support the unicode characters you're trying to display Java doesn't have any chance either. Windows will fall back to displaying the box for missing glyphs.


I guess he means his OS already supports japanese chars, but his Java app is not displaying them ;)

And in that case, just setting the default font of the text field component might be enough, really. Princec must have assumed you already did that, so the problem would still lie within the OS - if not with that little property you mentioned, I never had to use it so I know nothing about it.

Son Of Cain

Share this post


Link to post
Share on other sites
I've done this before (printed all UTF-16 characters) and IIRC the only problem I had was that java was defaulting to using a windows font that didn't have the characters. When I chose a better font, all was fine. Indeed, ISTR that on Windows2k it actually already had a default font with them in, and I had to do nothing.

Two things to check with:
1. Find the code to change font in java by passing in a fornt name, find the library calls to list all system proprietary fonts (not just the java-common ones!), and try each of them in turn

2. Use charmap (in programs > accessories) and go through your fonts to find which ones look identical to the one that your java is actually printing on screen; I suspect you'll find one, and that will turn out to be the one java is using at the moment

Share this post


Link to post
Share on other sites
try:
1."-encoding UTF-8" for javac
2.save java files in UTF-8 encoding
then:

public void paint(Graphics g)
{
super.paint(g);
g.setFont(new Font("MS Gothic", Font.PLAIN, 15));
g.drawString("some japanese goes here", 50, 50);

}


thats how I put some japanese on screen with netbeans ide

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And if you're reading unicode data, you might need to specify the encoding, e.g. "UTF-8"
new InputStreamReader(new FileInputStream("filename"), "UTF-8")

Share this post


Link to post
Share on other sites
I've had a problem with that before, just getting those dumb boxes instead of characters. I believe it's just a simple font issue and the characters not being supported by the font. The same code I used in Windows showed everything on my Mac just fine, english characters, japanese characters, etc..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ye... you just need a font which actually contains those characters (and you need to use that font obviously). And that's it.

If the application should run on other machines too, you'll need to put those chars into textures/images (use sheets). Be sure to pick a suited image fromat (like 8bit png [google for pngquant]). For 2400 chars (about 50x50 pixels each) I need about 330kb.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this