/*
	RandomSamp is an  applet that Draws graphics 
	Step by step to help students understand the normal distribution 
	of a simple random Sample, despite a distribution which is 
	not itself Normally distributed .  - 29 Sept 99 Fr. Chris Thiel, OFMCap <cct@ktb.net>
*/

import java.awt.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.*;
import java.lang.Math;


public class RandomSamp extends Applet
{
	//Global Variables
	Choice daMenu = new Choice();
	int currentStep=0;
	int lastStep=5;
	Color buttonColor;
	int buttonWidth=35;
	int buttonHeight=20;
	Rectangle go,back;
	Point A,B,M,C;
	int last, samp;
	int p [];
	float sampleSum, ss;
	float sm [];
	float sd [];
	
	private void circumscribe (Graphics g, int centerX, int centerY, int radius, int startArc, int endArc) {
		double theta [];
		theta = new double [21];
		g.setColor(Color.green);
		for (int j=0; j <= 20; j++ ) theta[j] = Math.PI*startArc/180 + j*((Math.PI*endArc/180)/20);
		for (int j=0; j < 20 ; j++) {
		 	g.drawLine(centerX +(int)(radius*Math.cos(theta[j])),(int)(centerY-(radius*Math.sin(theta[j]))), 
		              centerX +(int)(radius*Math.cos(theta[j+1])), (int)(centerY-(radius*Math.sin(theta[j+1]))));
		}
		g.setColor(Color.black);
	}
	private void dot (Graphics g, int x, int y, String label) {
	 	g.setColor(Color.red);
	 	g.fillOval(x-2,y-2,5,5);
	 	g.setColor(Color.black);
	 	g.drawString(label, x-4 , y-4);
	}
	private void fill_p(int n, int y){
		for (int i=0;i<n;i++){
			p[i+last]=y	;
		}
		last+=n;
	}
	public void init() {
	    buttonColor = new  Color( 200,250, 220);
	    setBackground(new Color(0xF0F0D0));
	    go = new Rectangle ( size().width-buttonWidth-10,size().height-buttonHeight-10,buttonWidth,buttonHeight);
	    back= new Rectangle ( 10, size().height-buttonHeight-10,buttonWidth,buttonHeight);
	    A = new Point (50, 100);
	    B = new Point (150,100);
	    M = new Point ( (int)((A.x+B.x)/2), (int)((A.y +B.y)/2) );
	    C = new Point (150,50);
	    daMenu.addItem("Sample Size of 5");
		daMenu.addItem("Sample Size of 10");
		daMenu.addItem("Sample Size of 20");
		daMenu.addItem("Sample Size of 50");
		daMenu.addItem("Sample Size of 100");
		daMenu.addItem("Sample Size of 200");
		daMenu.addItem("Sample Size of 500");
		
		add(daMenu);
		
		p = new int[1534];
		sm = new float [101];
		sd = new float [101];
		last=0;
		// fill the penny array with the coorect freq for each year
		fill_p(6,97);
		fill_p(131,96);
		fill_p(118,95);
		fill_p(138,94);
		fill_p(57,93);
		fill_p(69,92);
		fill_p(76,91);
		fill_p(78,90);
		fill_p(86,89);
		fill_p(73,88);
		fill_p(46,87);
		fill_p(61,86);
		fill_p(45,85);
		fill_p(51,84);
		fill_p(50,83);
		fill_p(55,82);
		fill_p(39,81);
		fill_p(44,80);
		fill_p(30,79);
		fill_p(23,78);
		fill_p(39,77);
		fill_p(31,76);
		fill_p(27,75);
		fill_p(23,74);
		fill_p(21,73);
		fill_p(11,72);
		fill_p(14,71);
		fill_p(19,70);
		fill_p(12,69);
		fill_p(7,68);
		fill_p(9,67);
		fill_p(4,66);
		fill_p(4,65);
		fill_p(9,64);
		fill_p(6,63);
		fill_p(6,62);
		fill_p(6,61);
		fill_p(3,60);
		fill_p(2,59);
		fill_p(1,58);
		fill_p(1,53);
		fill_p(2,42);
		fill_p(1,36);
		
		repaint();
	}
	
	private int CoordX(float x) {
		return (int)((99-x)*size().width/40);
	}
	
	private int CoordY(int y){
		return y*3 + 40;
	}
	public void paint( Graphics g ) {
	  
		switch (daMenu.getSelectedIndex()) {
			case (0) : // sample size of 5
				
				lastStep=5;
				
				break;
			case (1) : // 10
				
				lastStep=10;
				break;
			case (2) : // 20
				
				lastStep=20;
				break;
			case (3) : //50
				
				lastStep=50;
				break;
			case (4) : //100
				
				lastStep=100;
				break;
			case (5) : //200
				
				lastStep=200;
				break;
			case (6) : //500
				
				lastStep=500;
				break;
		}// of switch
		sampleSum = 0;
		ss = 0;
		
		for (int i=0;i<lastStep;i++){
			samp = 1+(int)(Math.random()*1533);
			sampleSum += p[samp] ;
			ss +=  p[samp]*p[samp];
		}	
		
		sm [currentStep] = sampleSum/lastStep;
		sd [currentStep] = (float)(Math.sqrt( (ss - (sampleSum*sampleSum/lastStep))/(lastStep-1) ));
		// update location of buttons in case window size changed
	   	go.move(size().width-buttonWidth-10,size().height-buttonHeight-10);
	   	back.move(10,size().height-buttonHeight-10);
		//draw colorful buttons and show how many random samples we took
		g.setColor(Color.red);
		g.drawString( "Number of Random Samples: "+Integer.toString(currentStep), (int)(size().width / 2)-50, size().height-buttonHeight );
		g.setColor( buttonColor);
		g.fill3DRect(go.x, go.y, go.width, go.height,true);
		g.fill3DRect(back.x, back.y, back.width, back.height,true);
		g.setColor( Color.black);
		g.draw3DRect(go.x, go.y, go.width, go.height,true);
		g.draw3DRect(back.x, back.y, back.width, back.height,true);
		g.setColor(Color.blue);
		g.drawString("Clear", back.x+(int)(.15*buttonWidth), back.y+(int)(.75*buttonHeight));
		g.drawString("Next", go.x+(int)(.15*buttonWidth),go.y+(int)(.75*buttonHeight));
		g.drawString("Sampl Avg = "+sm[currentStep], 50, 20);
		g.drawString("Sampl SD = "+sd[currentStep], 50, 35);

	
		for (int i=1; i<= currentStep; i++){
			g.setColor(Color.magenta);
			g.fillOval ( CoordX(sm[i]) , CoordY(i)-1, 3, 3);
			g.setColor(Color.blue);
		 	g.drawLine ( CoordX(sm[i]-(float)(1.96*sd[i]/Math.sqrt(lastStep))), CoordY(i), CoordX(sm[i]+(float)(1.96*sd[i]/Math.sqrt(lastStep))), CoordY(i)); 
		}
		for (int i=98; i > 50;i-=2){
			g.setColor(Color.black);
			g.drawString(Integer.toString(i), CoordX(i)-4, size().height-50);
		}
		g.setColor(Color.orange);
		g.drawLine(CoordX((float)(86.156)), 40, CoordX((float)(86.156)), size().height-50);
	}
    /*
     * Mouse methods
     */
    public boolean mouseDown(java.awt.Event evt, int x, int y) {
        if ( go.inside(x,y) ) currentStep++;
        if ( back.inside(x,y) ) currentStep=0;
        if ( currentStep > 100) currentStep=0;
        
        if ( go.inside(x,y)|| back.inside(x,y)) repaint();
        return true;
    }
	
    public boolean mouseMove(java.awt.Event evt, int x, int y) {
        //repaint();
        return true;
    }
    public void mouseEnter() {
        //repaint();
    }

    public void mouseExit() {
       
       // repaint();
    }
    /*
     * Event Methos for pop up menu
     */
    public boolean action(Event e, Object arg) {
                   if (e.target instanceof Choice) {
                     currentStep=0;
                     repaint();
                      return true;
                  } else { return false;}
               
    }
    
    public boolean handleEvent(Event e) {
                  return super.handleEvent(e);
    }
}//of class declaration
