473,387 Members | 1,476 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

drawString with Chinese Characters

Hi all,
I am trying to display a Chinese string (UTF-8) with
Graphics2D.drawString() 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(String data) {
try {
byte[] utf8 = data.getBytes("UTF-8");
data = new String(utf8);
BufferedImage bufferedImage = new BufferedImage(200, 200,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bufferedImage.createGraphics();
g .setColor( Color.WHITE );
g .fillRect(0,0,200,200);
g.setColor(Color.black);
g.drawString(data, 20, 20);
g.dispose();
ImageIO.write(bufferedImage, "JPG", new File("test2.jpg"));
} catch (Exception e) {
e.printStackTrace();
}
}

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

}

Please help me.

Thanks.
vaskar
Jun 10 '08 #1
6 6098
JosAH
11,448 Expert 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. byte[] utf8 = data.getBytes("UTF-8");
  2. data = new String(utf8);
  3.  
kind regards,

Jos
Jun 10 '08 #2
BigDaddyLH
1,216 Expert 1GB
By the way, it's always a good idea to check the return value of ImageIO.write to see if it was successful.

Expand|Select|Wrap|Line Numbers
  1. boolean success = ImageIO.write(bufferedImage, "jpeg", file);
Jun 10 '08 #3
BigDaddyLH
1,216 Expert 1GB
You will want to select a font, one that has the glyphs you need.

Expand|Select|Wrap|Line Numbers
  1. g.setFont(font);
Jun 10 '08 #4
JosAH
11,448 Expert 8TB
You will want to select a font, one that has the glyphs you need.

Expand|Select|Wrap|Line Numbers
  1. g.setFont(font);
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
Jun 10 '08 #5
BigDaddyLH
1,216 Expert 1GB
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.
Jun 10 '08 #6
JosAH
11,448 Expert 8TB
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
Jun 10 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jeff | last post by:
Having a problem displaying both german and chinese characters from a UTF-8 database on the same *.jsp page. Can make Chinese characters appear correctly by including the following lines: <%@...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
8
by: Agnes | last post by:
In my .net ,i need to generate an xml file , however, user may input a chinese character, Then , the xml will got something unknow characters. the following is my code, Does anyone know how to...
8
by: pabv | last post by:
Hello all, I am having a few issues with encoding to chinese characters and perhaps someone might be able to assist. At the moment I am only able to see chinese characters when displayed as...
4
by: K | last post by:
I've an XML file in UTF-8. It contains some chinese characters ( both simplified chinese and traditional chinese). In loading the XML file with MSXML parser, I used the below code to retrieve...
0
by: st.frey | last post by:
I've got a problem with importing chinese characters into a mysql-table and have read several mailings but didn't find a solution. i have a utf-8 text file that contains chinese characters. the...
5
by: Figmo | last post by:
I'm having a problem working with foreign characters (well....foreign to me anyway) I have a textbox control on a form. The font is set to MS Arial Unicode. If I use the Chinese input method...
2
by: Wassy | last post by:
Hi, i have a website which contains both chinese and english content which is stored in a database. Each record in the dB has an english and Chinese field. If a user enters a search string i have...
2
by: Flying Kite | last post by:
Hi All, I want to know how to print chinese characters on Zebra Printer, following code working fine with English string, but it's not working for Chinese string. It shows ASCII characters instead...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.