/* confidenceCanvas.java */

import java.awt.*;
import java.applet.*;


/** confidenceCanvas.java
* display for confidence applet
*/
class confidenceCanvas extends Canvas {
	
	Dimension r; 
	int nTrials;
	int n;
	double[][] normalData;
	double[] xBar;
	double[] s;
// LAYOUT PARAMETERS
	int topGap = 5;
	int bottomGap = 20;
	int leftGap = 25;
	int midHeight;
	double[] cvlist = {1.7291, 2.0930,  2.8609};
	int cvIndex;
	double cv;
	
/**
* Constructor for confidenceCanvas
*
*/

confidenceCanvas( int n,int nTrials) {
		this.n=n;
		this.nTrials = nTrials;
		}

	public Dimension preferredSize(){
		return new Dimension( 300, 250);
		}

	public Dimension minimumSize(){
		return new Dimension( 200, 200);
		}
	


/**
* paint method for confidenceCanvas
*/

	public void paint(Graphics g){
			int pheight;
			r=size();
			int xInterval = (r.width-leftGap)/(2*nTrials);
			int xPos;

			this.midHeight = r.height-(topGap+bottomGap);
			g.setColor(Color.black);
			g.drawLine(leftGap+8,midHeight+topGap,
					r.width,midHeight+topGap);
			g.drawLine(leftGap,topGap, r.width,topGap);
			g.setColor(Color.magenta);
			g.drawLine(leftGap+8, topGap + midHeight/2,
					 r.width, topGap+midHeight/2);
			drawVscale( 5, r.height-bottomGap, topGap, g);
			if (normalData != null) {
				drawData(normalData, leftGap, midHeight, topGap,xInterval, g);
			
				}
			}
	

	private void drawData(double[][] normalData, int leftGap , int 	
					midHeight, int topGap,
					int xInterval, Graphics g){
			int xPos = leftGap+xInterval/2;
				
			int yPos;
			double tAlpha= cvlist[cvIndex]; // 19 df 
	
			for (int trial=0; trial < nTrials; trial++) {
				g.setColor(Color.blue);
				xPos += xInterval;
				for (int i =0; i< n; i++) {
				yPos = scaleHeight(normalData[trial][i]);
				g.drawLine(xPos-2, yPos
					,xPos+2,yPos);
					}
				xPos += xInterval;
				int xBarHeight= scaleHeight(xBar[trial]);
				int ciTop = scaleHeight(xBar[trial]+
					tAlpha*s[trial]/Math.sqrt(n) );
				int ciBottom = scaleHeight(xBar[trial] -
					tAlpha*s[trial]/Math.sqrt(n) );
				g.setColor(Color.red);
				g.drawLine(xPos-2,ciTop,xPos+2,ciTop);
				g.drawLine(xPos-2, xBarHeight,
						 xPos+2, xBarHeight);
				g.drawLine(xPos-2, ciBottom, xPos+2,
							ciBottom);
				g.drawLine(xPos, ciTop, xPos, ciBottom);
				g.setColor(Color.green);
				g.drawLine(xPos+xInterval/2, topGap, 
					xPos+xInterval/2, topGap+midHeight);
				}
		}

	private int scaleHeight( double x){
		int sh;
		sh =(int)(topGap-midHeight*(x-3)/6);
		return sh;
		}

/*
* Draws scale of -3.0 to 3.0
*/	
	private void drawVscale( int xpos, int bottomY, int topY ,Graphics g) {
		//nt scaleHeight = bottomY - topY;
		g.setColor(Color.black);
		g.drawLine(xpos, bottomY, xpos, topY);
		g.drawLine(xpos-3, bottomY, xpos+3, bottomY);
		g.drawString( "-3.0", xpos+6, bottomY+4);
		g.drawLine(xpos-3, topY, xpos+3, topY);
		g.drawString("3.0", xpos+6, topY+4);
		g.drawLine(xpos-3, (topY+bottomY)/2, xpos+3, (topY+bottomY)/2);
		g.drawString( "0.0", xpos+6, (topY+bottomY)/2+4);
		g.drawLine(xpos-3, (topY/6+5*bottomY/6), xpos+3, (topY/6+5*bottomY/6));
		g.drawString("-2.0", xpos+6, (topY/6+5*bottomY/6)+4);
		g.drawLine(xpos-3, (topY/3+2*bottomY/3), xpos+3, (topY/3+2*bottomY/3));
		g.drawString("-1.0", xpos+6, (topY/3+2*bottomY/3)+4);
		g.drawLine(xpos-3, (4*topY/6+2*bottomY/6), xpos+3, (4*topY/6+2*bottomY/6));
		g.drawString("1.0",xpos+6,4*topY/6+2*bottomY/6);
		g.drawLine(xpos-3, (5*topY/6+bottomY/6), xpos+3,
				(5*topY/6+bottomY/6));
		g.drawString("2.0", xpos+6, 5*topY/6+bottomY/6);
		}
	
		

/**
* clear clears canvas and resets parameters
* @param n
* @param nTrials
*/
	public void clear(int n, int nTrials){
		this.xBar = new double[nTrials];
		this.normalData = new double[nTrials][n];		
		this.n = n;
		this.nTrials = nTrials;
		repaint();
		}


	public void update(int cvIndex, double[][] normalData, 
				double[] xBar, double[] s){
		this.normalData = normalData;
		this.xBar = xBar;
		this.s=s;
		this.cvIndex=cvIndex;
		repaint();
		
		}
}	
