Re: drawString with special Unicode characters to Graphics object

Discussion in 'Java' started by Chris Smith, May 31, 2004.

  1. Chris Smith

    Chris Smith Guest

    Wolfgang wrote:
    > Now I want to draw the Unicode string onto a Graphics object, like
    >
    > g2D_of_Image.drawString(unicodeString, x, y);
    >
    > This works, except that the "ö" shows up "funny", it looks like
    > "ö" (rather than the "o" with two dots above it)
    > (Other special characters similarly show up as funny stuff);
    >
    > So what do you do to make Unicode (UTF-8) show up as the proper
    > special character on the Graphics object?


    It looks very much like you've actually got two characters in your Java
    String object to represent this one character. That's definitely wrong.
    You should have one character (which apparently should be \u00F6).
    Where that two-byte sequence comes from is a different issue, as
    discussed below. You can pretty safely drop your concern about the
    drawString method, though, because it's apparent that your String is
    incorrect before you get there. A problem in drawString would not cause
    anything to draw two characters instead of one.

    Your last bit doesn't make a lot of sense. When you call drawString,
    you give it a Java String (which is a sequence of characters), not a
    sequence of bytes. A Java String doesn't have an encoding like UTF-8,
    because it's not encoded. So "Unicode (UTF-8)" is meaningless. All
    Java Strings are Unicode, and no Java Strings are UTF-8.

    > (I'm not actually sure, but the unicode may get mangled on the way
    > from the database into my string "unicodeString", so perhaps the
    > question is bigger, how do you write a Unicode-enabled java code :-o)


    That seems quite likely. This is sometimes tricky to get right,
    depending on your JDBC driver. Some databases can be built with
    multiple different encodings, and some JDBC drivers can get confused
    unless you help them along.

    Care to share which DBMS this is, and any special build-time or run-time
    configuration? The exact definition of the table and the Java code you
    use to retrieve the value would be good, too.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, May 31, 2004
    #1
    1. Advertising

  2. Chris Smith

    Wolfgang Guest

    The database I use is mySQL. I found out that mySQL prior to version
    4.1.0-alpha does not support Unicode, so there is the first problem. I
    then switched to mySQL version 4.1.0-alpha last night and there is
    some improvement in that I can now see special characters IF I query
    the database with an graphical client (I use iSQL-Viewer 2.1.1 from
    http://prdownloads.sourceforge.net/isql; from isqlviewer.com). But
    for this a query has to be formated in the following contorted way:

    SELECT full_name FROM gm WHERE full_name LIKE
    CONVERT(_latin1'Haussömmern' USING utf8);

    (this looks for the place name string "Haussömmern")

    Given this query, the graphical SQL client actually returns and
    displays the string "Haussömmern", incl. the correct special character
    "ö".

    If send the exact same query using my Java program, which extracts and
    draws the place name on a map, my graphic shows "Haussömmern". My
    Java program does things like this:

    ResultSet rs = stmt.executeQuery("SELECT full_name FROM gm WHERE
    full_name LIKE CONVERT(_latin1'Haussömmern' USING utf8);");
    String unicodeString = rs.getString("full_name");

    g2D_of_Image.drawString(unicodeString, x, y);

    I'm not surprised that, the Unicode for "ö" shows up as two
    characters "ö" instead of just one whatever character, because the
    Unicode consists of the two bytes C3 B6 (in fact, if you search Google
    for "ö", the URL that appears is contains the very same two
    characters, as in
    http://www.google.com/webhp?hl=en&tab=iw&q="ö").

    I'm just just frustrated that I can't make it show up as the "ö" it's
    supposed to represent.

    Thanks,
    Wolfgang

    Chris Smith <> wrote:
    >Wolfgang wrote:
    >> Now I want to draw the Unicode string onto a Graphics object, like
    >>=20
    >> g2D_of_Image.drawString(unicodeString, x, y);
    >>=20
    >> This works, except that the "=F6" shows up "funny", it looks like=20
    >> "=C3=B6" (rather than the "o" with two dots above it)=20
    >> (Other special characters similarly show up as funny stuff);=20
    >>=20
    >> So what do you do to make Unicode (UTF-8) show up as the proper
    >> special character on the Graphics object?

    >
    >It looks very much like you've actually got two characters in your Java=20
    >String object to represent this one character. That's definitely wrong. =
    >=20
    >You should have one character (which apparently should be \u00F6). =20
    >Where that two-byte sequence comes from is a different issue, as=20
    >discussed below. You can pretty safely drop your concern about the=20
    >drawString method, though, because it's apparent that your String is=20
    >incorrect before you get there. A problem in drawString would not cause=20
    >anything to draw two characters instead of one.
    >
    >Your last bit doesn't make a lot of sense. When you call drawString,=20
    >you give it a Java String (which is a sequence of characters), not a=20
    >sequence of bytes. A Java String doesn't have an encoding like UTF-8,=20
    >because it's not encoded. So "Unicode (UTF-8)" is meaningless. All=20
    >Java Strings are Unicode, and no Java Strings are UTF-8.
    >
    >> (I'm not actually sure, but the unicode may get mangled on the way
    >> from the database into my string "unicodeString", so perhaps the
    >> question is bigger, how do you write a Unicode-enabled java code :-o)

    >
    >That seems quite likely. This is sometimes tricky to get right,=20
    >depending on your JDBC driver. Some databases can be built with=20
    >multiple different encodings, and some JDBC drivers can get confused=20
    >unless you help them along.
    >
    >Care to share which DBMS this is, and any special build-time or run-time=20
    >configuration? The exact definition of the table and the Java code you=20
    >use to retrieve the value would be good, too.
    >
    >--=20
    >www.designacourse.com
    >The Easiest Way to Train Anyone... Anywhere.
    >
    >Chris Smith - Lead Software Developer/Technical Trainer
    >MindIQ Corporation
    Wolfgang, May 31, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonah Olsson

    Special characters with DrawString?!

    Jonah Olsson, Oct 1, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,365
    Dmitriy Lapshin [C# / .NET MVP]
    Oct 3, 2003
  2. Stefan Mueller
    Replies:
    3
    Views:
    33,001
    Stefan Mueller
    Jul 23, 2006
  3. Replies:
    2
    Views:
    1,088
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,651
    rvino
    Aug 14, 2007
  5. Grzegorz ¦liwiñski
    Replies:
    2
    Views:
    958
    Grzegorz ¦liwiñski
    Jan 19, 2011
Loading...

Share This Page