/*File: histogramCanvas.java */
//Now we try to create a more general histogram class
import java.awt.*;  
import java.applet.*;

/**
 * Draws a histogram given appropriate information
 * @author Charles Stanton
 * @version 1.1 January 29, 1997
 */
class histogramCanvas extends Canvas {
	int[] count;  //array of counts for each bin
	int bins;     //number of bins 
	Applet applet;
	double[] theoreticalCount;  // The theoretical distribution counts
	boolean drawTheoretical;  // Whether to plot the theoretical histogram
				 //  Which must be supplied
	int vPixels=4;  // the number of pixels to step the histogram

/**
 *    Initialize the histogram, no theoretical lines drawn
 * @param count is  an array of counts of the objects in the bins
 * @param bins  tells how many bins to make
 * @param applet is the applet calling the class
 */
	public histogramCanvas(int[] count, int bins, Applet applet) {
		this.applet=applet;
		this.bins = bins;
		this.count =count;
	}


	public histogramCanvas(int[] count, int bins, 
					int vPixels, Applet applet) {
		this.applet=applet;
		this.bins = bins;
		this.count =count;
		this.vPixels=vPixels;
	}


/**
 *    Initialize the histogram to draw theoretical distribution
 * @param count is  an array of counts of the objects in the bins
 * @param bins  tells how many bins to make
 * @param applet is the applet calling the class
 * @param theoreticalCount is the array of theoretical distribution values
 */	
	public histogramCanvas(int[] count, int bins, double[] theoreticalCount, Applet applet) {
		this.applet=applet;
		this.bins = bins;
		this.count =count;
		this.theoreticalCount=theoreticalCount;
		drawTheoretical = true;
	}




		
/**
 *    Painting method for drawHistogram
 */
 	public void paint(Graphics g){	
		Dimension s=size();
		int height=s.height;
		int width=s.width/(bins+2);
		int y;
		for (int j=0; j< bins; j++) {
		g.setColor(Color.white);		
		g.fillRect((j+1)*width,height-10- count[j]*vPixels 
					 	 ,width,count[j]*vPixels);
		if (drawTheoretical) {
			y = (int)(height-10-theoreticalCount[j]*vPixels);
			g.setColor(Color.red);
			g.drawLine((j+1)*width,y,(j+2)*width-1,y);
			g.drawLine((j+1)*width,y+1,(j+2)*width-1,y+1);
			} 
		}
	}
	
	public void update(int[] count){
		this.count = count;	
		repaint();
		}

	public void update(int[] count, double[] theoreticalCount){
		this.count = count;
		this.theoreticalCount=theoreticalCount;	
		repaint();
		}


/**
 *  clears Historgram and resets count and the number of bins
 * @param count is the number of objects in each bin
 * @param bins is the new number of bins
 */
	public void clearHistogram(int[] count,int bins) {
		this.bins = bins;
		this.count = count;
		repaint();
		}


	public void clearHistogram(int[] count,int bins, double[] theoreticalCount) {
		this.bins = bins;
		this.count = count;
		this.theoreticalCount=theoreticalCount;
		repaint();
		}

	public Dimension preferredSize(){
		Dimension d = new Dimension(200, 250);
		return d;
		}
		
}



