//GraphicComponent.java

import java.awt.*;

abstract class GraphicComponent //extends Panel 
					   implements IGraphicComponent{

	//for double buffering
	protected Image bufferImg;
	protected Graphics gBuffer;
	
	//fixed image.
	protected Image fixedImg;
	protected Graphics gFixed;
	
	//iff true, this component has been translated.
	protected boolean translated = false;
	protected int deltaX, deltaY;
	
	//the image size for this visual component.
	protected Dimension imgSize;
//	protected /*final*/ int width, height;
	
//	protected AppletLogic appletLogic;
	
	//for double buffering, 
	//if this component is continuously changing -- we need to repaint this
	//component every time.
	protected boolean changing = true;
	
	//for event handling. 
	//if this component is changed caused by an event -- repaint this component. 
	protected boolean changed = true;

	//for the first time drawing.
	protected boolean firstTime = true;
	
	

	//the abstract super class constructor.
	public GraphicComponent(){
		this(/*0,0*/new Dimension(0,0));
	}
	
	public GraphicComponent(/*int width, int height*/Dimension imgSize){
		setImageSize(/*width, height*/imgSize);
	}
	
	
/*	//For creating the images.
	public void addNotify(){
		super.addNotify();
		
		bufferImg = createImage(imgSize.width, imgSize.height);
		gBuffer = bufferImg.getGraphics();
		
		fixedImg = createImage(imgSize.width, imgSize.height);
		gFixed = fixedImg.getGraphics();

		draw(gBuffer);
		draw(gFixed);
	}
*/
	
	//draw method.
	public /*abstract*/ void draw(Graphics g){
		if(this.firstTime) {
			if(this.fixedImg!=null)
				drawThisCompFixedImg();
			this.firstTime = false;
		}

		if(this.bufferImg == null)
			drawThisComponent(g);
		else {
			if(this.changed){
				this.gBuffer.drawImage(this.fixedImg, 0,0, null);
				drawThisComponent(this.gBuffer);
				
				if(!this.changing) this.setThisComponentChanged(false);
			}
			
			g.drawImage(this.bufferImg, 0, 0, null);
		}
	}
	
	//draw this component's fixed image.
	public void drawThisCompFixedImg(){};
	
	//abstract method to draw this component.
	public /*abstract*/ void drawThisComponent(Graphics g){};
	
	
	//to be overrided by the GraphicComposite class.
	public /*abstract*/ void addGraphicComponent(GraphicComponent gc){};

	//to be overrided by the GraphicComposite class.
	public /*abstract*/ void removeGraphicComponent(GraphicComponent gc){};

	//set up the traslation.
	public void translate(int deltaX, int deltaY){
		this.translated = true;	
		this.deltaX = deltaX;
		this.deltaY = deltaY;
	}
	
	//return true iff this component has been translated.
	public boolean isTranslated(){return this.translated;}
	
	//return the new original coordinates of this component
	//after it is translated.
	public Point getNewPosition(){
		return new Point(this.deltaX, this.deltaY);
	}

	//set the image size of this component.
	public void setImageSize(/*int width, int height*/Dimension imgSize){
		this.imgSize = imgSize;
	//	this.width = width;
	//	this.height = height;
	}
	
	public Dimension getImageSize(){
		return this.imgSize;
	}
	
/*	public int getImageWidth(){
		return width;
	}
	public int getImageHeight(){
		return height;
	}
*/
	
	public void setBufferImg(Image bufferImg){
		this.bufferImg = bufferImg;
//		this.gBuffer = this.bufferImg.getGraphics();
	}
	
	public void setFixedImg(Image fixedImg){
		this.fixedImg = fixedImg;
//		this.gFixed = this.fixedImg.getGraphics();
	}

	public void setBufGraphics(Graphics gBuffer){
		this.gBuffer = gBuffer;
	}
	
	public void setFxdGraphics(Graphics gFixed){
		this.gFixed = gFixed;
	}
	
//	public void constructImages(){}
/*		//create all the images 
		//for all the GraphicComponents(GraphicComposite).
		Enumeration enum = this.componentList.elements();
		Dimension imgSize;
		Image componentImg;
		
		while(enum.hasMoreElements()){
			currentElement = (GraphicComponent)enum.nextElement();
	
			imgSize = currentElement.getImageSize();
			componentImg = createImage(imgSize.width, imgSize.height);
			
			currentElement.setBufferImg(componentImg);
			currentElement.setFixedImg(componentImg);			

			currentElement.constructImages();
		}	
	}*/
	
	public void setThisComponentChanged(boolean changed){this.changed = changed;}
	public boolean isThisComponentChanged(){return changed;}	

	public void setThisComponentAlwaysChanging(boolean changing){
		this.changing = changing;
	}
	public boolean isThisComponentAlwaysChanging(){return this.changing;}
}