Viewer.java



/** 表示のためのキャンバス */
package zoo;
import java.awt.*;

public class Viewer extends Canvas {

 /** Painterのオブジェクト */
    protected Painter painter=null;

 /** Painterのオブジェクトの取得 */
    public Painter getPainter() {
        return painter;
    }

 /** Painterのオブジェクトの設定 */
    public void setPainter( Painter p ) {
        painter = p;
    }

 /** コンストラクタ */
    public Viewer() {
        this( null, 128, 128 );
    }

    public Viewer( Painter p ) {
        this( p, 128, 128 );
    }

    public Viewer( int w, int h ) {
        this( null, w, h );
    }

    public Viewer( Painter p, int w, int h ) {
        super();
        setSize( w, h );
        setBackground( Color.white );
        painter = p;
    }

 /** 描画の内容を Painterクラスに行わせる */
    public void paint( Graphics g ) {
        if( painter != null )
            painter.paint( (Graphics2D)g );
    }

 /** 描画の内容を Painterクラスに行わせる */
    public void update( Graphics g ) {
        if( painter != null )
            painter.paint( (Graphics2D)g );
    }
}