Font と FontMetrics (AWT)

import java.awt.*;
import java.awt.event.*;

public class Test {
	public static void main (String[] args)
	{
		Win win = new Win("Test Window");
	}
}

class Win extends Frame {
	Win (String name)
	{
					// Frameクラスのコンストラクタ(Windowのタイトルを引き渡す)
		super(name);
					// Windowの大きさ
		setSize(400, 100);
					// 背景色の変更
		setBackground(new Color(144, 238, 144));
					// ウィンドウを表示
		setVisible(true);
					// イベントアダプタ
		addWindowListener(new WinEnd());
	}

	/******************************/
	/* 上,左,下,右の余白の設定 */
	/******************************/
	public Insets getInsets()
	{
		return new Insets(35, 10, 10, 10);
	}

	/************/
	/* 終了処理 */
	/************/
	class WinEnd extends WindowAdapter
	{
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}

	/********/
	/* 描画 */
	/********/
	public void paint (Graphics g)
	{
		int x, y;
		String str = new String("昨日東京へ行って来ました");
		String str1;
		Font f1 = new Font("MS 明朝", Font.BOLD, 20);
		Font f2 = new Font("MS 明朝", Font.ITALIC, 12);
		FontMetrics fm1 = g.getFontMetrics(f1);
		FontMetrics fm2 = g.getFontMetrics(f2);
					// 塗りつぶした正方形(左上x,y,幅,高さ)
		g.setColor(Color.white);
		g.fill3DRect(20, 45, 350, 30, true);
					// 文字列(文字列,位置x,y)
		x = 25;
		y = 67;
		g.setFont(f1);
		g.setColor(Color.black);
		str1 = str.substring(0, 2);
		g.drawString(str1, x, y);

		x += fm1.stringWidth(str1);
		g.setFont(f2);
		g.setColor(Color.red);
		str1 = str.substring(2, 5);
		g.drawString(str1, x, y);

		x += fm2.stringWidth(str1);
		g.setFont(f1);
		g.setColor(Color.blue);
		str1 = str.substring(5);
		g.drawString(str1, x, y);
	}
}