TiledImageTest.java



/**
 * イメージをタイリングする実験
 */
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;

public class TiledImageTest extends Canvas {

  /** タイリングに使用する TexturePaint のオブジェクト */
    public TexturePaint texture;

  /** Ellipse2D のオブジェクト */
    public Ellipse2D.Double ellipse;

  /** コンストラクタ */
    public TiledImageTest() {
        super();
        setSize( 128, 256 );

        BufferedImage source
           = new BufferedImage( 2, 2, BufferedImage.TYPE_INT_RGB );
        WritableRaster raster = source.getRaster();
        DataBufferInt buffer 
           = (DataBufferInt)(raster.getDataBuffer());
        buffer.setElem( 0x0, 0x00ffff00 );
        buffer.setElem( 0x1, 0x0000a0a0 );
        buffer.setElem( 0x2, 0x0000a0a0 );
        buffer.setElem( 0x3, 0x00ffff00 );

        Rectangle2D.Double rect
             = new Rectangle2D.Double( 0.0, 0.0, 32.0, 32.0 );
        texture = new TexturePaint( source, rect );
        ellipse = new Ellipse2D.Double( 0.0, 0.0, 128.0, 256.0 );
    }

  /** 描画メソッド */

    public void paint( Graphics g ) {

       ((Graphics2D)g).setPaint( texture );
       ((Graphics2D)g).fill( ellipse );
    }

  /** 最初に呼び出されるメソッド */

    public static void main( String argv[] ) {

        Frame frame = new Frame( "Tiled Image Test" );
        TiledImageTest canvas = new TiledImageTest(); 
        frame.add( canvas, "Center" );
        frame.pack();
        frame.setVisible( true );
    }
}