ITコンサルの日常

ITコンサル会社に勤務する普通のITエンジニアの日常です。

Graphics2D#drawStringせずに、文字列を描画した際の幅と高さをピクセル単位で取得する。

概要

下のサンプルは、
"2009/11/11"という文字列を、DialogのPLAINフォントで描画した場合に、
どのくらいの幅と高さが必要か、ピクセル単位で表示するものです。
1ptから200ptまでループして、その変化を見ています。

ソース


結果

Font size =   1(pt), Width =    8, Height =    2
Font size =   2(pt), Width =   10, Height =    3
Font size =   3(pt), Width =   18, Height =    4
Font size =   4(pt), Width =   18, Height =    5
Font size =   5(pt), Width =   26, Height =    7
Font size =   6(pt), Width =   28, Height =    8
Font size =   7(pt), Width =   36, Height =    9
Font size =   8(pt), Width =   36, Height =   10
Font size =   9(pt), Width =   46, Height =   12
Font size =  10(pt), Width =   54, Height =   14
...

つまり、"2009/11/11"を、DialogのPLAINフォント10ptで描画した場合、
幅54pixel、高さ14pixel必要ってことです。