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


2.7 クリッピングパスの設定

クリッピングパスの定義は、次の手順で行います。

  1. レンダリングする領域を示す Shape を作成する
  2. Graphics2D.setClip を呼び出し、作成した Shape を Graphics2D コンテキストのクリッピングパスとして設定する
クリッピングパスの縮小は、次の手順で行います。

  1. 現在のクリッピングパスと共通部分を持つ Shape を作成する
  2. clip を呼び出し、クリッピングパスを、現在のクリッピングパスと新しい Shape の共通部分に変更する
次の例では、まず楕円形でクリッピングパスを作成し、次に clip を呼び出してクリッピングパスを変更しています。

public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// The width and height of the canvas
int w = getSize().width;
int h = getSize().height;
// Create an ellipse and use it as the clipping path
Ellipse2D e = new Ellipse2D.Float(w/4.0f,h/4.0f,
w/2.0f,h/2.0f);
g2.setClip(e);
// Fill the canvas. Only the area within the clip is rendered
g2.setColor(Color.cyan);
g2.fillRect(0,0,w,h);
// Change the clipping path, setting it to the intersection of
// the current clip and a new rectangle.
Rectangle r = new Rectangle(w/4+10,h/4+10,w/2-20,h/2-20);
g2.clip(r);
// Fill the canvas. Only the area within the new clip
// is rendered
g2.setColor(Color.magenta);
g2.fillRect(0,0,w,h);
}


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