1. Cause
JDK needs to read the font list from the font configuration file. The default font list does not contain any Chinese font.
2. Solution
Add a Chinese font to the JDK font configuration file
3. Procedure
3.1. Copy the font file to the JDK font directory.
CD/usr/local/Java/JRE/lib/Fonts
CP/usr/share/fonts/zh_cn/TrueType/gbsn00l. TTF. |
|
3.2. Generate a font list file
3.3. view the newly generated fonts. dir file and view the section about gbsn00l. TTF.
3.4. It should be in the last line of the file
| -Arphic Technology Co.-ar PL sungtil GB-Medium-r-normal -- 0-0-0-0-p-0-ascii-0 |
|
3.5. Return to the previous directory and add gbsn00lp. TTF to font. properties.
CD ..
VI font. Properties |
|
3.6. Add method:
3.6.1 first, copy the line of text shown in section 3.4 and copy
Change 0-0-0-0-p-0 to *-% d-*-p -*
The purpose is to scale the font proportionally, which is now:
| -Arphic Technology Co.-ar PL sungtil GB-Medium-r-normal -- *-% d-*-p-*-ascii-0 |
|
3.6.2 the font. properties file already contains 0 and 1 fonts. For example, if the first line is serif.0 and the second line is serif.1, you need to add
| Serif.2 =-arphic Technology Co.-ar PL sungtil GB-Medium-r-normal -- *-% d-*-p-*-ascii-0 |
|
3.6.3 add xxxx.2 under each xxxx.1 until the # Missing glyph character of the file.
3.6.4 after the instance is added, save the disk and exit.
3.7. Add the environment variable java_fonts
| VI/etc/profile
Java_fonts = $ java_home/JRE/lib/Fonts |
|
3.8. Log On again to make the environment variable take effect
4. Test
| Testchsgraphic. Java (jdk1.3) |
ImportJava. AWT .*;ImportJava. AWT. image .*; ImportJava. Io .*;ImportJava. util .*; ImportCom.sun.image.codec.jpeg .*; Public ClassTestchsgraphic {Public StaticVoidMain(String [] ARGs)ThrowsException {system.Setproperty("Java. AWT. Headless", "true"); bufferedimage Bi =New Bufferedimage(200,100, bufferedimage. type_int_rgb); graphics G = Bi.Getgraphics(); String S = "Chinese test ";// Note! The specific transcoding may be different!G.Drawstring(New String(S.Getbytes(), "Gb2312"), 50, 50); bi.Flush(); Fileoutputstream Fos =New Fileoutputstream("Test.jpg"); your imageencoder must Ie = Your codec.Createjpegencoder(FOS); IE.Encode(BI); FOS.Close();}} |
| Testchsgraphic. Java (jdk1.4) |
ImportJava. AWT. graphics;ImportJava. AWT. bufferedimage;ImportJavax. ImageIO. ImageIO; ImportJava. Io .*; Public ClassTestchsgraphic {Public StaticVoidMain(String [] ARGs)ThrowsException {system.Setproperty("Java. AWT. Headless", "true"); bufferedimage Bi =New Bufferedimage(200,100, bufferedimage. type_int_rgb); graphics G = Bi.Getgraphics(); String S = "Chinese test ";// Note! The specific transcoding may be different!G.Drawstring(New String(S.Getbytes(), "Gb2312"), 50, 50); ImageIO.Write(Bi, "Jpeg ",New File("Test.jpg "));}} |