はい、両方の文字セットを含むフォントを使用している限り、簡体字中国語と繁体字中国語の両方のテキストをJavaで表示できます。
は私が実証するために、この簡単なプログラムを書いた:
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<Font> chineseFonts = new ArrayList<Font>();
String simplifiedAndTraditionalChinese = "????";
for (int j = 0; j < 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: 汉字");
}
}
使用簡素化2及び2つの伝統的な中国の文字が漢字にWikipediaのページからです。実行すると、使用されているすべてのフォントがstdoutに出力され、イメージはフォント名と漢字で出力されます。ここで
3つの論理フォントと働いていた最初の物理フォントです:
- は等幅
- サンセリフ
- セリフ
- はArial UnicodeのMS S
ます任意のユースケース従来の&簡体字中国語のテキストを同じコンポーネントで表示する必要がありますか? – belgther
私は、ユーザーがアプリケーションを再起動せずにオンザフライで言語を切り替えるようにしたいと考えています。また、リスト内の使用可能な言語の名前はネイティブスクリプトで書かれているので、それらも適切に表示します。 – ThVortex
あなたの場合、私はあなたのテキストを描画するためのフォントとテキストを設定することをお勧めします。まず1つのフォントでレンダリングし、次にもう一方のフォントでレンダリングします。それは働くだろうか? – belgther