import java.applet.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class SetupButton extends Applet
{
	public MonteCarlo monte;
	public int number_monte_points;
	public boolean monte_boolean;
	public TextField monte_runs;
	public Label monte_label;
									   
	public boolean all = true;  // if true, show all calculations as a single applet, if false only show the requested applet
	private Button default_button = new Button("Default");
	private Button compute_button = new Button("Compute");
	private Button critique_button = new Button("Critique");
	public Button bar_plot = new Button("Bar Chart");
	String blank = "     ";
	private myTextField[][] cxx;
	private myTextField[][] full_cxx;
	private myTextField[] full_dx;
	private myTextField[] full_fx;
	private myTextField[] fcoveri;
	private myTextField[] dcoverj;
	private myTextField[][] fijcover;
	private int M;
	private int N;
	private int xspace;
	private int yspace;
	
	private double[] co_array;
	private double[] yo_array;
	private double[] yc_array;
	private double[] sc_array;

	private myTextField labor_rate;
	private myTextField labor_burden;
	private myTextField depreciation;
	private myTextField quantity;
	private myTextField hours_per_year;
	
	private myTextField time1;
	private myTextField defects1;
	private myTextField mach_cap1;
	private myTextField mach_prog1;
	private myTextField mach_cost1;
	private myTextField mat_cost1;
	
	private myTextField time2;
	private myTextField defects2;
	private myTextField mach_cap2;
	private myTextField mach_prog2;
	private myTextField mach_cost2;
	private myTextField mat_cost2;
	
	private SlideBar op_util1;
	private SlideBar mach_util1;
	private SlideBar op_util2;
	private SlideBar mach_util2;
	
	private myTextField costin;
	private SlideBar yieldin;
	private myTextField costtest;
	private myTextField nrew;
	private SlideBar yieldtest;
	private SlideBar fctest;
	private SlideBar yielddiag;
	private SlideBar fd;
	private mySlideBar yieldrew;
	private SlideBar fr;
	private SlideBar fpos;
	private SlideBar yieldbefore;
	private SlideBar yieldafter;
	private SlideBar loops;
	private myTextField costout;
	private myTextField costdiag;
	
	private myTextField costrew;
	private dataCompMember costin_data;
	private dataCompMember costtest_data;
	private dataCompMember costdiag_data;
	private dataCompMember costrew_data;
	private dataCompMember yieldin_data;
	private dataCompMember yieldbefore_data;
	private dataCompMember yieldafter_data;
	private dataCompMember yielddiag_data;
	private dataCompMember yieldrew_data;
	private dataCompMember fr_data;
	private dataCompMember fd_data;
	private dataCompMember fc_data;
	private dataCompMember fp_data;

	private myTextField costout_sd;
	private myTextField yieldout_sd;
	private myTextField pass_sd;
	private myTextField scrap_sd;
	
	private myTextField yieldout;
	private myTextField pass;
	private myTextField scrap;
	private myTextField diagscrap;
	private myTextField rewscrap;
	private Choice yieldchoice;
	private Choice fpchoice;
	public myChoice mc_choice;
	private Button default_button_s = new Button("Default");
	private Button compute_button_s = new Button("Compute");
	private Button critique_button_s = new Button("Critique");
	private Button default_button_p = new Button("Default");
	private Button compute_button_p = new Button("Compute");
	private Button critique_button_p = new Button("Critique");
	private Button default_button_cp = new Button("Default");
	private Button compute_button_cp = new Button("Compute");
	private Button critique_button_cp = new Button("Critique");
	private Button plot = new Button("Start Plot");
	private ScatterPlot sp;

			
	private Button default_button_2 = new Button("Default");
	private Button update_button_2 = new Button("Update");
	private Button compute_button_2 = new Button("Compute");
	private Button critique_button_2 = new Button("Critique");
	private myTextField[] dx;
	private myTextField[] dpmx;
	private myTextField[] nx;
	private myTextField d1;
	private myTextField d2;
	private myTextField yield;
	private myTextField[][] dj;
	private myTextField[][] cij;
	private myTextField[][] fijr;
	private Button update_button_3 = new Button("Update");
	private Button compute_button_3 = new Button("Compute");
	private Button critique_button_3 = new Button("Critique");
	private Button default_button_4 = new Button("Default");
	private Button update_button_4 = new Button("Update");
	private Button compute_button_4 = new Button("Compute");
	private Button critique_button_4 = new Button("Critique");
	private Button default_button_5 = new Button("Default");
	private Button update_button_5 = new Button("Update");
	private Button compute_button_5 = new Button("Compute");
	private Button critique_button_5 = new Button("Critique");
	
	private Color cij_field_color;
	private Color dj_field_color;
	private Color fij_field_color;

    private DataClass data;
	private String path = "data/bogus_data";  // load/save path

    // Parameters
    private String slide;

	//MyCanvas c;

	public String getAppletInfo()
	{
		return("true");
	}

    public void start()
	{
        jitter(1);  // doesn't work
	}
	
	public void destroy()
	{
		System.out.println("in destroy");
	}
	

	public void init()
	{
		monte = new MonteCarlo(this,234);
		monte_boolean = false;
		
		// Initialize dataCompMembers
		costin_data = new dataCompMember(2);
		costtest_data = new dataCompMember(2);
		costdiag_data = new dataCompMember(2);
		costrew_data = new dataCompMember(2);
		yieldin_data = new dataCompMember(2);
		yieldbefore_data = new dataCompMember(2);
		yieldafter_data = new dataCompMember(2);
		yielddiag_data = new dataCompMember(2);
		yieldrew_data = new dataCompMember(2);
		fc_data = new dataCompMember(2);
		fp_data = new dataCompMember(2);
		fr_data = new dataCompMember(2);
		fd_data = new dataCompMember(2);

	    M = 3;
	    N = 2;
	    data = new DataClass();
	    data.initialize(N,M);
	    xspace = 400;
	    yspace = 5;
	    slide = this.getParameter("slide");
		if(slide != null && (slide.compareTo("simpleprocess") == 0 || slide.compareTo("complexprocess") == 0 || slide.compareTo("process") == 0 || slide.compareTo("solution") == 0))
			all = false;
		else  {
			slide = "one";
			all = true;
		}
		//slide = "complexprocess";
		all = false;
		
		if(slide.compareTo("solution") == 0)
			setBackground(Color.lightGray);
		else
			setBackground(Color.white);
		cij_field_color = Color.yellow;
		dj_field_color = Color.cyan;
		fij_field_color = Color.green;
		
		Label field_name;

		Panel master = new Panel();
		GridBagLayout gblm = new GridBagLayout();
		master.setLayout(gblm);
		GridBagConstraints gbcm = new GridBagConstraints();
		gbcm.fill = GridBagConstraints.BOTH;
        gbcm.weightx = 1.0;

		if(all == false && slide.compareTo("simpleprocess") == 0)  {
			build_simpleprocess();
		}
		
		if(all == false && slide.compareTo("complexprocess") == 0)  {
			build_complexprocess();
		}
				
		if(all == false && slide.compareTo("process") == 0)  {
			build_process();
		}
		
		if(all == false && slide.compareTo("solution") == 0)  {
			build_solution();
		}
		
	    //------------------------------------------------------------
        // Conversion Matrix
        //------------------------------------------------------------
        if(all || slide.compareTo("one") == 0)  {
            Panel comp_matrix = new Panel();
            comp_matrix.setLayout(new GridLayout(2,1));

    	    cxx = new myTextField[M+1][N+1];
    	    Panel cm = new Panel();
    	    cm.setLayout(new GridLayout(4,2));
    	    cm.add(new Label("   A"));
    	    cm.add(new Label("   B"));
    	    for(int i = 1; i <= M; i++)  {
    	        for(int j = 1; j <= N; j++)  {
                    cxx[i][j] = new myTextField(this,blank,cij_field_color);
    	            cm.add(cxx[i][j]);
    	        }
    	    }

            MyCanvas3 cf2 = new MyCanvas3(this,"full2.gif",0,16);
            cf2.setBackground(Color.white);
            cf2.resize(20,75);

            MyCanvas3 cf3 = new MyCanvas3(this,"full6.gif",0,16);
            cf3.setBackground(Color.white);
            cf3.resize(15,75);

    		// Faults list
    		Panel faults = new Panel();
    		faults.setLayout(new GridLayout(4,1));
    		faults.add(new Label(""));
    		faults.add(new Label("Open"));
    		faults.add(new Label("Short"));
    		faults.add(new Label("Other"));

    		// C equals
    		Panel Cequals = new Panel();
    		Cequals.setLayout(new GridLayout(4,1));
    		Cequals.add(new Label(""));
    		Cequals.add(new Label(""));
    		Cequals.add(new Label("  C ="));
    		Cequals.add(new Label(""));

    		// Coversion Matrix + Labels
    		Panel cm_plus = new Panel();
    		//cm_plus.setLayout(new GridLayout(1,3));
    		cm_plus.add(Cequals);
    		cm_plus.add(cf3);
    		cm_plus.add(cm);
    		cm_plus.add(cf2);
    		cm_plus.add(faults);

    		Panel bp = new Panel();
    		bp.add(critique_button);
    		bp.add(default_button);

    		comp_matrix.add(cm_plus);
    		comp_matrix.add(bp);

			if(all)  {
				field_name = new Label("Conversion Matrix");
				field_name.setFont(new Font("Helvetica",Font.BOLD,18));
				gbcm.insets = new Insets(0,150,0,0);
				gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
				gblm.setConstraints(field_name,gbcm);
				master.add(field_name);
				gbcm.insets = new Insets(0,0,0,0);
			}
			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(comp_matrix,gbcm);
			master.add(comp_matrix);
        }

		//Canvas space = new Canvas();
		SpaceCanvas space = new SpaceCanvas(xspace,106);
        //space.setBackground(Color.yellow);
        space.resize(xspace,106);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space,gbcm);
        master.add(space);
		
		Canvas space1 = new Canvas();
        //space1.setBackground(Color.yellow);
        space1.resize(xspace,4);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space1,gbcm);
        master.add(space1);


        //------------------------------------------------------------
        // Defect Spectrum Elements
        //------------------------------------------------------------
        if(all || slide.compareTo("two") == 0)  {
            Panel defect_spectrum_1 = new Panel();
            defect_spectrum_1.setLayout(new BorderLayout());
			
			if(all)  {
				field_name = new Label("Defect Spectrum");
				field_name.setFont(new Font("Helvetica",Font.BOLD,18));
				gbcm.insets = new Insets(0,165,0,0);
				gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
				gblm.setConstraints(field_name,gbcm);
				master.add(field_name);
				gbcm.insets = new Insets(0,0,0,0);
			}
			
            //Panel defect_spectrum = new Panel();
            //defect_spectrum.setLayout(new BorderLayout());
            dx = new myTextField[N+1];
            dpmx = new myTextField[N+1];
            nx = new myTextField[N+1];

            for(int j = 1; j <= N; j++)  {
                Panel defect_spectrum = new Panel();
                defect_spectrum.setLayout(new BorderLayout());
                if(j == 1)  {
                    //defect_spectrum.add("North",new Label("j=1, (placement error)"));
					Label lb = new Label("j=1, (placement error)");
					gbcm.insets = new Insets(0,120,0,0);
					gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
					gblm.setConstraints(lb,gbcm);
					master.add(lb);
					gbcm.insets = new Insets(0,0,0,0);
                    //add("North",new Label("j=1, (placement error)"));
                }
                if(j == 2)  {
                    //defect_spectrum.add("North",new Label("j=2, (broken wirebond)"));
					Label lb = new Label("j=2, (broken wirebond)");
					gbcm.insets = new Insets(0,100,0,0);
					gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
					gblm.setConstraints(lb,gbcm);
					master.add(lb);
					gbcm.insets = new Insets(0,0,0,0);
                    //add("North",new Label("j=2, (broken wirebond)"));
                }
    		/*    Integer J = new Integer(j);
                MyCanvas2 dequals = new MyCanvas2("       d" + J.toString() + " =",60,38);
                //dequals.setBackground(Color.yellow);
                dequals.resize(35,30);  */
				
				Integer J = new Integer(j);
                MyCanvas3 dequals = new MyCanvas3(this,"d" + J.toString() + ".gif",95,25);
                //dequals.setBackground(Color.yellow);
                //dequals.resize(100,30);

                dx[j] = new myTextField(this,blank,dj_field_color);
				//dx[j].setEditable(false);
                dpmx[j] = new myTextField(this,blank);
                nx[j] = new myTextField(this,blank);

                Panel e1 = new Panel();
                e1.add(dpmx[j]);
                e1.add(nx[j]);

                MyCanvas1 c = new MyCanvas1("1,000,000");
                //c.setBackground(Color.yellow);
                c.resize(90,1);

                Panel equation = new Panel();
                equation.setLayout(new GridLayout(2,1));
                equation.add(e1);
                equation.add(c);

                Panel r1 = new Panel();
                r1.add("West",new Label("="));
                r1.add(dx[j]);

    		    Panel result = new Panel();
    		    result.setLayout(new GridLayout(2,1));
    		    result.add(r1);

                GridBagLayout gbl =  new GridBagLayout();
    		    GridBagConstraints gbc = new GridBagConstraints();

                Panel d_plus1 = new Panel();
    		    d_plus1.setLayout(new GridLayout(1,2));
    		    d_plus1.add(dequals);
    		    d_plus1.add(equation);

    		    Panel d_plus = new Panel();
    		    d_plus.setLayout(gbl);

    		    gbc.insets = new Insets(0,10,0,0);
    		    gbl.setConstraints(d_plus1,gbc);
    		    d_plus.add(d_plus1);

    		    gbc.gridwidth = GridBagConstraints.REMAINDER;
    		    gbc.insets = new Insets(33,0,0,0);
    		    gbl.setConstraints(result,gbc);
    		    d_plus.add(result);

    		    defect_spectrum.add("Center",d_plus);
    		    //defect_spectrum.add("South",bp_2);

    		    //add(defect_spectrum);
				
				gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
				gblm.setConstraints(d_plus,gbcm);
				master.add(d_plus);

            }

        //------------------------------------------------------------
        // Defect Spectrum Yield Calculation from Elements
        //------------------------------------------------------------
        //if(slide.compareTo("three") == 0)  {

            Panel defect_spectrum_yield = new Panel();

            MyCanvas3 c31 = new MyCanvas3(this,"yield1.gif",0,0);
            c31.setBackground(Color.black);
            c31.resize(125,45);

            MyCanvas2 c32 = new MyCanvas2(") =",1,17);
            c32.resize(18,25);

            MyCanvas2 c33 = new MyCanvas2("+",1,17);
            c33.resize(10,25);

            d1 = new myTextField(this,blank);
			d1.setEditable(false);
            d2 = new myTextField(this,blank);
			d2.setEditable(false);
            yield = new myTextField(this,blank);
			yield.setEditable(false);

            defect_spectrum_yield.add(c31);
            defect_spectrum_yield.add(d1);
            defect_spectrum_yield.add(c33);
            defect_spectrum_yield.add(d2);
            defect_spectrum_yield.add(c32);
            defect_spectrum_yield.add(yield);

		    Panel bp_2 = new Panel();
		    bp_2.add(critique_button_2);
		    bp_2.add(default_button_2);
		    bp_2.add(compute_button_2);

		    defect_spectrum_1.add("Center",defect_spectrum_yield);
		    defect_spectrum_1.add("South",bp_2);
			
			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(defect_spectrum_1,gbcm);
			master.add(defect_spectrum_1);
		    //add(defect_spectrum_1);
		}

        SpaceCanvas space2 = new SpaceCanvas(xspace,13);
        //space2.setBackground(Color.yellow);
        space2.resize(xspace,13);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space2,gbcm);
        master.add(space2);

		Canvas space3 = new Canvas();
        //space3.setBackground(Color.yellow);
        space3.resize(xspace,7);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space3,gbcm);
        master.add(space3);


        //------------------------------------------------------------
        // fij calculation
        //------------------------------------------------------------
        if(all || slide.compareTo("four") == 0)  {
            Panel fij_panel1 = new Panel();
            fij_panel1.setLayout(new BorderLayout());
			
			if(all)  {
				field_name = new Label("fij Calculation");
				field_name.setFont(new Font("Helvetica",Font.BOLD,18));
				gbcm.insets = new Insets(0,175,0,0);
				gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
				gblm.setConstraints(field_name,gbcm);
				master.add(field_name);
				gbcm.insets = new Insets(0,0,0,0);
			}
			
            Panel fij_panel = new Panel();
            fij_panel.setLayout(new GridLayout(N*M,1));

            dj = new myTextField[M+1][N+1];
            cij = new myTextField[M+1][N+1];
            fijr = new myTextField[M+1][N+1];

            for(int j = 1; j <= N; j++)  {
                Integer z2 = new Integer(j);
                for(int i = 1; i <= M; i++)  {
                    Integer z1 = new Integer(i);
                    //MyCanvas2 fij = new MyCanvas2("f"+z1.toString()+z2.toString()+" = ",0,17);
					MyCanvas3 fij = new MyCanvas3(this,"f"+z1.toString()+z2.toString()+".gif",12,2);
					//fij.setBackground(Color.yellow);
					fij.resize(30,25);

                    MyCanvas2 e = new MyCanvas2(" = ",5,17);
                    //e.setBackground(Color.yellow);
                    e.resize(4,25);
                    dj[i][j] = new myTextField(this,blank);
                    cij[i][j] = new myTextField(this,blank);
                    fijr[i][j] = new myTextField(this,blank,fij_field_color);
					//fijr[i][j].setEditable(false);

                    fij_panel.add(fij);
                    fij_panel.add(dj[i][j]);
                    fij_panel.add(cij[i][j]);
                    fij_panel.add(e);
                    fij_panel.add(fijr[i][j]);

					gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
					gblm.setConstraints(fij_panel,gbcm);
					master.add(fij_panel);
                    //add(fij_panel);
                }
            }

    		Panel bp_3 = new Panel();
    		bp_3.add(critique_button_3);
    		bp_3.add(update_button_3);
    		bp_3.add(compute_button_3);
			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(bp_3,gbcm);
			master.add(bp_3);
    		//add(bp_3);
    	}

        SpaceCanvas space4 = new SpaceCanvas(xspace,161);
        //space4.setBackground(Color.yellow);
        space4.resize(xspace,161);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space4,gbcm);
        master.add(space4);

		Canvas space5 = new Canvas();
        //space5.setBackground(Color.yellow);
        space5.resize(xspace,7);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space5,gbcm);
        master.add(space5);

        //------------------------------------------------------------
        // full matrix
        //------------------------------------------------------------
        if(all || slide.compareTo("five") == 0)  {
            Panel full_comp_matrix = new Panel();

            MyCanvas3 cf1 = new MyCanvas3(this,"full1.gif",8,0);
            cf1.setBackground(Color.white);
            cf1.resize(60,60);

            MyCanvas3 cf2 = new MyCanvas3(this,"full2.gif",0,0);
            cf2.setBackground(Color.white);
            cf2.resize(30,60);

            MyCanvas3 cf3 = new MyCanvas3(this,"full3.gif",0,0);
            cf3.setBackground(Color.white);
            cf3.resize(45,60);

            MyCanvas3 cf4 = new MyCanvas3(this,"full4.gif",0,0);
            cf4.setBackground(Color.white);
            cf4.resize(30,60);


    	    full_cxx = new myTextField[M+1][N+1];
    	    Panel full_cm = new Panel();
    	    full_cm.setLayout(new GridLayout(3,2));
    	    for(int i = 1; i <= M; i++)  {
    	        for(int j = 1; j <= N; j++)  {
                    full_cxx[i][j] = new myTextField(this,blank);
    	            full_cm.add(full_cxx[i][j]);
    	        }
    	    }

    	    full_dx = new myTextField[M+1];
    	    Panel full_dm = new Panel();
    	    full_dm.setLayout(new GridLayout(2,1));
    	    for(int j = 1; j <= N; j++)  {
                full_dx[j] = new myTextField(this,blank);
    	        full_dm.add(full_dx[j]);
    	    }

    	    full_fx = new myTextField[M+1];
    	    Panel full_fm = new Panel();
    	    full_fm.setLayout(new GridLayout(3,1));
    	    for(int i = 1; i <= M; i++)  {
                full_fx[i] = new myTextField(this,blank,Color.lightGray);
    	        full_fm.add(full_fx[i]);
    	    }

    	    full_comp_matrix.add(cf1);
    	    full_comp_matrix.add(full_cm);
    	    full_comp_matrix.add(cf4);
    	    full_comp_matrix.add(full_dm);
    	    full_comp_matrix.add(cf3);
    	    full_comp_matrix.add(full_fm);
    	    full_comp_matrix.add(cf2);

    		Panel full_bp = new Panel();
    		full_bp.add(critique_button_4);
    		full_bp.add(update_button_4);
    		full_bp.add(compute_button_4);

			if(all)  {
				field_name = new Label("Full Matrix");
				field_name.setFont(new Font("Helvetica",Font.BOLD,18));
				gbcm.insets = new Insets(0,190,0,0);
				gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
				gblm.setConstraints(field_name,gbcm);
				master.add(field_name);
				gbcm.insets = new Insets(0,0,0,0);
				//add(field_name);
			}
			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(full_comp_matrix,gbcm);
			master.add(full_comp_matrix);
            //add(full_comp_matrix);
			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(full_bp,gbcm);
			master.add(full_bp);
            //add(full_bp);
        }

        SpaceCanvas space6 = new SpaceCanvas(xspace,137);
        //space6.setBackground(Color.yellow);
        space6.resize(xspace,137);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space6,gbcm);
        master.add(space6);

		Canvas space7 = new Canvas();
        //space7.setBackground(Color.yellow);
        space7.resize(xspace,7);
		gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblm.setConstraints(space7,gbcm);
        master.add(space7);

        //------------------------------------------------------------
        // fault coverage
        //------------------------------------------------------------
        if(all || slide.compareTo("six") == 0)  {
            Panel dcover_panel = new Panel();
            dcover_panel.setLayout(new BorderLayout());

            Panel fcover1_panel = new Panel();
            fcover1_panel.setLayout(new GridLayout(M,1));

            Panel fcover2_panel = new Panel();
            fcover2_panel.setLayout(new GridLayout(N,1));

            Panel fcover3_panel = new Panel();

            fcoveri = new myTextField[M+1];
            dcoverj = new myTextField[N+1];
            fijcover = new myTextField[M+1][N+1];

            for(int i = 1; i <= M; i++)  {
                Panel fcover_panel = new Panel();
                Integer z1 = new Integer(i);
                MyCanvas3 fij = new MyCanvas3(this,"cover"+z1.toString()+".gif",0,0);
                //fij.setBackground(Color.yellow);
                fij.resize(45,25);

                fcoveri[i] = new myTextField(this,blank);

                fcover_panel.add(fij);
                fcover_panel.add(fcoveri[i]);

                fcover1_panel.add(fcover_panel);
            }
            dcover_panel.add("North",fcover1_panel);

            for(int j = 1; j <= N; j++)  {
                Panel fcover_panel = new Panel();
                Integer z1 = new Integer(j+3);
                MyCanvas3 dj = new MyCanvas3(this,"cover"+z1.toString()+".gif",0,0);
                //dj.setBackground(Color.yellow);
                dj.resize(80,25);
                fcover_panel.add(dj);

                for(int i = 1; i <= M; i++)  {

                    fijcover[i][j] = new myTextField(this,blank);
                    fcover_panel.add(fijcover[i][j]);

                    if(i != M)  {
                        z1 = new Integer(j+3+2);
                        MyCanvas3 plusf = new MyCanvas3(this,"cover"+z1.toString()+".gif",0,0);
                        //plusf.setBackground(Color.yellow);
                        plusf.resize(45,25);
                        fcover_panel.add(plusf);
                    }

                    if(i == M)  {
                        MyCanvas2 eq = new MyCanvas2("=",0,16);
                        eq.resize(10,25);
                        //eq.setBackground(Color.yellow);
                        fcover_panel.add(eq);

                        dcoverj[j] = new myTextField(this,blank);
                        fcover_panel.add(dcoverj[j]);
                    }
                    fcover2_panel.add(fcover_panel);
                }

                dcover_panel.add("Center",fcover2_panel);
            }

			if(all)  {
				field_name = new Label("Fault Coverage");
				field_name.setFont(new Font("Helvetica",Font.BOLD,18));
				gbcm.insets = new Insets(0,170,0,0);
				gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
				gblm.setConstraints(field_name,gbcm);
				master.add(field_name);
				gbcm.insets = new Insets(0,0,0,0);
				//add(field_name);
			}

			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(dcover_panel,gbcm);
			master.add(dcover_panel);
            //add(dcover_panel);

    		Panel bp_5 = new Panel();
    		bp_5.add(critique_button_5);
    		bp_5.add(default_button_5);
    		bp_5.add(update_button_5);
    		bp_5.add(compute_button_5);
			gbcm.gridwidth = GridBagConstraints.REMAINDER; //end row
			gblm.setConstraints(bp_5,gbcm);
			master.add(bp_5);
    		//add(bp_5);
		}
		
		if(all != false && slide.compareTo("process") != 0 && slide.compareTo("solution") != 0)
			add(master);



//---------------------


        //this.setBackground(Color.blue);
		this.resize(450,300);
		if(slide.compareTo("six") == 0)
		    this.resize(570,300);
		
		invalidate();
		validate();		//forces repaint
	}

    // Jitters the size of the form to force a refresh - necessary for NetScape
    public void jitter(int i)
	{
		if(i == 1 || System.getProperty("java.vendor").compareTo("Netscape Communications Corporation") == 0)  {
			// Get actuals here
			Dimension dimension = size();
			int x = dimension.width;
			int y = dimension.height;

			// Resize
			resize(x+1,y+1);
			show();

			// Reset current size
			resize(x,y);
		}
	}

    // Saves the state of the dataClass
    public void save()
	{
		if(!all)  {
			try{
				FileOutputStream fos = new FileOutputStream(this.path);
				ObjectOutputStream out = new ObjectOutputStream(fos);
				out.writeObject(data);
				out.flush();
				out.close();
			}  catch(IOException e) {System.out.println("IO Exception in save");}
		}
	}

	public void save_as()
			{
				// Get a file name and path
				FileDialog fd;
				fd = new FileDialog(new Frame(),"Save",FileDialog.SAVE);
				if(fd != null)  {	
					//fd.setDirectory(this.path);
					fd.show();
					String selected_directory = fd.getDirectory();
					String selected_file = fd.getFile();
					String local_path = selected_directory + selected_file;
					String load_save_path = selected_directory;
				
					// Write the object to the file
					if(selected_file != null)  {
						try{
							FileOutputStream fos = new FileOutputStream(local_path);
							ObjectOutputStream out = new ObjectOutputStream(fos);
							out.writeObject(data);
							out.flush();
							out.close();
						}  catch(IOException e) {}
					}
				}  else  {
					//ErrorDialog ed = new ErrorDialog("ERROR",true);
					//ed.report("Screw you");
					System.out.println("screw you");
					
				}
			}
	
	// Save the conversion matrix
	public void save_matrix()
	{
		// Save individual elements
		for(int i = 1; i <= M; i++)  {
			for(int j = 1; j <= N; j++)  {
				Double Sxx = Double.valueOf(cxx[i][j].getText());
			    double sxx = Sxx.doubleValue();
			    data.set_cij(i,j,sxx);
			}
		}

		// Save field colors
		for(int i = 1; i <= M; i++)  {
			for(int j = 1; j <= N; j++)  {
				data.set_cij_color(i,j,cxx[i][j].getBackground());
			}
		}
				
        // Save state to file
        save();
	}
	
	// Save the defect spectrum
	public void save_spectrum()
	{
	}
	
	
    // Loads the state of the dataClass
    public boolean load()
	{
			/*	// Get a file name and path
				FileDialog fd;
				fd = new FileDialog(new Frame(),"Load",FileDialog.LOAD);
				if(fd != null)  {	
					fd.setDirectory(this.path);
					fd.show();
					String selected_directory = fd.getDirectory();
					String selected_file = fd.getFile();
					String local_path = selected_directory + selected_file;
					String load_save_path = selected_directory;
				
					// Write the object to the file
					if(selected_file != null)  {
						
						*/
						
		if(!all)  {
			 try{
				FileInputStream fis = new FileInputStream(this.path);
				//FileInputStream fis = new FileInputStream(path);
				ObjectInputStream in = new ObjectInputStream(fis);
				data = (DataClass) in.readObject();
				in.close();
			}  catch(Exception e) {System.out.println("IO Exception in load"); return(false);}
		}
					
				//	}}
					
					
					return(true);
	}


	public boolean action(Event event, Object obj)
	{
		Object oTarget = event.target;

		if(oTarget instanceof Button)  {
            Button button = (Button)oTarget;
			if(button == critique_button)
			{
			    String help_string = "";

			    // Check individual elements
			    for(int i = 1; i <= M; i++)  {
			        for(int j = 1; j <= N; j++)  {
			            Double Sxx = Double.valueOf(cxx[i][j].getText());
			            double sxx = Sxx.doubleValue();
			            data.set_cij(i,j,sxx);
			            Integer I = new Integer(i);
			            Integer J = new Integer(j);
			            if(sxx > 1.0)  // all must be less than or equal to one
			                help_string = help_string + "Element C" + I.toString() + J.toString() + " must be less than or equal to one.\n";
			            if(sxx < 0.0)  // all must be greater than or equal to zero
			                help_string = help_string + "Element C" + I.toString() + J.toString() + " must be greater than or equal to zero.\n";
			        }
			    }

			    // Check columns
			    for(int j = 1; j <= N; j++)  {
			        double sxx = 0.0;
			        for(int i = 1; i <= M; i++)  {
			            Double Sxx = Double.valueOf(cxx[i][j].getText());
			            sxx += Sxx.doubleValue();

			        }
			        Integer J = new Integer(j);
			        if(sxx > 1.0)  // all rows must be less than or equal to one
			                help_string = help_string + "Row " + J.toString() + " must sum to be less than or equal to one. \n ";
			        if(sxx < 1.0)  // all rows should be equal to one
			                help_string = help_string + "Row " + J.toString() + " should sum to one if defects are conserved. \n ";
			    }

			    // Check for no errors
			    if(help_string.length() < 5)
			        help_string = "No errors detected.";

                // Save state to file
				for(int i = 1; i <= M; i++)  {
			        for(int j = 1; j <= N; j++)  {
						data.set_cij_color(i,j,cxx[i][j].getBackground());
			        }
			    }
                save();

			    // Post results of error check
			    Frame bogus_frame = new Frame();
                HelpDialog hd = new HelpDialog(bogus_frame,"Conversion Matrix",help_string,true);
                hd.popup();
			}
			if(button == default_button)
			{
				this.load();
				
			/*	for(int i = 1; i <= M; i++)  {
					for(int j = 1; j <= N; j++)  {
						Double z = new Double(data.get_cij(i,j));
						cxx[i][j].setText(z.toString());
					}
				}  */
			    cxx[1][1].setText("0.1");
			    cxx[1][2].setText("0.7");
			    cxx[2][1].setText("0.8");
			    cxx[2][2].setText("0.0");
			    cxx[3][1].setText("0.1");
			    cxx[3][2].setText("0.3");  

			    data.set_cij(1,1,0.1);
			    data.set_cij(1,2,0.7);
			    data.set_cij(2,1,0.8);
			    data.set_cij(2,2,0.0);
			    data.set_cij(3,1,0.1);
			    data.set_cij(3,2,0.3);
				
				this.save();
			}

			if(button == compute_button)
			{
			    save_matrix();
			}
			
			if(button == default_button_2)
			{
				this.load();
				
			    dpmx[1].setText("1000");
			    dpmx[2].setText("100");
			    nx[1].setText("10");
			    nx[2].setText("4000");
				
				this.save();
			}

			if(button == compute_button_2)
			{
				this.load();
				
			    Double dpm1 = Double.valueOf(dpmx[1].getText());
			    Double dpm2 = Double.valueOf(dpmx[2].getText());
			    Double n1 = Double.valueOf(nx[1].getText());
			    Double n2 = Double.valueOf(nx[2].getText());

			    double x = dpm1.doubleValue()*n1.doubleValue()/1000000.0;
			    Double x1 = new Double(x);
			    dx[1].setText(x1.toString());

			    x = dpm2.doubleValue()*n2.doubleValue()/1000000.0;
			    x1 = new Double(x);
			    dx[2].setText(x1.toString());

			    // yield calculation
			    double sum = 0.0;
			    for(int j = 1; j <= N; j++)  {
			        Double Sxx = Double.valueOf(dx[j].getText());
			        double sxx = Sxx.doubleValue();
			        sum += sxx;
			        Double z = new Double(sxx);
			        Integer J = new Integer(j);
					if(j == 1)  {
			            d1.setText(z.toString());
						d1.setBackground(dx[j].getBackground());
					}
					if(j == 2)  {
			            d2.setText(z.toString());
						d2.setBackground(dx[j].getBackground());
					}
			    }
			    Double z = new Double(1.0-sum);
			    yield.setText(z.toString());

			    // populate dj fields
			    for(int i=1; i<=M; i++)  {
			        for(int j=1; j<=N; j++)  {
			            if(j == 1)  {
			                data.set_dj(j,dpm1.doubleValue()*n1.doubleValue()/1000000.0);
			                x1 = new Double(data.get_dj(j));
			            }
			            if(j == 2)  {
			                data.set_dj(j,dpm2.doubleValue()*n2.doubleValue()/1000000.0);
			                x1 = new Double(data.get_dj(j));
			            }
			        }
			    }
				
				for(int j = 1; j <= N; j++)  {
					data.set_dj_color(j,dx[j].getBackground());
					System.out.println("color = " + dx[j].getBackground());
				}
				this.save();
			}

            if(button == update_button_3)
			{

			    // Reset dataClass state
	            this.load();
				
				// if all is true, go back and capture the data from the conversion matrix
				// only other way this data is captured is if its critique button is pressed
				save_matrix();
				
			    // populate dj fields
				Double x1;
			    for(int i=1; i<=M; i++)  {
			        for(int j=1; j<=N; j++)  {
			            x1 = new Double(data.get_dj(j));
						System.out.println("x1 = " + x1.toString());
			            dj[i][j].setText(x1.toString());
						dj[i][j].setFieldColor(data.dj_color[j]);
			        }
			    }

			    // populate conversion matrix
			    Double z;
			    for(int i = 1; i <= M; i++)  {
			        for(int j = 1; j <= N; j++)  {
			            z = new Double(data.get_cij(i,j));
			            cij[i][j].setText(z.toString());
						cij[i][j].setFieldColor(data.cij_color[i][j]);
			            fijr[i][j].setText("");
			        }
			    }
			}

			if(button == compute_button_3)
			{
			    for(int i=1; i<=M; i++)  {
			        for(int j=1; j<=N; j++)  {
			            Double x1 = Double.valueOf(dj[i][j].getText());
			            double sx1 = x1.doubleValue();
			            Double x2 = Double.valueOf(cij[i][j].getText());
			            double sx2 = x2.doubleValue();
						SigFig s = new SigFig();
			            Double z = new Double(s.customround(sx1*sx2,3));
						fijr[i][j].setText(z.toString());
                        data.set_fij(i,j,sx1*sx2);
						
						data.set_fij_color(i,j,fijr[i][j].getBackground());
			        }
			    }
				
				this.save();
			}

			if(button == update_button_4)
			{
			    // Reset dataClass state
	            this.load();

			/*    Double dpm1 = Double.valueOf(dpmx[1].getText());
			    Double dpm2 = Double.valueOf(dpmx[2].getText());
			    Double n1 = Double.valueOf(nx[1].getText());
			    Double n2 = Double.valueOf(nx[2].getText());

			    double x = dpm1.doubleValue()*n1.doubleValue()/1000000.0;
			    Double x1 = new Double(x);

			    x = dpm2.doubleValue()*n2.doubleValue()/1000000.0;
			    x1 = new Double(x);  */

			    // populate dj fields
				Double x1;
			    for(int i=1; i<=M; i++)  {
			        for(int j=1; j<=N; j++)  {
			        /*    if(j == 1)
			                x1 = new Double(dpm1.doubleValue()*n1.doubleValue()/1000000.0);
			            if(j == 2)
			                x1 = new Double(dpm2.doubleValue()*n2.doubleValue()/1000000.0);  */
			            x1 = new Double(data.get_dj(j));
			            full_dx[j].setText(x1.toString());
						full_dx[j].setFieldColor(data.dj_color[j]);
			        }
			    }

			    // populate conversion matrix
			    Double z;
			    for(int i = 1; i <= M; i++)  {
			        for(int j = 1; j <= N; j++)  {
			            z = new Double(data.get_cij(i,j));
			            full_cxx[i][j].setText(z.toString());
						full_cxx[i][j].setFieldColor(data.cij_color[i][j]);
			        }
			    }
			}

			if(button == compute_button_4)
			{
			    double sum = 0.0;
			    for(int i=1; i<=M; i++)  {
			        for(int j=1; j<=N; j++)  {
			            Double x1 = Double.valueOf(full_dx[j].getText());
			            double sx1 = x1.doubleValue();
			            Double x2 = Double.valueOf(full_cxx[i][j].getText());
			            double sx2 = x2.doubleValue();
			            sum += sx1*sx2;
			        }
					SigFig s = new SigFig();
			        Double z = new Double(s.customround(sum,3));
			        sum = 0.0;
                    full_fx[i].setText(z.toString());
                    data.set_fi(i,sum);
			    }
				
				this.save();
			}

			if(button == default_button_5)
			{
				this.load();
				
			    fcoveri[1].setText("0.5");
			    fcoveri[2].setText("1.0");
			    fcoveri[3].setText("1.0");
				
				this.save();
			}

			if(button == update_button_5)
			{
			    // Reset dataClass state
	            this.load();

			    Double z;
			    for(int j = 1; j <= N; j++)
			        for(int i = 1; i <= M; i++)  {
						SigFig s = new SigFig();
			            z = new Double(s.customround(data.get_fij(i,j),3));
			            fijcover[i][j].setText(z.toString());
						fijcover[i][j].setFieldColor(data.fij_color[i][j]);
			            //fijcover[i][j].setText(fijr[i][j].getText());
			        }
			}

			if(button == compute_button_5)
			{
			    double sum;
			    for(int j = 1; j <= N; j++)  {
			        sum = 0.0;
			        for(int i = 1; i <= M; i++)  {
			            Double z1 = Double.valueOf(fijcover[i][j].getText());
			            Double z2 = Double.valueOf(fcoveri[i].getText());
			            sum += z1.doubleValue()*z2.doubleValue();
			        }
					SigFig s = new SigFig();
			        Double z = new Double(s.customround(sum,3));
			        dcoverj[j].setText(z.toString());
			    }
				
				this.save();
			}


			if(button == critique_button_2)
			{
			    String help_string = "";

			    // Check d1 and d2
			    for(int j = 1; j <= N; j++)  {
			        Double Sxx = Double.valueOf(dx[j].getText());
			        double sxx = Sxx.doubleValue();
			        Integer J = new Integer(j);
			        if(sxx > 1.0)  // all must be less than or equal to one
			            help_string = help_string + "d" + J.toString() + " must be less than or equal to one. ^^ ";
			    }

                // Interpret d1 and d2
			    for(int j = 1; j <= N; j++)  {
			        Double Sxx = Double.valueOf(dx[j].getText());
			        double sxx = Sxx.doubleValue();
			        Double z = new Double(1.0-sxx);
			        Integer J = new Integer(j);
			        if(j == 1)
			            help_string = help_string + "Yield with respect to defect 1 (placement) = " + z.toString() + " ^^ ";
			        if(j == 2)
			            help_string = help_string + "Yield with respect to defect 2 (broken wirebond) = " + z.toString() + " ^^ ";
			    }

			    // Post results of error check
			    Frame bogus_frame = new Frame();
                HelpDialog hd = new HelpDialog(bogus_frame,"dj Calculation",help_string,true);
                hd.popup();
				
				this.save();
			}

			if(button == critique_button_3)
			{
			    String help_string = "";

                double fijsum = 0.0;
                double djsum = 0.0;

			    for(int i=1; i<=M; i++)  {
			        for(int j=1; j<=N; j++)  {
			            Double x1 = Double.valueOf(dj[i][j].getText());
			            double sx1 = x1.doubleValue();
			            Double x2 = Double.valueOf(cij[i][j].getText());
			            double sx2 = x2.doubleValue();
                        fijsum += sx1*sx2;
			        }
			    }

			    Double z = new Double(fijsum);
			    help_string = help_string + "Sum of all the fij's = " + z.toString() + " ^^ ";

                for(int j=1; j<=N; j++)  {
    			    Double x1 = Double.valueOf(dj[1][j].getText());
    			    double sx1 = x1.doubleValue();
                    djsum += sx1;
			    }

			    z = new Double(djsum);
			    help_string = help_string + "Sum of all the dj's = " + z.toString() + " ^^ ";

			    if(Math.abs(djsum-fijsum) < 0.001)
			        help_string = help_string + "Defects are conserved since the sum of the fij's = sum of the dj's. ^^ ";
			    else
			    	help_string = help_string + "Defects are not conserved since the sum of the fij's is not equal to the sum of the dj's. ^^ ";

			    // Post results of error check
			    Frame bogus_frame = new Frame();
                HelpDialog hd = new HelpDialog(bogus_frame,"fij Calculation",help_string,true);
                hd.popup();
				
				this.save();
			}

            if(button == critique_button_4)
			{
			    String help_string = "";

                double fisum = 0.0;

			    for(int i=1; i<=M; i++)  {
			        Double x1 = Double.valueOf(full_fx[i].getText());
			        double sx1 = x1.doubleValue();
                    fisum += sx1;
			    }

			    Double z = new Double(fisum);
			    help_string = help_string + "Sum of all the fi's = " + z.toString() + " ^^ ";

                SigFig s = new SigFig();
			    z = new Double(s.customround((1.0-fisum)*100.0,1));
			    help_string = help_string + "Yield computed from the fi's is " + z.toString() + "%.";

			    // Post results of error check
			    Frame bogus_frame = new Frame();
                HelpDialog hd = new HelpDialog(bogus_frame,"fij Calculation",help_string,true);
                hd.popup();
				
				this.save();
			}
			
			if(button == critique_button_5)
			{
			    String help_string = "";

				// Check the fault coverages
			    for(int i=1; i<=M; i++)  {
			        Double x1 = Double.valueOf(fcoveri[i].getText());
			        double sx1 = x1.doubleValue();
					Integer z = new Integer(i);
                    if(sx1 < 0.0)
						help_string = help_string + "Fault Coverage " + z.toString() + " must be equal to or greater than 0." + " ^^ ";
					if(sx1 > 1.0)
						help_string = help_string + "Fault Coverage " + z.toString() + " must be equal to or less than 1." + " ^^ ";
				}
				
				// Interpret defect coverages
				help_string = help_string + " ^^ ";
				double sum = 0.0;
				double sum_dj = 0.0;
				for(int j=1; j<=N; j++)  {
					Double x1 = Double.valueOf(dcoverj[j].getText());
			        double sx1 = 100.0*x1.doubleValue()/data.get_dj(j);
					Double z = new Double(sx1);
					Integer jj = new Integer(j);
					help_string = help_string + z.toString() + "% of all defects of type " + jj.toString() + " are detected by these tests." + " ^^ ";
					sum += x1.doubleValue();
					sum_dj += data.get_dj(j);
				}
				
				// Get total defect coverage
				double x = 100.0*sum/sum_dj;
				SigFig s = new SigFig();
				Double z = new Double(s.customround(x,2));
				help_string = help_string + z.toString() + "% of all defects are detected. ^^ ";
				
			    // Post results of error check
			    Frame bogus_frame = new Frame();
                HelpDialog hd = new HelpDialog(bogus_frame,"Fault Coverage",help_string,true);
                hd.popup();
				
				this.save();
			}

			if(button == compute_button_s)
			{
				// Get inputs
				Double ci = Double.valueOf(costin.getText());
				double yi = yieldin.getValue()/100.0;
				Double lr = Double.valueOf(labor_rate.getText());
				Double lb = Double.valueOf(labor_burden.getText());
				Double dep = Double.valueOf(depreciation.getText());
				Double q = Double.valueOf(quantity.getText());
				Double hpy = Double.valueOf(hours_per_year.getText());
				Double t1 = Double.valueOf(time1.getText());
				Double matc1 = Double.valueOf(mat_cost1.getText());	
				Double machc1 = Double.valueOf(mach_cost1.getText());
				Double machcap1 = Double.valueOf(mach_cap1.getText());
				Double machprog1 = Double.valueOf(mach_prog1.getText());
				Double d1 = Double.valueOf(defects1.getText());
				Double mc1 = Double.valueOf(mat_cost1.getText());
				double ou1 = op_util1.getValue()/100.0;
				double mu1 = mach_util1.getValue()/100.0;
				Double t2 = Double.valueOf(time2.getText());
				Double matc2 = Double.valueOf(mat_cost2.getText());	
				Double machc2 = Double.valueOf(mach_cost2.getText());
				Double machcap2 = Double.valueOf(mach_cap2.getText());
				Double machprog2 = Double.valueOf(mach_prog2.getText());
				Double d2 = Double.valueOf(defects2.getText());
				Double mc2 = Double.valueOf(mat_cost2.getText());
				double ou2 = op_util2.getValue()/100.0;
				double mu2 = mach_util2.getValue()/100.0;
				
				// Compute outputs
				double y1 = (1.0-d1.doubleValue()/1.0e6);
				double y2 = (1.0-d2.doubleValue()/1.0e6);
				double yo = yi*y1*y2;
				
				double co = ci.doubleValue();
				
				// Step 1
				double labor_cost = ou1*lr.doubleValue()*lb.doubleValue()*t1.doubleValue()/60.0/60.0/machcap1.doubleValue();
				System.out.println("Step 1: ou1 = " + ou1 + ", t1 = " + t1.doubleValue() + ", lb = " + lb.doubleValue() + ", machcap = " + machcap1.doubleValue());
				double machine_cost = machc1.doubleValue()*t1.doubleValue()/60.0/60.0/machcap1.doubleValue()/(mu1*hpy.doubleValue())/dep.doubleValue();
				double material_cost = matc1.doubleValue();
				double tooling_cost = machprog1.doubleValue()/q.doubleValue();
				co += labor_cost+machine_cost+material_cost+tooling_cost;
				System.out.println("Step 1: labor_cost = " + labor_cost + ", mach cost = " + machine_cost + ", mat cost = " + material_cost + ", tool cost = " + tooling_cost);
			
				// Step 2
				labor_cost = ou2*lr.doubleValue()*lb.doubleValue()*t2.doubleValue()/60.0/60.0/machcap2.doubleValue();
				machine_cost = machc2.doubleValue()*t2.doubleValue()/60.0/60.0/machcap2.doubleValue()/(mu2*hpy.doubleValue())/dep.doubleValue();
				material_cost = matc2.doubleValue();
				tooling_cost = machprog2.doubleValue()/q.doubleValue();
				co += labor_cost+machine_cost+material_cost+tooling_cost;
				System.out.println("Step 2: labor_cost = " + labor_cost + ", mach cost = " + machine_cost + ", mat cost = " + material_cost + ", tool cost = " + tooling_cost);

				// Put results back into fields
				SigFig s = new SigFig();
			    Double z = new Double(s.customround(co,3));
				costout.setText(z.toString());
				z = new Double(s.customround(yo,4));
				yieldout.setText(z.toString());
			}
			
			if(button == default_button_s)
			{
				costin.setText("10.0");
				yieldin.setValue(90);
				labor_rate.setText("20.0");
				labor_burden.setText("1.8");
				depreciation.setText("5.0");
				quantity.setText("100000");
				hours_per_year.setText("6000.0");
				mat_cost1.setText("0.0");
				time1.setText("0.5");
				mach_cost1.setText("150000");
				mach_prog1.setText("5000");
				mach_cap1.setText("1");
				mach_util1.setValue(65);
				op_util1.setValue(50);
				defects1.setText("20.0");
				mat_cost2.setText("1.0");
				time2.setText("10.0");
				mach_cost2.setText("50000");
				mach_prog2.setText("0.0");
				mach_cap2.setText("8");
				mach_util2.setValue(45);
				op_util2.setValue(25);
				defects2.setText("50.0");
			} 
						
			if(button == compute_button_p)
			{
				// Get inputs
				Double ci = Double.valueOf(costin.getText());
				double yi = yieldin.getValue()/100.0;
				Double ct = Double.valueOf(costtest.getText());
				double yt = yieldtest.getValue()/100.0;
				double fc = fctest.getValue()/100.0;
				int i = yieldchoice.getSelectedIndex();
				
				if(i == 0) {   // yield hit into (before) test
					// check to make sure yt is not 0
					if(yt == 0)  {
						System.out.println("**ERROR** test yield can not be zero if it applies to before the test.");
						yt = 0.001;
					}
					yi = yi*yt;
				}

				// Compute outputs
				double yo = Math.pow(yi,(1.0-fc));
				if(i == 1)  // yield hit out of (after) test
					yo = yo*yt;
				double p = Math.pow(yi,fc);
				double sc = 1.0-p;
				double co = (ci.doubleValue() + ct.doubleValue())/p;
			
				// Put results back into fields
				SigFig s = new SigFig();
			    Double z = new Double(s.customround(co,3));

				costout.setText(z.toString());
				z = new Double(s.customround(yo,4));
				yieldout.setText(z.toString());
				z = new Double(s.customround(p,3));
				pass.setText(z.toString());
				z = new Double(s.customround(sc,3));
				scrap.setText(z.toString());
				
				// Add point to the plot
				if(sp != null)
					sp.addPoint(fc*100,yo*100);
			}
			
			// START - TEST ECONOMICS
			if(button == compute_button_cp)
			{
				// Set cursor to hourglass
				this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
				
				double cid;
				double ctd;
				double cdd;
				double crd;
				double yid;
				double ytbd;
				double ytad;
				double yrd;
				double ydd;
				double fcd = 1.0;
				double fpd;
				double frd;
				double fdd;
				double yo = 1.0;
				double co = 0.0;
				double p = 1.0;
				double sc = 0.0;
				
				// Clear output fields
				costout.setText("");
				yieldout.setText("");
				pass.setText("");
				scrap.setText("");
				costout_sd.setText("");
				yieldout_sd.setText("");
				pass_sd.setText("");
				scrap_sd.setText("");
				
				// Get inputs from interface
				Double ci = Double.valueOf(costin.getText());
				Double ct = Double.valueOf(costtest.getText());
				Double cd = Double.valueOf(costdiag.getText());
				Double cr = Double.valueOf(costrew.getText());
				costin_data.setdvalue(ci.doubleValue());
				costtest_data.setdvalue(ct.doubleValue());
				costdiag_data.setdvalue(cd.doubleValue());
				costrew_data.setdvalue(cr.doubleValue());
				double yi = yieldin.getValue()/100.0;
				double yr = yieldrew.getValue()/100.0;
				double yd = yielddiag.getValue()/100.0;
				double ytb = yieldbefore.getValue()/100.0;
				double yta = yieldafter.getValue()/100.0;
				yieldin_data.setdvalue(yi);
				yieldrew_data.setdvalue(yr);
				yielddiag_data.setdvalue(yd);
				yieldbefore_data.setdvalue(ytb);
				yieldafter_data.setdvalue(yta);
				double fc = fctest.getValue()/100.0;
				double fp = fpos.getValue()/100.0;
				double fdiag = fd.getValue()/100.0;
				double frew = fr.getValue()/100.0;
				fc_data.setdvalue(fc);
				fp_data.setdvalue(fp);
				fd_data.setdvalue(fdiag);
				fr_data.setdvalue(frew);
				int n = loops.getValue();
				int ifp = fpchoice.getSelectedIndex();
				
				// Monte Carlo Loop
				if(mc_choice.getSelectedIndex() == 1)
					monte_boolean = true;
				co_array = new double[monte.number_runs()];
				yo_array = new double[monte.number_runs()];
				yc_array = new double[monte.number_runs()];
				sc_array = new double[monte.number_runs()];
				for(int ii = 0; ii < monte.number_runs(); ii++)  {

					// Set values
					cid = costin_data.distribution_value(this);
					ctd = costtest_data.distribution_value(this);
					cdd = costdiag_data.distribution_value(this);
					crd = costrew_data.distribution_value(this);
					yid = yieldin_data.distribution_value(this);
					ydd = yielddiag_data.distribution_value(this);
					yrd = yieldrew_data.distribution_value(this);
					ytbd = yieldbefore_data.distribution_value(this);
					ytad = yieldafter_data.distribution_value(this);
					fcd = fc_data.distribution_value(this);;
					fpd = fp_data.distribution_value(this);;
					frd = fr_data.distribution_value(this);;
					fdd = fd_data.distribution_value(this);;
				
					// Check the inputs
				/*	System.out.println("Inputs:");
					System.out.println("ci = " + cid);
					System.out.println("ct = " + ctd);
					System.out.println("cd = " + cdd);
					System.out.println("cr = " + crd);
					System.out.println("yi = " + yid);
					System.out.println("yr = " + yrd);
					System.out.println("yd = " + ydd);
					System.out.println("ytb = " + ytbd);
					System.out.println("yta = " + ytad);
					System.out.println("fc = " + fcd);
					System.out.println("fp = " + fpd);
					System.out.println("fdiag = " + fdd);
					System.out.println("frew = " + frd);
					System.out.println("n = " + n);
					System.out.println("ifp = " + ifp);
					System.out.println("");  */
				
					// Initialize the process
					double cin = cid;
					double yin = yid;
					double norig = 100; //50000;  // solution is independent of this number (it must be > 0)
					double nin = norig;  
					yo = 1.0;
					co = 0.0;
					p = 1.0;
					sc = 0.0;
				
					// Rework loop
					double c1 = 0.0;
					double c2 = 0.0;
					double c3 = 0.0;
					double c4;
					double cinorig = 0.0;
					double ninorig = 0.0;
					double yinorig = 1.0;
					double nsum = 0.0;
					double y = 0.0;  // this is correct
					double Nout;
					double Ns1;
					double Ns2;
					double Nd1 = 0.0;
					for(int i = 0; i <= n; i++)  {
						if(ifp == 0)  {  // fp affects all parts
							Nout = nin*(1.0-fpd)*composite(ifp,fpd,yin,ytbd,fcd);
							Nd1 = nin*(1.0-fpd) - Nout + fpd*nin*(1.0-yin*ytbd);
					/*		System.out.println("Nd1 = " + Nd1);
							System.out.println("nin = " + nin);
							System.out.println("Nout = " + Nout);
							System.out.println("fpd = " + fpd);
							System.out.println("yin = " + yin);
							System.out.println("ytbd = " + ytbd);  */
						}  else  {  //fp affects only good parts
							Nout = nin*(1.0-fpd*yin*ytbd)*composite(ifp,fpd,yin,ytbd,fcd);
							Nd1 = nin*(1.0-fpd*yin*ytbd) - Nout;
						}
						Ns1 = (1.0-fdd)*Nd1;
						Ns2 = (1.0-frd)*fdd*Nd1;
						
				/*		System.out.println("");
						System.out.println("rework interation i = " + i);
						System.out.println("cin = " + cin);
						System.out.println("yin = " + yin);
						System.out.println("nin = " + nin);
						System.out.println("Nout = " + Nout);
						System.out.println("Nd1 = " + Nd1);
						System.out.println("Ns1 = " + Ns1);
						System.out.println("Ns2 = " + Ns2);  */
						
						c1 += (cin + ctd)*Nout;
						if(n != 0 && i != n)  {
							c2 += (cin + ctd + cdd)* Ns1;
							c3 += (cin + ctd + cdd + crd)* Ns2;
						}
						y += ytad*Nout*composite(ifp,fpd,yin,ytbd,1.0-fcd);
						nsum += Nout;
						
						// Prepare for next iteration
						// keep original values for c4 calculation if necessary
						ninorig = nin;
						cinorig = cin;
						yinorig = yin;
						nin = fpd*yin*ytbd*nin+frd*fdd*Nd1;
						if(nin != 0.0)  {
							cin = ((cin + ctd + cdd)*fpd*yin*ytbd*ninorig + (cin + ctd + cdd + crd)*frd*fdd*Nd1)/nin;
							yin = (fpd*yin*ytbd*ninorig + yrd*frd*fdd*Nd1)/nin;
						}  else  {  // not relevant what these numbers are
							cin = 0.0;
							yin = 1.0;
						}
					}
				
					c4 = (cinorig + ctd)*Nd1;  // last iteration test scraps
					System.out.println("Nd1 = " + Nd1);
					if(ifp == 0) { // fp affects all parts
						//System.out.println("c4 in = " + c4);
						//c4 += (cinorig + ctd)*ninorig*fpd;  // last iteration false positives
						c4 += (cinorig + ctd)*ninorig*yinorig*ytbd*fpd;  // last iteration false positives that are not actually defective
						// same as only good parts case because the first pass at C4 already scooped up the truely bad parts
					/*	System.out.println("cinorig = " + cinorig);
						System.out.println("ctd = " + ctd);
						System.out.println("ninorig = " + ninorig);
						System.out.println("fpd = " + fpd);  */
					}
					else  // fp only affects good parts
						c4 += (cinorig + ctd)*ninorig*yinorig*ytbd*fpd;  // last iteration false positives
					co = (c1 + c2 + c3 + c4)/nsum;
					yo = y/nsum;
					sc = (norig-nsum)/norig;
					p = 1.0-sc;
				
				/*	System.out.println("c1 = " + c1);
					System.out.println("c2 = " + c2);
					System.out.println("c3 = " + c3);
					System.out.println("c4 = " + c4);
					System.out.println("nsum = " + nsum);
					System.out.println("y = " + y);   */
					
					// Record solutions in arrays for post processing later
					co_array[ii] = co;
					yo_array[ii] = yo;
					yc_array[ii] = co/yo;
					sc_array[ii] = sc;
				}
			
				// Put results back into fields
				SigFig s = new SigFig();
			    Double z = new Double(s.customround(mean(co_array),3));
				costout.setText(z.toString());
				z = new Double(s.customround(stddev(co_array),3));
				costout_sd.setText(z.toString());
				z = new Double(s.customround(mean(yo_array),4));
				yieldout.setText(z.toString());
				z = new Double(s.customround(stddev(yo_array),4));
				yieldout_sd.setText(z.toString());
				z = new Double(s.customround(1.0-mean(sc_array),3));
				pass.setText(z.toString());
				z = new Double(s.customround(mean(sc_array),3));
				scrap.setText(z.toString());
				z = new Double(s.customround(stddev(sc_array),3));
				scrap_sd.setText(z.toString());
				pass_sd.setText(z.toString()); // same as scrap
				
				System.out.println("Yielded Cost (mean) = " + s.customround(mean(yc_array),3));
				System.out.println("Yielded Cost (standard deviation) = " + s.customround(stddev(yc_array),3));
				System.out.println("");
				
				// Add point to the plot
				if(sp != null)
					sp.addPoint(fcd*100,yo*100);
				
				// Reset the cursor
				this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
			// END - TEST ECONOMICS

			if(button == default_button_p)
			{
				costin.setText("10.0");
				yieldin.setValue(90);
				costtest.setText("20.0");
				yieldtest.setValue(99);
				fctest.setValue(80);
				yieldchoice.select(0);
			}
			
			// DEFAULTS - TEST ECONOMICS
			if(button == default_button_cp)
			{
				// Set cursor to hourglass
				this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
				
				fc_data.setallvalues(2,0.7,-1,"Fault Coverage (frac)",1,0.7,0.8,0.1);
				fctest.setValue((int)(100*fc_data.doublevalue()));
				
				fr_data.setallvalues(2,0.81,-1,"Repair Fraction (frac)",1,0.78,0.9,0.05);
				fr.setValue((int)(100*fr_data.doublevalue()));
				
				fd_data.setallvalues(2,1.0,-1,"Diagnosis Fraction (frac)",1,0.92,1.0,0.1);
				fd.setValue((int)(100*fd_data.doublevalue()));
				
				fp_data.setallvalues(2,0.1,-1,"False Positives (frac)",1,0.05,0.15,0.01);
				fpos.setValue((int)(100*fp_data.doublevalue()));

				yieldin_data.setallvalues(2,0.9,-1,"Input Yield (frac)",0,0.8,1.0,0.1);
				yieldin.setValue((int)(100*yieldin_data.doublevalue()));
			
				yieldbefore_data.setallvalues(2,0.97,-1,"Before Test Yield (frac)",1,0.9,1.0,0.1);
				yieldbefore.setValue((int)(100*yieldbefore_data.doublevalue()));
			
				yieldafter_data.setallvalues(2,0.97,-1,"After Test Yield (frac)",1,0.9,1.0,0.1);
				yieldafter.setValue((int)(100*yieldafter_data.doublevalue()));
			
				yieldrew_data.setallvalues(2,0.9,-1,"Rework Yield (frac)",1,0.8,1.0,0.1);
				yieldrew.setValue((int)(100*yieldrew_data.doublevalue()));
				
				costin_data.setallvalues(2,100.0,-1,"Input Cost",0,5.0,15.0,1.0);
				Double z = new Double(costin_data.doublevalue());
				costin.setText(z.toString());
				
				costtest_data.setallvalues(2,20.0,-1,"Test Cost",1,10.0,22.0,2.0);
				z = new Double(costtest_data.doublevalue());
				costtest.setText(z.toString());
				
				costdiag_data.setallvalues(2,10.0,-1,"Diagnosis Cost",1,5.0,15.0,1.0);
				z = new Double(costdiag_data.doublevalue());
				costdiag.setText(z.toString());
				
				costrew_data.setallvalues(2,25.0,-1,"Rework Cost",1,35.0,45.0,3.0);
				z = new Double(costrew_data.doublevalue());
				costrew.setText(z.toString());
				
				fpchoice.select(1);
				monte_boolean = false;
				mc_choice.select(0);
				number_monte_points = 1000;
				Integer zi = new Integer(number_monte_points);
				monte_runs.setText(zi.toString());
				
				// Reset the cursor
				this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
			}
			
			if(button == critique_button_p)
			{
			    String help_string = "";

			    // Get inputs
				Double ci = Double.valueOf(costin.getText());
				double yi = yieldin.getValue()/100.0;
				Double ct = Double.valueOf(costtest.getText());
				double yt = yieldtest.getValue()/100.0;
				double fc = fctest.getValue()/100.0;
				int i = yieldchoice.getSelectedIndex();
				
				// Check values
				if(ci.doubleValue() < 0.0)
					help_string = "Input cost must be greater than or equal to 0.0." + " ^^ ";
			    if(yi < 0.0 || yi > 1.0)
					help_string = "Input yield must be between 0 and 100 inclusive." + " ^^ ";
				if(ct.doubleValue() < 0.0)
					help_string = "Test cost must be greater than or equal to 0.0." + " ^^ ";
				if(fc < 0.0 || fc > 1.0)
					help_string = "Fault coverage must be between 0 and 100 inclusive." + " ^^ ";
				if(yt < 0.0 || yt > 1.0)
					help_string = "Test yield must be between 0 and 100 inclusive." + " ^^ ";

			    // Check for no errors
			    if(help_string.length() < 5)
			        help_string = "No errors detected.";

			    // Post results of error check
			    Frame bogus_frame = new Frame();
                HelpDialog hd = new HelpDialog(bogus_frame,"Test Process Step",help_string,true);
                hd.popup();
			}
			
			if(button == plot)
			{
				// Scatter plot
				if(sp == null)
					sp = new ScatterPlot(this,"");
				sp.loadScatterPlotData("Fault Coverage (%)","Output Yield (%)",100.0,0.0,100.0,0.0);
				sp.popup();
			}
			
			// Make bar chart
			if(button == bar_plot)
			{
			    double stddev = 4;
			    int number = this.monte.number_runs()/100;
				if(number > 20)
					number = 20;

				String[] labels = new String[this.monte.number_runs()+1];
			    double[] values = new double[this.monte.number_runs()+1];

				// Get data
			    if(!barchart_data("Yielded Cost",values,labels,stddev,number))  {
			        System.out.println("No distribution exists for the selected plot variable.");
					return(false);
			    }

				// Plot barchart
			    BarChart barchart = null;
			    barchart = new BarChart(this,"Probablity Distribution");
			    barchart.loadBarData(number,labels,values,"Yielded Cost","Probability");
			    barchart.popup();
			    barchart.drawBarChart();
			}

			return(true);
		}
		return(false);
	}
	
	private void build_solution()  {
		
		Choice sol_choice = new Choice();
		sol_choice.add("Basic Tutorial");
		sol_choice.add("Problem #1 Hints");
		sol_choice.add("Problem #1 Solution");
		sol_choice.add("Problem #2 Hints");
		sol_choice.add("Problem #2 Solution");
		sol_choice.add("Problem #3 Hints");
		sol_choice.add("Problem #3 Solution");
		
		Panel sol_master = new Panel();
		sol_master.add(new Label("Default:"));
		sol_master.add(sol_choice);
		
		sol_master.setBackground(Color.lightGray);

		add(sol_master);
		
		return;
	}
	
	private void build_simpleprocess()  {
		
		Panel proc_master = new Panel();
		GridBagLayout gblmp = new GridBagLayout();
		proc_master.setLayout(gblmp);
		GridBagConstraints gbcmp = new GridBagConstraints();
		gbcmp.fill = GridBagConstraints.BOTH;
        gbcmp.weightx = 1.0;  
		
		// Build fields
		costin = new myTextField(this,blank);
		costout = new myTextField(this,blank);
		yieldout = new myTextField(this,blank);
		time1 = new myTextField(this,blank);
		defects1 = new myTextField(this,blank);
		mach_cap1 = new myTextField(this,blank);
		mach_prog1 = new myTextField(this,blank);
		mach_cost1 = new myTextField(this,blank);
		mat_cost1 = new myTextField(this,blank);
		time2 = new myTextField(this,blank);
		defects2 = new myTextField(this,blank);
		mach_cap2 = new myTextField(this,blank);
		mach_prog2 = new myTextField(this,blank);
		mach_cost2 = new myTextField(this,blank);
		mat_cost2 = new myTextField(this,blank);
		labor_rate = new myTextField(this,blank);
		labor_burden = new myTextField(this,blank);
		depreciation = new myTextField(this,blank);
		quantity = new myTextField(this,blank);
		hours_per_year = new myTextField(this,blank);
		
		// Build slide bars
		// documentation at http://www.mindspring.com/~rcleah/bob_slide.html
		yieldin = new SlideBar();
		yieldin.setMinimum(1);
		yieldin.setMaximum(100);
		op_util1 = new SlideBar();
		op_util1.setMinimum(0);
		op_util1.setMaximum(100);
		op_util2 = new SlideBar();
		op_util2.setMinimum(0);
		op_util2.setMaximum(100);
		mach_util1 = new SlideBar();
		mach_util1.setMinimum(0);
		mach_util1.setMaximum(100);
		mach_util2 = new SlideBar();
		mach_util2.setMinimum(0);
		mach_util2.setMaximum(100);
		
		// Build process step 1 box
		PanelFormat pf = new PanelFormat();
		Panel step_panel1 = new Panel();
		step_panel1.setBackground(Color.green);
		pf.initializePanel(step_panel1);
		pf.Subtitle(step_panel1,"  Process Step #1");
		pf.LabelField(step_panel1,new Label("Time (sec/part)"),time1);
		pf.LabelSlide(step_panel1,new Label("Operator Utilization (%)"),op_util1);
		pf.LabelField(step_panel1,new Label("Material Cost ($/part)"),mat_cost1);
		pf.LabelField(step_panel1,new Label("Tooling ($)"),mach_prog1);
		pf.LabelField(step_panel1,new Label("Machine Capacity (parts)"),mach_cap1);
		pf.LabelField(step_panel1,new Label("Machine Cost ($)"),mach_cost1);
		pf.LabelSlide(step_panel1,new Label("Machine Utilization (%)"),mach_util1);
		pf.LabelField(step_panel1,new Label("Defects (ppm)"),defects1);
		
		// Build process step 2 box
		Panel step_panel2 = new Panel();
		step_panel2.setBackground(Color.green);
		pf.initializePanel(step_panel2);
		pf.Subtitle(step_panel2,"  Process Step #2");
		pf.LabelField(step_panel2,new Label("Time (sec/part)"),time2);
		pf.LabelSlide(step_panel2,new Label("Operator Utilization (%)"),op_util2);
		pf.LabelField(step_panel2,new Label("Material Cost ($/part)"),mat_cost2);
		pf.LabelField(step_panel2,new Label("Tooling ($)"),mach_prog2);
		pf.LabelField(step_panel2,new Label("Machine Capacity (parts)"),mach_cap2);
		pf.LabelField(step_panel2,new Label("Machine Cost ($)"),mach_cost2);
		pf.LabelSlide(step_panel2,new Label("Machine Utilization (%)"),mach_util2);
		pf.LabelField(step_panel2,new Label("Defects (ppm)"),defects2);
		
		// Incoming box
		PanelFormat pf1 = new PanelFormat();
		Panel in_panel = new Panel();
		pf1.initializePanel(in_panel);
		pf1.LabelField(in_panel,new Label("Incoming cost"),costin);
		pf1.LabelSlide(in_panel,new Label("Incoming yield (%)"),yieldin);

		// Output box
		PanelFormat pf2 = new PanelFormat();
		Panel out_panel = new Panel();
		pf2.initializePanel(out_panel);
		pf2.LabelField(out_panel,new Label("Outgoing cost"),costout);
		pf2.LabelField(out_panel,new Label("Outgoing yield (%)"),yieldout);
		
		// Infrastructure panel
		PanelFormat pf2x = new PanelFormat();
		Panel infra_panel = new Panel();
		pf2x.initializePanel(infra_panel);
		pf2x.Subtitle(infra_panel,"Factory Details:");
		pf2x.LabelField(infra_panel,new Label("Lifetime Quantity of Parts"),quantity);
		pf2x.LabelField(infra_panel,new Label("Labor Rate ($/hr)"),labor_rate);
		pf2x.LabelField(infra_panel,new Label("Labor Burden (multiplier)"),labor_burden);
		pf2x.LabelField(infra_panel,new Label("Working Hours per Year"),hours_per_year);
		pf2x.LabelField(infra_panel,new Label("Depreciation (years)"),depreciation);
		Panel scrap_panel1 = new Panel();
		//scrap_panel1.setLayout(new GridLayout(3,1));
    	//scrap_panel1.add(new Label(""));
		scrap_panel1.add(infra_panel);
		//scrap_panel1.add(new Label(""));
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(in_panel,gbcmp);
		proc_master.add(in_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l = new Label("");
		gblmp.setConstraints(l,gbcmp);
		proc_master.add(l);
		
		MyCanvas3 cf22 = new MyCanvas3(this,"downarrow.gif",90,0);
        cf22.setBackground(Color.white);
        cf22.resize(20,90);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf22,gbcmp);
		proc_master.add(cf22);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(step_panel1,gbcmp);
		proc_master.add(step_panel1);
		//MyCanvas3 cf23 = new MyCanvas3(this,"rightarrow.gif",0,10);
        //cf23.setBackground(Color.white);
        //cf23.resize(100,75);
		//gblmp.setConstraints(cf23,gbcmp);
		//proc_master.add(cf23);
		Label lxx = new Label("     ");
		gblmp.setConstraints(lxx,gbcmp);
		proc_master.add(lxx);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(scrap_panel1,gbcmp);
		proc_master.add(scrap_panel1);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		
		MyCanvas3 cf21 = new MyCanvas3(this,"downarrow.gif",90,0);
        cf21.setBackground(Color.white);
        cf21.resize(20,95);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf21,gbcmp);
		proc_master.add(cf21);
		
		
		
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(step_panel2,gbcmp);
		proc_master.add(step_panel2);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l1x = new Label("");
		gblmp.setConstraints(l1x,gbcmp);
		proc_master.add(l1x);
		
		MyCanvas3 cf21x = new MyCanvas3(this,"downarrow.gif",90,0);
        cf21x.setBackground(Color.white);
        cf21x.resize(20,95);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf21x,gbcmp);
		proc_master.add(cf21x);
		
		
		
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(out_panel,gbcmp);
		proc_master.add(out_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l1 = new Label("");
		gblmp.setConstraints(l1,gbcmp);
		proc_master.add(l1);
		
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l3 = new Label("");
		gblmp.setConstraints(l3,gbcmp);
		proc_master.add(l3);
		
		Panel button_panel = new Panel();
		button_panel.add(default_button_s);
		button_panel.add(critique_button_s);
		button_panel.add(compute_button_s);
		//button_panel.add(plot);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(button_panel,gbcmp);
		proc_master.add(button_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l2 = new Label("");
		gblmp.setConstraints(l2,gbcmp);
		proc_master.add(l2);
		
		add(proc_master);
								
		invalidate();
		validate();		//forces repaint
			
		return;
	}
	
	private void build_process()  {
		
		Panel proc_master = new Panel();
		GridBagLayout gblmp = new GridBagLayout();
		proc_master.setLayout(gblmp);
		GridBagConstraints gbcmp = new GridBagConstraints();
		gbcmp.fill = GridBagConstraints.BOTH;
        gbcmp.weightx = 1.0;  
		
		// Build fields
		costin = new myTextField(this,blank);
		costout = new myTextField(this,blank);
		yieldout = new myTextField(this,blank);
		pass = new myTextField(this,blank);
		costtest = new myTextField(this,blank);
		scrap = new myTextField(this,blank);
		
		// Build slide bars
		// documentation at http://www.mindspring.com/~rcleah/bob_slide.html
		yieldin = new SlideBar();
		yieldin.setMinimum(1);
		yieldin.setMaximum(100);
		yieldtest = new SlideBar();
		yieldtest.setMinimum(0);
		yieldtest.setMaximum(100);
		fctest = new SlideBar();
		fctest.setMinimum(0);
		fctest.setMaximum(100);
		
		// Build choice
		yieldchoice = new Choice();
		yieldchoice.add("into test");
		yieldchoice.add("out of test");
		
		// Build test step box
		PanelFormat pf = new PanelFormat();
		Panel step_panel = new Panel();
		step_panel.setBackground(Color.green);
		pf.initializePanel(step_panel);
		pf.LabelField(step_panel,new Label("Test cost"),costtest);
		pf.LabelSlide(step_panel,new Label("Fault coverage (%)"),fctest);
		pf.LabelSlide(step_panel,new Label("Test yield (%)"),yieldtest);
		pf.LabelChoice(step_panel,new Label("  Yield is: "),yieldchoice);
		
		// Incoming box
		PanelFormat pf1 = new PanelFormat();
		Panel in_panel = new Panel();
		pf1.initializePanel(in_panel);
		pf1.LabelField(in_panel,new Label("Incoming cost"),costin);
		pf1.LabelSlide(in_panel,new Label("Incoming yield (%)"),yieldin);

		// Output box
		PanelFormat pf2 = new PanelFormat();
		Panel out_panel = new Panel();
		pf2.initializePanel(out_panel);
		pf2.LabelField(out_panel,new Label("Outgoing cost"),costout);
		pf2.LabelField(out_panel,new Label("Outgoing yield (%)"),yieldout);
		pf2.LabelField(out_panel,new Label("Pass fraction (%)"),pass);
		
		// Scrap panel
		Panel scrap_panel = new Panel();
		scrap_panel.add(new Label("Scrap fraction"));
		scrap_panel.add(scrap);
		
		Panel scrap_panel1 = new Panel();
		scrap_panel1.setLayout(new GridLayout(3,1));
    	scrap_panel1.add(new Label(""));
		scrap_panel1.add(scrap_panel);
		scrap_panel1.add(new Label(""));
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(in_panel,gbcmp);
		proc_master.add(in_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l = new Label("");
		gblmp.setConstraints(l,gbcmp);
		proc_master.add(l);
		
		MyCanvas3 cf22 = new MyCanvas3(this,"downarrow.gif",90,0);
        cf22.setBackground(Color.white);
        cf22.resize(20,90);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf22,gbcmp);
		proc_master.add(cf22);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(step_panel,gbcmp);
		proc_master.add(step_panel);
		MyCanvas3 cf23 = new MyCanvas3(this,"rightarrow.gif",0,10);
        cf23.setBackground(Color.white);
        cf23.resize(100,75);
		gblmp.setConstraints(cf23,gbcmp);
		proc_master.add(cf23);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(scrap_panel1,gbcmp);
		proc_master.add(scrap_panel1);
		
		MyCanvas3 cf21 = new MyCanvas3(this,"downarrow.gif",90,0);
        cf21.setBackground(Color.white);
        cf21.resize(20,95);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf21,gbcmp);
		proc_master.add(cf21);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(out_panel,gbcmp);
		proc_master.add(out_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l1 = new Label("");
		gblmp.setConstraints(l1,gbcmp);
		proc_master.add(l1);
		
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l3 = new Label("");
		gblmp.setConstraints(l3,gbcmp);
		proc_master.add(l3);
		
		Panel button_panel = new Panel();
		button_panel.add(default_button_p);
		button_panel.add(critique_button_p);
		button_panel.add(compute_button_p);
		button_panel.add(plot);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(button_panel,gbcmp);
		proc_master.add(button_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l2 = new Label("");
		gblmp.setConstraints(l2,gbcmp);
		proc_master.add(l2);
		
		add(proc_master);
								
		invalidate();
		validate();		//forces repaint
			
		return;
	}

	private void build_complexprocess()  {
		
		Panel proc_master = new Panel();
		GridBagLayout gblmp = new GridBagLayout();
		proc_master.setLayout(gblmp);
		GridBagConstraints gbcmp = new GridBagConstraints();
		gbcmp.fill = GridBagConstraints.BOTH;
        gbcmp.weightx = 1.0;  
		
		// Build fields
		costin = new myTextField(this,costin_data,blank);
		costout = new myTextField(this,blank);
		yieldout = new myTextField(this,blank);
		pass = new myTextField(this,blank);
		costtest = new myTextField(this,costtest_data,blank);
		costdiag = new myTextField(this,costdiag_data,blank);
		costrew = new myTextField(this,costrew_data,blank);
		scrap = new myTextField(this,blank);
		diagscrap = new myTextField(this,blank);
		rewscrap = new myTextField(this,blank);
		
		costout_sd = new myTextField(this,blank);
		yieldout_sd = new myTextField(this,blank);
		pass_sd = new myTextField(this,blank);
		scrap_sd = new myTextField(this,blank);
		
		// Build slide bars
		yieldin = new mySlideBar(this,yieldin_data);
		yieldin.setMinimum(1);
		yieldin.setMaximum(100);
		yieldin.setBackgroundColor(Color.lightGray);
		yieldbefore = new mySlideBar(this,yieldbefore_data);
		yieldbefore.setMinimum(1);
		yieldbefore.setMaximum(100);
		yieldbefore.setBackgroundColor(Color.white);
		yieldafter = new mySlideBar(this,yieldafter_data);
		yieldafter.setMinimum(0);
		yieldafter.setMaximum(100);
		yieldafter.setBackgroundColor(Color.white);
		yielddiag = new mySlideBar(this,yielddiag_data);
		yielddiag.setMinimum(0);
		yielddiag.setMaximum(100);
		yielddiag.setBackgroundColor(Color.white);
		yieldrew = new mySlideBar(this,yieldrew_data);
		yieldrew.setMinimum(0);
		yieldrew.setMaximum(100);
		yieldrew.setBackgroundColor(Color.white);
		fctest = new mySlideBar(this,fc_data);
		fctest.setMinimum(0);
		fctest.setMaximum(100);
		fctest.setBackgroundColor(Color.white);
		fd = new mySlideBar(this,fd_data);
		fd.setMinimum(0);
		fd.setMaximum(100);
		fd.setBackgroundColor(Color.white);
		fr = new mySlideBar(this,fr_data);
		fr.setMinimum(0);
		fr.setMaximum(100);
		fr.setBackgroundColor(Color.white);
		fpos = new mySlideBar(this,fp_data);
		fpos.setMinimum(0);
		fpos.setMaximum(99);
		fpos.setBackgroundColor(Color.white);
		loops = new SlideBar();
		loops.setMinimum(0);
		loops.setMaximum(10);
		loops.setBackgroundColor(Color.lightGray);
		
		// Build choice
		fpchoice = new Choice();
		fpchoice.add("affects all parts");
		fpchoice.add("affects good parts");
		
		// Build test step box
		PanelFormat pf = new PanelFormat();
		Panel step_panel = new Panel();
		step_panel.setBackground(Color.orange);
		pf.initializePanel(step_panel);
		pf.LabelSlide(step_panel,new Label("Yield before test (%)"),yieldbefore);
		pf.Space(step_panel);
		costtest.setBackground(Color.white);
		pf.LabelField(step_panel,new Label("Test cost"),costtest);
		pf.LabelSlide(step_panel,new Label("Fault coverage (%)"),fctest);
		pf.LabelSlide(step_panel,new Label("False positives (%)"),fpos);
		pf.LabelChoice(step_panel,new Label("False postives"),fpchoice);
		pf.Space(step_panel);
		pf.LabelSlide(step_panel,new Label("Yield after test (%)"),yieldafter);
		
		// Incoming box
		PanelFormat pf1 = new PanelFormat();
		Panel in_panel = new Panel();
		pf1.initializePanel(in_panel);
		pf1.LabelField(in_panel,new Label("Incoming cost"),costin);
		pf1.LabelSlide(in_panel,new Label("Incoming yield (%)"),yieldin);

		// Output box
		PanelFormat pf2 = new PanelFormat();
		Panel out_panel = new Panel();
		pf2.initializePanel(out_panel);
		pf2.LabelLabelLabel(out_panel,new Label(""),new Label("Mean"),new Label("Std Dev"));
		pf2.LabelFieldField(out_panel,new Label("Outgoing cost"),costout,costout_sd);
		pf2.LabelFieldField(out_panel,new Label("Outgoing yield (%)"),yieldout,yieldout_sd);
		pf2.LabelFieldField(out_panel,new Label("Pass fraction (%)"),pass,pass_sd);
		
		// Diagnosis panel
		PanelFormat pf3 = new PanelFormat();
		Panel diag_panel = new Panel();
		diag_panel.setBackground(Color.pink);
		pf3.initializePanel(diag_panel);
		//pf3.Space(diag_panel);
		pf3.LabelField(diag_panel,new Label("Diagnosis cost"),costdiag);
		//Label ldiag = new Label("Diagnosis yield (%)");
		//pf3.LabelSlide(diag_panel,ldiag,yielddiag);
		pf3.LabelSlide(diag_panel,new Label("Diagnosis fraction (%)"),fd);
		//pf3.Space(diag_panel);
		
		// Rework panel
		PanelFormat pf4 = new PanelFormat();
		Panel rew_panel = new Panel();
		rew_panel.setBackground(Color.green.brighter().brighter());
		pf4.initializePanel(rew_panel);
		//pf4.Space(rew_panel);
		costrew.setBackground(Color.white);
		pf4.LabelField(rew_panel,new Label("Rework cost"),costrew);
		pf4.LabelSlide(rew_panel,new Label("Rework yield (%)"),yieldrew);
		pf4.LabelSlide(rew_panel,new Label("Rework fraction (%)"),fr);	
		//pf4.Space(rew_panel);
		
		// Scrap panel
		PanelFormat pf5 = new PanelFormat();
		Panel scrap_panel = new Panel();
		pf5.initializePanel(scrap_panel);
		pf5.LabelLabelLabel(scrap_panel,new Label(""),new Label("Mean"),new Label("Std Dev"));
		pf5.LabelFieldField(scrap_panel,new Label("Scrap fraction (%)"),scrap,scrap_sd);
		pf5.Space(scrap_panel);
		pf5.Space(scrap_panel);
		
		Panel scrap_panel1 = new Panel();
		GridBagLayout gblms = new GridBagLayout();
		scrap_panel1.setLayout(gblms);
		GridBagConstraints gbcms = new GridBagConstraints();
		gbcms.fill = GridBagConstraints.BOTH;
        gbcms.weightx = 1.0; 
		
		gbcms.gridwidth = GridBagConstraints.REMAINDER; 
		gblms.setConstraints(rew_panel,gbcms);
		scrap_panel1.add(rew_panel);
		
		Panel loops_panel = new Panel();
		loops_panel.add(new Label("Rework attempts"));
		loops_panel.add(loops);
		MyCanvas3 cf22 = new MyCanvas3(this,"smuparrow.gif",0,0);
        cf22.setBackground(Color.white);
        cf22.resize(20,25);
		gbcms.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblms.setConstraints(cf22,gbcms);
		//scrap_panel1.add(cf22);
		loops_panel.add(cf22);
		
		gbcms.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblms.setConstraints(loops_panel,gbcms);
		scrap_panel1.add(loops_panel);
		
		gbcms.gridwidth = GridBagConstraints.REMAINDER;
		gblms.setConstraints(diag_panel,gbcms);
		scrap_panel1.add(diag_panel);

		// Leftright arrow panel
		Panel lr_panel = new Panel();
		GridBagLayout gblmlr = new GridBagLayout();
		lr_panel.setLayout(gblmlr);
		GridBagConstraints gbcmlr = new GridBagConstraints();
		gbcmlr.fill = GridBagConstraints.BOTH;
        gbcmlr.weightx = 1.0; 
		
		MyCanvas3 cf23 = new MyCanvas3(this,"smleftarrow.gif",0,43);
        cf23.setBackground(Color.white);
        cf23.resize(30,100);
		gbcmlr.gridwidth = GridBagConstraints.REMAINDER;
		gblmlr.setConstraints(cf23,gbcmlr);
		lr_panel.add(cf23);
		
		MyCanvas3 cf28 = new MyCanvas3(this,"smrightarrow.gif",0,35);
        cf28.setBackground(Color.white);
        cf28.resize(30,100);
		gbcmlr.gridwidth = GridBagConstraints.REMAINDER;
		gblmlr.setConstraints(cf28,gbcmlr);
		lr_panel.add(cf28);
	
		// Whole thing
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(in_panel,gbcmp);
		proc_master.add(in_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l = new Label("");
		gblmp.setConstraints(l,gbcmp);
		proc_master.add(l);
		
		MyCanvas3 cf29 = new MyCanvas3(this,"downarrow.gif",90,0);
        cf29.setBackground(Color.white);
        cf29.resize(20,90);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf29,gbcmp);
		proc_master.add(cf29);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(step_panel,gbcmp);
		proc_master.add(step_panel);

		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; 
		gblmp.setConstraints(lr_panel,gbcmp);
		proc_master.add(lr_panel);
		
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(scrap_panel1,gbcmp);
		proc_master.add(scrap_panel1);
		
		MyCanvas3 cf21 = new MyCanvas3(this,"downarrow.gif",90,0);
        cf21.setBackground(Color.white);
        cf21.resize(20,95);
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL; //end row
		gblmp.setConstraints(cf21,gbcmp);
		proc_master.add(cf21);
		MyCanvas3 cf27 = new MyCanvas3(this,"downarrow.gif",115,0);
        cf27.setBackground(Color.white);
        cf27.resize(20,95);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(cf27,gbcmp);
		proc_master.add(cf27);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(out_panel,gbcmp);
		proc_master.add(out_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		gblmp.setConstraints(scrap_panel,gbcmp);
		proc_master.add(scrap_panel);
		
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l3 = new Label("");
		gblmp.setConstraints(l3,gbcmp);
		proc_master.add(l3);
		
		// Controls
		Panel button_panel = new Panel();
		button_panel.add(default_button_cp);
		//button_panel.add(critique_button_cp);
		button_panel.add(compute_button_cp);
		//button_panel.add(plot);
		button_panel.add(bar_plot);
		Panel monte_panel = new Panel();
		mc_choice = new myChoice(this);
		mc_choice.add("No");
		mc_choice.add("Yes");
		monte_panel.add(new Label("Monte Carlo"));
		monte_panel.add(mc_choice);
		monte_label = new Label("Number of runs");
		monte_panel.add(monte_label);
		monte_runs = new TextField(blank);
		monte_panel.add(monte_runs);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(button_panel,gbcmp);
		proc_master.add(button_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l2 = new Label("");
		gblmp.setConstraints(l2,gbcmp);
		proc_master.add(l2);
		
		gbcmp.gridwidth = GridBagConstraints.HORIZONTAL;
		gblmp.setConstraints(monte_panel,gbcmp);
		proc_master.add(monte_panel);
		gbcmp.gridwidth = GridBagConstraints.REMAINDER; //end row
		Label l53 = new Label("");
		gblmp.setConstraints(l53,gbcmp);
		proc_master.add(l53);
		
		add(proc_master);
						
		//ldiag.disable();
		yielddiag.disable();
		mc_choice.modifyFields();
		
		invalidate();
		validate();		//forces repaint
			
		return;
	}
	
	double composite(int i,double f,double y1,double y2,double e)
	{
		if(i == 0)  // fp affects all parts
			f = 0.0;
		
		double x = ((1.0 - f)*y1*y2)/(1.0-f*y1*y2);
		//System.out.println("y1 = " + y1 + ", y2 = " + y2 + ", f = " + f + ", x = " + x + ", e = " + e);
		//System.out.println("composite = " + Math.pow(x,e));
		return(Math.pow(x,e));
	}
	
	double mean(double[] d)
	{
		double sum = 0.0;
		for(int i = 0; i < monte.number_runs(); i++)
			sum += d[i];
		return(sum/monte.number_runs());
	}
	
	double stddev(double[] d)
	{
		if (!this.monte_boolean)  // Monte Carlo is off
			return(0.0);
		
		double sum = 0.0;
		double m = mean(d);
		for(int i = 0; i < monte.number_runs(); i++)
			sum += (d[i]-m)*(d[i]-m);
		return(Math.sqrt(sum/(monte.number_runs()-1)));
	}
	
	// Bin the specified data into the specified number of bins
    // v = returned data array for plotting
    // l = returned label array for plotting
    // k = number of standard deviations
    // m = number of bars
    // Returns false if there is not distribution to plot, otherwize true
    public boolean barchart_data(String name,double[] v,String[] l, double k,int m)
    {
        double[] value = new double[N];
        int n = this.monte.number_runs();

        // Get data_member reference corresponding to "name"
		if(name.compareTo("Cost") == 0)
			value = co_array;
		if(name.compareTo("Yield") == 0)
            value = yo_array;
		if(name.compareTo("Yielded Cost") == 0)
            value = yc_array;
		if(name.compareTo("Scrap") == 0)
            value = sc_array;
	/*	if(value != anything)  {
            ErrorHandler.report("Applicable data set for plotting not found");
			return(false);
        }  */

        // Check data, if no distribution - return false
        double value0 = value[0];
        int i;
        for(i = 1; i < this.monte.number_runs(); i++)  {
            if(value[i] != value0)
                break;
        }
        if(i == this.monte.number_runs() && value[this.monte.number_runs()-1] == value0)
 		    return(false);

        // Get mean of plot data
        double mean = mean(value);

        // Get range of plot data
        double range = k*2.0*stddev(value);

        // Figure out bin size
        double bin_size = range/m;

        // Initialize the return array
        for(int j = 0; j < n; j++)  {
            v[j] = 0;
        }

        // Get starting point
        double x = mean-range/2.0;

        // Don't allow negative bins
        if(x < 0.0)  {
            bin_size = bin_size*(mean + range/2.0)/range;// reduce bin size to get the same end point
            x = 0.0;  // reset starting point
        }

        // Bin the runs into k bins
        for(i = 0; i < n; i++)  {
            if(value[i] < x || value[i] >= m*bin_size+x)
                    continue;
            for(int j = 0; j < m; j++)  {  // bin loop
                if(value[i] >= x+j*bin_size && value[i] < x+(j+1)*bin_size)
                    v[j]++;
            }
        }

        // Label the bins
		SigFig sf = new SigFig();
        for(int j = 0; j < m; j++)  {
            Double z1;
            if(name.compareTo("Yield") == 0)  {
                z1 = new Double(sf.customround(x+j*bin_size,5));
             }  else  {
                z1 = new Double(sf.customround(x+j*bin_size,3));
            }
            l[j] = z1.toString();
        }

        return(true);
    }
}

class SpaceCanvas extends Canvas {

	int xsp;
    int ysp;

    public SpaceCanvas(int i,int j)  {
        xsp = i;
		ysp = j;
    }

    public void paint(Graphics g) {
        g.setColor(Color.gray);
		Dimension d = this.size();
        g.draw3DRect(0,ysp-4,d.width,3,true);
    }
}

class MyCanvas1 extends Canvas {

    String s1;

    public MyCanvas1(String s)  {
        s1 = s;
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawLine(1,2,300,2);
        g.drawString(s1,20,20);
    }
}

class MyCanvas2 extends Canvas {

    String s1;
    int x1;
    int y1;

    public MyCanvas2(String s,int i1,int i2)  {
        s1 = s;
        x1 = i1;
        y1 = i2;
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawString(s1,x1,y1);
    }
}

class MyCanvas3 extends Canvas {

    Applet a1;
    String s1;
    int x1;
    int y1;

    public MyCanvas3(Applet a,String s,int i1,int i2)  {
        a1 = a;
        s1 = s;
        x1 = i1;
        y1 = i2;
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawImage(a1.getImage(a1.getCodeBase(),"images/" + s1),x1,y1,null);
    }
}

class MyCanvas4 extends Canvas {

    Applet a1;
    String s1;
    int x1;
    int y1;
    int w;
    int l;

    public MyCanvas4(Applet a,String s,int i1,int i2,int i3,int i4)  {
        a1 = a;
        s1 = s;
        x1 = i1;
        y1 = i2;
        w = i3;
        l = i4;
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.drawImage(a1.getImage(a1.getCodeBase(),"images/" + s1),x1,y1,w,l,null);
    }
}
