通过自定义字体逐字符绘制字符串值
/** * 时代Java公众号 提 供 **/ import java.awt.*; import java.io.*; import java.util.*; public class StreamConverter { static void writeOutput(String str) { try { FileOutputStream fos = new FileOutputStream("test.txt"); Writer out = new OutputStreamWriter(fos, "UTF8"); out.write(str); out.close(); } catch (IOException e) {/*来自 时代Java公众号 - nowjava.com*/ e.printStackTrace(); } } static String readInput() { StringBuffer buffer = new StringBuffer(); try { FileInputStream fis = new FileInputStream("test.txt"); InputStreamReader isr = new InputStreamReader(fis, "UTF8"); Reader in = new BufferedReader(isr); int ch; while ((ch = in.read()) > -1) { buffer.append((char) ch); } in.close(); return buffer.toString(); } catch (IOException e) { e.printStackTrace(); return null; } } public static void main(String[] args) { String jaString = new String("\u65e5\u672c\u8a9e\u6587\u5b57\u5217"); writeOutput(jaString); String inputString = readInput(); String displayString = jaString + " " + inputString; new ShowString(displayString, "Conversion Demo"); } } class ShowString extends Frame { FontMetrics fontM; String outString; ShowString(String target, String title) { setTitle(title); outString = target; Font font = new Font("Monospaced", Font.PLAIN, 36); fontM = getFontMetrics(font); setFont(font); int size = 0; for (int i = 0; i < outString.length(); i++) { size += fontM.charWidth(outString.charAt(i)); } size += 24; setSize(size, fontM.getHeight() + 60); setLocation(getSize().width / 2, getSize().height / 2); setVisible(true); } public void paint(Graphics g) { Insets insets = getInsets(); int x = insets.left; int y = insets.top; g.drawString(outString, x + 6, y + fontM.getAscent() + 14); } }