package confidence;



import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.geom.*;

/** 
 * ConfidencePanel display for confidence applet
 * @author Charles S. Stanton
 * @version Sun Jul 14 11:41:10 PDT 2002
*/
class ConfidencePanel extends JPanel {
    
    Dimension r; 
    int topGap = 5;
    int bottomGap = 20;
    int leftGap = 25;
    int midHeight;
    double[] cvlist = {1.7291, 2.0930,  2.8609}; //t values
    int cvIndex=1;
    double cv;
    int nTrials;
    ConfidenceModel cm;
    
    /**
    * Constructor for ConfidencePanel
    *
    */
    ConfidencePanel( ConfidenceModel acm) {
        cm = acm;
        nTrials = cm.getNTrials();
        setBackground(Color.white);
    }

    public Dimension getPreferredSize(){
        return new Dimension( 300, 250);
    }

    public Dimension getMinimumSize(){
        return new Dimension( 200, 200);
    }
    


/**
* paint method for ConfidencePanel
*/
    public void paint(Graphics g){
            super.paint(g);
            
            int pheight;
            r=getSize();
            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);
            drawData(cm, leftGap, midHeight, topGap,xInterval, g);
        }
    

    private void drawData(ConfidenceModel cm, int leftGap , int 	
                    midHeight, int topGap,
                    int xInterval, Graphics g){
                        
            int xPos = leftGap+xInterval/2;
            double[][] normalData = cm.getNormalData();
            double[] xBar = cm.getXBar();
            double[] s= cm.getS();
            int n = normalData[0].length;	
            double yPos;
            double tAlpha= cvlist[cvIndex]; // 19 df 
            Line2D topLine, bottomLine, meanLine, vLine, dataLine;
            
            
            Graphics2D g2 = (Graphics2D) g;
            for (int trial=0; trial < nTrials; trial++) {
                g2.setPaint(Color.blue);
                xPos += xInterval;
                for (int i =0; i< n; i++) {
                yPos = scaleHeight(normalData[trial][i]);
                dataLine = new Line2D.Double(xPos-2, yPos, xPos+2,yPos);
                g2.draw(dataLine);
                //g.drawLine(xPos-2, yPos ,xPos+2,yPos);
                    }
                xPos += xInterval;
             
                double  xBarHeight= scaleHeight(xBar[trial]);
                double ciTop = xBar[trial]+tAlpha*s[trial]/Math.sqrt(n);
                double ciTopScaled = scaleHeight(ciTop );
                double  ciBottom = xBar[trial] -
                    tAlpha*s[trial]/Math.sqrt(n) ;
                double ciBottomScaled = scaleHeight(ciBottom);
                if ( (ciTop < 0) || (ciBottom > 0)) {
                    g2.setPaint(Color.red);
                }
                else{
                    g2.setPaint(Color.black);
                }
                //draw a short line at top of confidence interval
                topLine = new Line2D.Double( xPos-2, ciTopScaled, xPos+2, ciTopScaled);
                g2.draw(topLine);
                //draw a short line at mean position
                meanLine = new Line2D.Double(xPos-2, xBarHeight,
                         xPos+2, xBarHeight);
                         g2.draw(meanLine);
                // draw a short line at bottom of confidence interval
                bottomLine = new Line2D.Double(xPos-2, ciBottomScaled, xPos+2, ciBottomScaled);
                g2.draw(bottomLine);
                //draw the vertical line for the confidence interval
                vLine = new Line2D.Double(xPos, ciTopScaled, xPos, ciBottomScaled);
                g2.draw(vLine);
                //g.drawLine(xPos, ciTop, xPos, ciBottom);
                g2.setPaint(Color.green);
                
                g.drawLine(xPos+xInterval/2, topGap, 
                    xPos+xInterval/2, topGap+midHeight);
            }
        }

    private double scaleHeight( double x){
        double sh;
        sh =(topGap-midHeight*(x-3.0)/6.0);
        return sh;
        }

/*
* Draws scale of -3.0 to 3.0
*/	
    private void drawVscale( int xpos, int bottomY, int topY ,Graphics g) {
    //	String scaleLabels = {"-3.0","-2.0","-1.0","0.0","1.0","2.0","3.0"};
        //nt scaleHeight = bottomY - topY;
        g.drawLine(xpos, bottomY, xpos, topY);
        g.setColor(Color.black);
        int increment = (topY-bottomY)/6;
        int yTickPos = bottomY;
        for (int i = -3; i <= 3; i++){
            g.drawLine(xpos-3,yTickPos,xpos+3,yTickPos);
            g.drawString( String.valueOf(i), xpos+6, yTickPos+4);
            yTickPos += increment;
        }

    }

    public void setConfidenceLevel(int i){
        cvIndex =i;
    }



    public void update(ConfidenceModel cm){
        nTrials = cm.getNTrials();
        repaint();
        }
}	
