Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can display both Simplified and Traditional Chinese text in Java, so long as you have a font that includes both sets of characters.</p> <p>I wrote this brief program to demonstrate:</p> <pre><code>import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.util.ArrayList; import java.util.List; import javax.imageio.ImageIO; public class ChineseFonts { public static final int GAP = 35; public static final int FONTS_PER_LINE = 2; public ChineseFonts(String s) { Rectangle rect = new Rectangle(0, 0, 1024, 768); BufferedImage bufferedImage = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR); Graphics graphics = bufferedImage.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(rect.x, rect.y, rect.width, rect.height); graphics.setColor(Color.BLUE); String title = "Chinese Fonts on " + System.getProperty("os.name") + ", version " + System.getProperty("os.version"); int fontY = 30; printString(title, graphics, 0, fontY, new Font(Font.SERIF, Font.BOLD | Font.ITALIC, 28), false); fontY += GAP + 10; int counter = 0; for (String fontName : new String[]{Font.MONOSPACED, Font.SANS_SERIF, Font.SERIF}) { Font font = new Font(fontName, Font.PLAIN, 24); printString(s, graphics, counter++, fontY, font, true); if (counter % FONTS_PER_LINE == 0) fontY += GAP; } Font[] localFonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts(); List&lt;Font&gt; chineseFonts = new ArrayList&lt;Font&gt;(); String simplifiedAndTraditionalChinese = "????"; for (int j = 0; j &lt; localFonts.length; j++) { if (localFonts[j].canDisplayUpTo(simplifiedAndTraditionalChinese) == -1) { chineseFonts.add(localFonts[j].deriveFont(24F)); } } for (Font font : chineseFonts) { printString(s, graphics, counter++, fontY, font, true); if (counter % FONTS_PER_LINE == 0) fontY += GAP; } graphics.dispose(); try { ImageIO.write(bufferedImage, "png", new File("chineseFonts.png")); } catch (Exception e) { // ignored } } private void printString(String s, Graphics graphics, int counter, int y, Font font, boolean showFontDetails) { graphics.setFont(font); if (showFontDetails) s = font.getFamily() + " " + s; graphics.drawString(s, 20 + (counter % FONTS_PER_LINE) * 510, y); if (showFontDetails) System.out.println("Printing " + s + " using " + font.getName() + ", which is " + font.getFontName() + " in family " + font.getFamily()); } public static void main(String args[]) { new ChineseFonts("S: 漢字 T: 汉字"); } } </code></pre> <p>The 2 simplified and 2 traditional Chinese characters used are from the wikipedia page on Chinese Characters. When you run it, all the fonts used are printed to stdout, and the image is output with the font names and the Chinese characters.</p> <p>Here are the 3 logical fonts and the first physical font that worked:</p> <ul> <li>Monospaced </li> <li>SansSerif </li> <li>Serif </li> <li>Arial Unicode MS S</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload