import java.awt.*;
//import gjt.Stopwatch;
//import gjt.StopwatchClient;



public class BarChart extends Bargauge implements StopwatchClient{


	Image m_image;
	Graphics m_g;
	Rectangle m_dimImage = null;
	Stopwatch m_timer=new Stopwatch(this);
	int m_tickCount=0;

	public BarChart(Color fillColor){
		super(fillColor);
		((Thread)m_timer).start();
	}

	protected void ResizeImage(){
		Rectangle dim = border.getInnerBounds();
		int nWidth = dim.width;
		int nHeight = dim.height;
		if (nWidth<2 || nHeight<2) return;

		if (m_dimImage != null &&
			m_dimImage.width == nWidth &&
			m_dimImage.height == nHeight){

			return;
		}

		m_dimImage = new Rectangle(dim.x,dim.y,nWidth, nHeight);
		m_image = createImage(nWidth, nHeight);
		m_g = m_image.getGraphics();
	}


	public void paint(Graphics g){
		border.raise();
		border.paint();
		fill();
		g.drawImage(m_image,m_dimImage.x,m_dimImage.y,null);
	}


	public void fill(){
		if (m_image==null) ResizeImage();

		int x1=m_dimImage.width;
		int y1=m_dimImage.height;
		if (percentFill >=0){
			if (x1>=y1){
				m_g.copyArea(0,0,x1,y1,-1,0);
				m_g.setColor(getBackground());
				m_g.drawLine(x1-1,0,x1-1,y1);
				m_g.setColor(fillColor);
				m_g.drawLine(x1-1,y1,x1-1,y1-y1*(int)percentFill/100);
			}
			else{
				m_g.copyArea(0,0,x1,y1,0,1);
				m_g.setColor(getBackground());
				m_g.drawLine(0,0,x1,0);
				m_g.setColor(fillColor);
				m_g.drawLine(0,0,x1*(int)percentFill/100,0);
			}
		}
	}
			



	public void tick(){
		m_tickCount = (++m_tickCount)%20;
		if (m_tickCount == 0) repaint();

	}


   	public void reshape(int x, int y, int w, int h){
   		super.reshape(x,y,w,h);
   		ResizeImage();
   	}


	public void start(){
		m_timer=new Stopwatch(this);
		((Thread)m_timer).start();
	}

	public void stop(){
		((Thread)m_timer).stop();
		m_timer=null;
	}


}
