drawString with Chinese Characters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vaskarbasak
    New Member
    • Feb 2008
    • 27

    drawString with Chinese Characters

    Hi all,
    I am trying to display a Chinese string (UTF-8) with
    Graphics2D.draw String() but nothing is displayed.
    here i paste my code

    package com;

    import java.awt.*;
    import java.awt.image. *;
    import java.io.*;
    import javax.imageio.* ;

    public class StringImage {

    public void drawImages(Stri ng data) {
    try {
    byte[] utf8 = data.getBytes(" UTF-8");
    data = new String(utf8);
    BufferedImage bufferedImage = new BufferedImage(2 00, 200,
    BufferedImage.T YPE_INT_RGB);
    Graphics2D g = bufferedImage.c reateGraphics() ;
    g .setColor( Color.WHITE );
    g .fillRect(0,0,2 00,200);
    g.setColor(Colo r.black);
    g.drawString(da ta, 20, 20);
    g.dispose();
    ImageIO.write(b ufferedImage, "JPG", new File("test2.jpg "));
    } catch (Exception e) {
    e.printStackTra ce();
    }
    }

    public static void main(String args[]) {
    new StringImage().d rawImages("埔里鎮育 英街175巷12弄 7號");
    }

    }

    Please help me.

    Thanks.
    vaskar
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    It works fine for me; btw, you can remove the following two lines because they
    don't do anything, i.e. they convert a String to a String:

    [code=java]
    byte[] utf8 = data.getBytes(" UTF-8");
    data = new String(utf8);
    [/code]

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      By the way, it's always a good idea to check the return value of ImageIO.write to see if it was successful.

      [CODE=Java]boolean success = ImageIO.write(b ufferedImage, "jpeg", file);[/CODE]

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        You will want to select a font, one that has the glyphs you need.

        [CODE=Java]g.setFont(font) ;[/CODE]

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by BigDaddyLH
          You will want to select a font, one that has the glyphs you need.

          [CODE=Java]g.setFont(font) ;[/CODE]
          My installation is a default Java SE 1.6 with a Dialog font family used and is
          perfectly well capable of running (and displaying) those foreign characters
          (on an ordinary vista laptop, US locale).

          kind regards,

          Jos

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by JosAH
            My installation is a default Java SE 1.6 with a Dialog font family used and is
            perfectly well capable of running (and displaying) those foreign characters
            (on an ordinary vista laptop, US locale).

            kind regards,

            Jos
            Call me paranoid, but I'm having doubts about the OP's setup. To test if a font can display a string, they can use Font's canDisplayUpTo.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by BigDaddyLH
              Call me paranoid, but I'm having doubts about the OP's setup. To test if a font can display a string, they can use Font's canDisplayUpTo.
              Same here; if I recall correctly there are only two Java SE downloads and I just
              tried it on an CJK installation: no problem there either. It could be a funny
              default bytes to chars conversion though (see my first reply about those two lines).

              kind regards,

              Jos

              Comment

              Working...