目次 | 前の項目 | 次の項目 Java 2D API


4.6 フォント派生の作成

Font.deriveFont メソッドを使うと、既存の Font オブジェクトから、属性の異なる新しい Font オブジェクトを作成できます。よく使われるのは、既存のフォントを変形して新しい派生フォントを作成する方法です。この手順は次のとおりです。

  1. Font オブジェクトを作成する
  2. Font に適用する AffineTransform を作成する
  3. Font.deriveFont を呼び出し、AffineTransform を渡す
この方法によって、独自のサイズのフォントや既存のフォントにスキューのかかったバージョンを簡単に作成できます。

次のコードでは、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);



目次 | 前の項目 | 次の項目
Copyright © 1997-1998 Sun Microsystems, Inc. All Rights Reserved.