import java.awt.Color;
import java.awt.Component;
import java.awt.Image;

import java.awt.image.ImageObserver;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;

class   Playfield
extends Raster
{
	public Raster rasters[];

    /**
     *  This constructor which takes no arguments
     *  allows for future extension.
     */
    public Playfield() 
	{
    }


    /**
     *  This constructor creates an uninitialized
     *  Raster Object of a given size (w x h).
     */
    public Playfield( int w, int h )
	{
		super( w, h );
    }


    /**
     *  This constructor creates an Raster initialized
     *  with the contents of an image.
     */
    public Playfield( Image img )
    {
		super( img );
    }


    /**
     *  Add a new raster
     */
    public void
	addRaster( Raster raster )
    {
		if ( rasters == null ) {
			rasters    = new Raster[1];
			rasters[0] = raster;
			return;
		}
		
		Raster tmp_rasters[] = new Raster[rasters.length+1];
		
		for ( int i=0; i<rasters.length; i++ ) {
			tmp_rasters[i] = rasters[i];
		}
		tmp_rasters[tmp_rasters.length-1] = raster;
		
		rasters = tmp_rasters;
    }


    /**
     *  Draw the rasters
     */
    public void
	draw()
    {
		if ( rasters == null ) {
			return;
		}
			
		for ( int i=0; i<rasters.length; i++ ) {
			if ( rasters[i] instanceof Sprite ) {
				((Sprite)rasters[i]).draw( this );
			}
			
			if ( rasters[i] instanceof Raster ) {
				Raster raster = rasters[i];
				int    rows   = raster.getHeight();
				int    cols   = raster.getWidth();

				for ( int row=0; row<rows; row++ ) {
					for ( int col=0; col<cols; row++ ) {
						pixel[row*width + col] = raster.getPixel( col, row );
					}
				}
			}
		}
    }


    /**
     *  Converts Rasters to Images
     */
    public Image 
	toImage( Component root )
	{
		System.out.println( "Width  = " + width );
		System.out.println( "Height = " + height );
		return root.createImage( new MemoryImageSource( width, 
	  													height, 
														pixel, 
														0, 
														width) );
    }
}
