473,699 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

drawString with Chinese Characters

27 New Member
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
Jun 10 '08 #1
6 6186
JosAH
11,448 Recognized Expert MVP
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 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
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 Recognized Expert Top Contributor
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 Recognized Expert MVP
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
4761
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: <%@ page pageEncoding="UTF-8" %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6
22261
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 header, I found "zh" is not standard. I found this line in apache2's default httpd.conf # Simplified Chinese (zh-CN) AddLanguage zh-CN .zh-cn
8
3482
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 solve it ?? Private Sub Init() With AMSXML ..Formatting = Formatting.Indented ..Indentation = 4 ..IndentChar = " " ..WriteStartDocument()
8
11979
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 part of a datagrid. When an input textbox is displayed it does not display chinese characters, but rather the unicode characters stored in the mssql 2000 server backend.
4
6896
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 the data in a node. The CString was then display in CListCtrl. For the traditional chinese characters, they were shown correctly, but for simplified characters, I encounted many "?", but some characters were correct.
0
3704
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 table where i want to import the data using "load data local infile" has collation utf8_unicode_ci. but after the import is done, the chinese characters are converted into so strange characters. to show the chinese symbols in a php-script i use...
5
8551
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 I can type characters into this box no problem. They display correctly. I can also copy and paste Chinese characters from web sites into this text box and they display no problem. Here is my problem. If I put some Chinese characters in the...
2
6291
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 to be able to detect which characters are latin based and which are chinese ideographs. eg) a user may enter "hello ÐÂÎÅÍø world" this is because many Chinese search phrases (especially those involved with technology may include English words...
2
6714
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 of Chinese characters, on my machine I installed language pack and currently set language as Chinese PRC but still it's not working. Option Explicit Private Type DOCINFO pDocName As String pOutputFile As String pDatatype As String
0
8685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.