| 目次 | 前の項目 | 次の項目 | Java 2D API |
Font.deriveFont メソッドを使うと、既存の Font オブジェクトから、属性の異なる新しい Font オブジェクトを生成できます。 よく使われるのは、既存のフォントを変形して新しい派生フォントを作成する方法です。 この手順は次のとおりです。
この方法によって、独自のサイズのフォントや既存フォントの歪んだ種類を簡単に作成できます。次のコードでは、AffineTransform を適用し、フォント Helvetica の歪んだ種類を作成します。 次に、新しい派生フォントを使って文字列をレンダリングします。
// Create a transformation for the font.
AffineTransform fontAT = new AffineTransform();
fontAT.setToShear(-1.2, 0.0);
// Create a Font Object.
Font theFont = new Font("Helvetica", Font.PLAIN, 1);
// Derive a new font using the shear transform
theDerivedFont = theFont.deriveFont(fontAT);
// Add the derived font to the Graphics2D context
g2.setFont(theDerivedFont);
// Render a string using the derived font
g2.drawString("Java", 0.0f, 0.0f);