//******************************************************************************
// NonChiSqr.java:	Applet
//
//******************************************************************************

import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.URL;
import VBrun400;

//==============================================================================
// Main class for applet NonChiSqr.java
//
//==============================================================================
public class NonChiSqr extends Applet
{
    
    // Load Visual Basic run-time Java classes.
    //--------------------------------------------------------------------------
    VBrun400 VB = new VBrun400();


    // Create java components corresponding to Visual Basic controls.
    //--------------------------------------------------------------------------
    Button Command2 = new Button("Exit");
    TextField txtDelta = new TextField("0");
    TextField txtDoF = new TextField("5");
    TextField txtX = new TextField("3");
    Button Command1 = new Button("Solve for Probability");
    Label lblTerms = new Label("");
    Label Label6 = new Label("Terms in the expansion ");
    Label lblX2 = new Label("");
    Label Label7 = new Label("Central Chi-square");
    Label lblnonX2ans = new Label("");
    Label Label5 = new Label("Area under a non-central chi-square less than, x ");
    Label Label4 = new Label("Enter a value for the non-centrality parameter");
    Label Label3 = new Label("Enter a value for the degrees of freedom");
    Label Label2 = new Label("Enter a value for x");
    Label Label1 = new Label("P( NCCS(d.f.,delta)) < x )=");
    Image jpgNonChiSqr;
    
    
    // Coordinate based layout manager used to mimic Visual Basic WYSIWYG layout
    //--------------------------------------------------------------------------
    protected void addControl(Container cContainer,
                              Component cControl,
                              int top,
                              int left,
                              int width,
                              int height)
    {
        cContainer.add(cControl);
        cControl.reshape(left, top, width, height);
    }


    // The init() method is used to initialize the applet. It is automatically
    // called by the system when the applet is first loaded.
    //--------------------------------------------------------------------------
    public void init()
    {
        setLayout(null);
        resize(592, 506);
        setBackground(new Color(16761024));

        Command2.setForeground(new Color(0, 0, 0));
        Command2.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, Command2, 470, 8, 123, 25);

        txtDelta.setBackground(new Color(255, 255, 255));
        txtDelta.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtDelta, 186, 365, 66, 25);

        txtDoF.setBackground(new Color(255, 255, 255));
        txtDoF.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtDoF, 122, 365, 66, 25);

        txtX.setBackground(new Color(255, 255, 255));
        txtX.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtX, 89, 365, 66, 25);

        Command1.setBackground(new Color(8454016));
        Command1.setForeground(new Color(0, 0, 0));
        Command1.setFont(new Font("Helvetica", Font.PLAIN, 13));
        addControl(this, Command1, 243, 178, 147, 42);

        lblTerms.setBackground(new Color(16777088));
        lblTerms.setForeground(new Color(0, 0, 0));
        lblTerms.setFont(new Font("Dialog", Font.PLAIN, 11));
        lblTerms.setAlignment(Label.CENTER);
        addControl(this, lblTerms, 397, 292, 58, 25);

        Label6.setBackground(new Color(16711680));
        Label6.setForeground(new Color(16777215));
        Label6.setFont(new Font("Dialog", Font.PLAIN, 13));
        addControl(this, Label6, 397, 81, 179, 25);

        lblX2.setBackground(new Color(16777088));
        lblX2.setForeground(new Color(0, 0, 0));
        lblX2.setFont(new Font("Dialog", Font.PLAIN, 13));
        lblX2.setAlignment(Label.CENTER);
        addControl(this, lblX2, 340, 292, 147, 33);

        Label7.setBackground(new Color(16711680));
        Label7.setForeground(new Color(16777215));
        Label7.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, Label7, 300, 292, 171, 25);

        lblnonX2ans.setBackground(new Color(16777088));
        lblnonX2ans.setForeground(new Color(0, 0, 0));
        lblnonX2ans.setFont(new Font("Dialog", Font.PLAIN, 13));
        lblnonX2ans.setAlignment(Label.CENTER);
        addControl(this, lblnonX2ans, 340, 41, 139, 33);

        Label5.setBackground(new Color(16711680));
        Label5.setForeground(new Color(16777215));
        Label5.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label5.setAlignment(Label.CENTER);
        addControl(this, Label5, 8, 32, 455, 33);

        Label4.setBackground(new Color(16711680));
        Label4.setForeground(new Color(16777215));
        Label4.setFont(new Font("Dialog", Font.PLAIN, 13));
        Label4.setAlignment(Label.CENTER);
        addControl(this, Label4, 186, 16, 333, 42);

        Label3.setBackground(new Color(16711680));
        Label3.setForeground(new Color(16777215));
        Label3.setFont(new Font("Dialog", Font.PLAIN, 13));
        Label3.setAlignment(Label.CENTER);
        addControl(this, Label3, 122, 57, 293, 42);

        Label2.setBackground(new Color(16711680));
        Label2.setForeground(new Color(16777215));
        Label2.setFont(new Font("Dialog", Font.PLAIN, 13));
        Label2.setAlignment(Label.CENTER);
        addControl(this, Label2, 89, 203, 139, 17);

        Label1.setBackground(new Color(16711680));
        Label1.setForeground(new Color(16777215));
        Label1.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, Label1, 300, 41, 187, 25);

    }


    // The paint() method is used to draw graphics.
    //--------------------------------------------------------------------------
    public void paint(Graphics g)
    {
    }


    // The update() method is automatically called to redraw the Applet. The
    // default implementation is to call paint().
    //--------------------------------------------------------------------------
    public void update(Graphics g)
    {
        paint(g);
    }


    // User defined functions derived from Visual Basic code.
    //--------------------------------------------------------------------------

    private void Command1_Click()
    {
        try
        {
        double term1 = 0;
        double phi = 0;
        double xs = 0;
        double r1 = 0;
        double a1 = 0;
        double p1 = 0;
        int I1 = 0;
        double s1 = 0;
        double sum1 = 0;
        int I = 0;
        double ai = 0;
        double bi = 0;
        double sgn1 = 0;
        double term2 = 0;
        double phi1 = 0;
        double term = 0;
        double y = 0;
        double delta = 0;
        double df = 0;
        double x = 0;
        double h = 0;
        double pi = 0;
        int idf = 0;
        int ddf = 0;
        double p = 0;
        double pp = 0;
        double a = 0;
        double r = 0;
        int m = 0;
        int j = 0;
        double sum = 0;
        double sterm = 0;
        double s = 0;
        double sterm1 = 0;
        delta = VB.CDbl(txtDelta.getText());
        df = VB.CDbl(txtDoF.getText());
        x = VB.CDbl(txtX.getText());
        h = 0.5 * x;
        pi = 3.141592653597;
        idf = VB.CInt((df / 2) - 0.5);
        ddf = idf;
        //First compute the probability for a central chi-square
        if ((df - (2 * ddf) - 0.5) > 0)
         {
            y = Math.sqrt((double)x);
            xs = y * y;
            y = Math.sqrt((double)xs);
        if (y > 4)
            {
            phi = 0.5;
}
            else
            {
            term = y / (Math.sqrt((double)2 * 3.1415926535897));
            phi = term;
        }
        I = 0;
        while (I < 8 || Math.abs(term) > 0.000001) {
            I1 = I;
           I = I1 + 1;
           ai = VB.CDbl(I);
           bi = VB.CDbl(2 * I + 1);
           term2 = term;
           term = -1 * term2 * xs * (bi - 2) / (2 * ai * bi);
           phi1 = phi;
           phi = phi1 + term;
}
        sgn1 = y / y;
        phi1 = phi;
        phi = phi1 * sgn1 + 0.5;
            p = 2 * phi - 1;
            a = (Math.exp((double)-0.5 * x) / Math.sqrt((double)pi * h));
            r = -0.5;
            m = idf;
}
              else
              {
            p = 1 - Math.exp((double)-1 * h);
            a = Math.exp((double)-1 * h);
            r = 0;
            m = idf - 1;
        }
        if (df >= 2.1)
         {
            for (j = 1; j <= m; j++)
            {
            r1 = r;
            r = r1 + 1;
            a1 = a;
            a = a1 * h / r;
            p1 = p;
            p = p1 - a;
            }
        }
        pp = p;
        lblX2.setText(String.valueOf(pp));
        //Now compute the noncentral probability
        term = Math.exp((double)-1 * delta);
        sum = term * p;
        s = 0;
        sterm = term;
        while (sterm < 0.999999999999)
         {
            s1 = s;
            s = s1 + 1;
            term1 = term;
            term = term1 * delta / s;
            r1 = r;
            r = r1 + 1;
            a1 = a;
            a = a1 * h / r;
            p1 = p;
            p = p1 - a;
            sum1 = sum;
            sum = sum1 + term * p;
            sterm1 = sterm;
            sterm = sterm1 + term;
}
        lblnonX2ans.setText(String.valueOf(sum));
        lblTerms.setText(String.valueOf(s));
        }
        catch (Exception ex)
        {
            // TODO: Exception Handling
        
            // Display Exception message in browser's status bar
            showStatus(ex.getMessage());
        }
    }



    private void Command2_Click()
    {
        try
        {
// Not supported when making a single form applet
        // End
        }
        catch (Exception ex)
        {
            // TODO: Exception Handling
        
            // Display Exception message in browser's status bar
            showStatus(ex.getMessage());
        }
    }




    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Command2(Event e)
    {
        boolean bReturn = false;

        if (e.id == Event.ACTION_EVENT)
        {
            Command2_Click();
            bReturn = true;
        }

        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtDelta(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtDoF(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtX(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Command1(Event e)
    {
        boolean bReturn = false;

        if (e.id == Event.ACTION_EVENT)
        {
            Command1_Click();
            bReturn = true;
        }

        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_lblTerms(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label6(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_lblX2(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label7(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_lblnonX2ans(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label5(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label4(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label3(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label2(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_Label1(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }
    
    
    // The routeEvent() method is used to route user events to this Applet
    //--------------------------------------------------------------------------
    private boolean routeEvent_NonChiSqr(Event e)
    {
        boolean bReturn = false;
    

        return bReturn;
    }


    // The handleEvent() method is automatically called whenever the applet
    // receives an event.
    //--------------------------------------------------------------------------
    public boolean handleEvent( Event e )
    {
        boolean bReturn = false;

        if (e.target == Command2)
            bReturn = routeEvent_Command2(e);
        if (e.target == txtDelta)
            bReturn = routeEvent_txtDelta(e);
        if (e.target == txtDoF)
            bReturn = routeEvent_txtDoF(e);
        if (e.target == txtX)
            bReturn = routeEvent_txtX(e);
        if (e.target == Command1)
            bReturn = routeEvent_Command1(e);
        if (e.target == lblTerms)
            bReturn = routeEvent_lblTerms(e);
        if (e.target == Label6)
            bReturn = routeEvent_Label6(e);
        if (e.target == lblX2)
            bReturn = routeEvent_lblX2(e);
        if (e.target == Label7)
            bReturn = routeEvent_Label7(e);
        if (e.target == lblnonX2ans)
            bReturn = routeEvent_lblnonX2ans(e);
        if (e.target == Label5)
            bReturn = routeEvent_Label5(e);
        if (e.target == Label4)
            bReturn = routeEvent_Label4(e);
        if (e.target == Label3)
            bReturn = routeEvent_Label3(e);
        if (e.target == Label2)
            bReturn = routeEvent_Label2(e);
        if (e.target == Label1)
            bReturn = routeEvent_Label1(e);
        if (e.target == this) 
            bReturn = routeEvent_NonChiSqr(e);
        
        if (bReturn == true)
            return true;
        else
            return super.handleEvent(e);
    }
}
