import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import Raster;
import javax.swing.*;

public class VisuRaster extends Raster {

/** Not very useful for the momment... just for futur developpment */
	public VisuRaster()	{ 	
		super();
		backgroundColor = Color.white;
		lineColor = Color.black;
		fill(backgroundColor);
	}
	
	public VisuRaster(int width, int height, double zoom)	{	
		super(width, height);   
		this.zoom = zoom;
		backgroundColor = Color.white;
		lineColor = Color.black;
		
		fill(backgroundColor);
		
		GraphicsEnvironment local = GraphicsEnvironment.getLocalGraphicsEnvironment();
		GraphicsDevice screen = local.getDefaultScreenDevice();
		GraphicsConfiguration configuration = screen.getDefaultConfiguration();
		
		rWidth = (int)(width*zoom)+1;
		rHeight = (int)(height*zoom)+1;
 		imgBuf = configuration.createCompatibleImage(rWidth, rHeight);
 		
		bufContext = imgBuf.createGraphics();
		
		reset();
		
    }

	public VisuRaster(int width, int height, double zoom, Color background, Color line)	{	
		this( width,  height,  zoom);
		backgroundColor = background;
		lineColor = line;
		reset();
    }
    
    public void reset()	{
		
		bufContext.setPaint(backgroundColor);
		bufContext.fill(new Rectangle2D.Double(0.,0.,width*zoom-1,height*zoom-1));

		bufContext.setPaint(lineColor);


		int i,j;
		Color col;
		for ( i = 0; i <= width; i++)	{
			bufContext.draw(new Line2D.Double(i*zoom,0.,i*zoom,height*zoom));
		}
		

		 
		for ( j = 0; j <= height; j++)	{
			bufContext.draw(new Line2D.Double(0.,j*zoom,width*zoom,j*zoom));
		}


		for ( i = 0; i < width; i++)	{
			for ( j = 0; j < height; j++)	{
				col = getColor(i, j);
				
				//  How, Here is an optimisation  !!!!!!!
				
				if (!col.equals(backgroundColor))	{
					bufContext.setPaint(col);
					bufContext.fill(new Ellipse2D.Double(i*zoom+1,(height-(j+1))*zoom+1,zoom-1,zoom-1));
				}
			}
		}    	
    }
    
     /**
     *  Sets a pixel to a given value
     */
    public  boolean setPixel(int pix, int x, int y)
    {	return this.setColor(new Color(pix),x,y);
	}


    /**
     *  Sets a pixel to a given color
     */
    public  boolean setColor(Color c, int x, int y)
    {	super.setColor(c,x,y);
		bufContext.setPaint(c);
		bufContext.fill(new Ellipse2D.Double(x*zoom+1,(height-(y+1))*zoom+1,zoom-1,zoom-1));
        return true;
    }

    
    public void paint(Graphics2D g2)		{
	   	g2.drawImage(imgBuf,0,0,null);
    }
    
    public void exportResult()	{
    	JPanel jp = new JPanel();
    	Image result = toImage(jp);
    	bufContext.setPaint(backgroundColor);
		bufContext.fill(new Rectangle2D.Double(0.,0.,width*zoom-1,height*zoom-1));
		bufContext.drawImage(result, 0, 0, null);
	}
    
    public int getRWidth()	{
    	return rWidth;
    }

    public int getRHeight()	{
    	return rHeight;
    }

    public double getZoom()	{
    	return zoom;
    }
    
    
private Color			backgroundColor,lineColor;
private double   		zoom;
private BufferedImage	imgBuf;
private Graphics2D 		bufContext;
private int				rWidth,rHeight;
}
