//******************************************************************************
// DNCF.java:	Applet
//
//******************************************************************************

import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.URL;
import VBrun400;

//==============================================================================
// Main class for applet DNCF.java
//
//==============================================================================
public class DNCF extends Applet
{
    
    // Load Visual Basic run-time Java classes.
    //--------------------------------------------------------------------------
    VBrun400 VB = new VBrun400();


    // Create java components corresponding to Visual Basic controls.
    //--------------------------------------------------------------------------
    TextField txtX = new TextField("1.5");
    TextField txtdnc2 = new TextField("1");
    TextField txtdnc1 = new TextField("1");
    TextField txtdf2 = new TextField("7");
    TextField txtdf1 = new TextField("6");
    Button Command1 = new Button("Compute Probability");
    Label prdncf = new Label("");
    Label prcentf = new Label("");
    Label Label7 = new Label("Critical Point, f ");
    Label Label6 = new Label("Denominator Non-centrality Parameter, k2 ");
    Label Label5 = new Label("Numerator Non-centrality Parameter, k1 ");
    Label Label4 = new Label("Denominator Degrees of Freedom, n2 ");
    Label Label3 = new Label("Numerator Degrees of Freedom, n1 ");
    Label Label2 = new Label("P( F < f ; n1, n2, k1, k2 ) = ?");
    Label Label1 = new Label("Probability Under a Doubly Non-central F");
    Image jpgDNCF;
    
    
    // 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(505, 436);
        setBackground(new Color(8454016));

        txtX.setBackground(new Color(255, 255, 255));
        txtX.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtX, 284, 356, 66, 33);

        txtdnc2.setBackground(new Color(255, 255, 255));
        txtdnc2.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtdnc2, 235, 356, 66, 33);

        txtdnc1.setBackground(new Color(255, 255, 255));
        txtdnc1.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtdnc1, 186, 356, 66, 33);

        txtdf2.setBackground(new Color(255, 255, 255));
        txtdf2.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtdf2, 138, 356, 66, 33);

        txtdf1.setBackground(new Color(255, 255, 255));
        txtdf1.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtdf1, 89, 356, 66, 33);

        Command1.setForeground(new Color(0, 0, 0));
        Command1.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, Command1, 332, 154, 187, 25);

        prdncf.setBackground(new Color(16776960));
        prdncf.setForeground(new Color(0, 0, 0));
        prdncf.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, prdncf, 381, 259, 236, 25);

        prcentf.setBackground(new Color(16776960));
        prcentf.setForeground(new Color(0, 0, 0));
        prcentf.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, prcentf, 381, 8, 236, 25);

        Label7.setBackground(new Color(65280));
        Label7.setForeground(new Color(0, 0, 0));
        Label7.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label7.setAlignment(Label.RIGHT);
        addControl(this, Label7, 284, 138, 204, 25);

        Label6.setBackground(new Color(65280));
        Label6.setForeground(new Color(0, 0, 0));
        Label6.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label6.setAlignment(Label.RIGHT);
        addControl(this, Label6, 235, 24, 317, 33);

        Label5.setBackground(new Color(65280));
        Label5.setForeground(new Color(0, 0, 0));
        Label5.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label5.setAlignment(Label.RIGHT);
        addControl(this, Label5, 186, 49, 293, 33);

        Label4.setBackground(new Color(65280));
        Label4.setForeground(new Color(0, 0, 0));
        Label4.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label4.setAlignment(Label.RIGHT);
        addControl(this, Label4, 138, 49, 293, 33);

        Label3.setBackground(new Color(65280));
        Label3.setForeground(new Color(0, 0, 0));
        Label3.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label3.setAlignment(Label.RIGHT);
        addControl(this, Label3, 89, 49, 293, 33);

        Label2.setBackground(new Color(8454143));
        Label2.setForeground(new Color(0, 0, 0));
        Label2.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label2.setAlignment(Label.CENTER);
        addControl(this, Label2, 57, 97, 228, 25);

        Label1.setBackground(new Color(65280));
        Label1.setForeground(new Color(0, 0, 0));
        Label1.setFont(new Font("Dialog", Font.PLAIN, 24));
        Label1.setAlignment(Label.CENTER);
        addControl(this, Label1, 8, 8, 487, 33);

    }


    // 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
        {
        String ncpp = new String();
        String pp = new String();
        double ca[] = new double[4+1];
        double cb[] = new double[4+1];
        double lx = 0;
        double r = 0;
        double b = 0;
        double s = 0;
        double df = 0;
        double delta = 0;
        int ii = 0;
        int jj = 0;
        double df1 = 0;
        double df2 = 0;
        double dnc1 = 0;
        double dnc2 = 0;
        double x = 0;
        double vv = 0;
        double bb1 = 0;
        double bb2 = 0;
        double av = 0;
        double v = 0;
        double b1 = 0;
        double b2 = 0;
        double xx = 0;
        double y = 0;
        double p = 0;
        double pr = 0;
        double ncp = 0;
        double g = 0;
        double z = 0;
        double u = 0;
        double et = 0;
        double xa = 0;
        df1 = VB.CDbl(txtdf1.getText());
        df2 = VB.CDbl(txtdf2.getText());
        dnc1 = VB.CDbl(txtdnc1.getText());
        dnc2 = VB.CDbl(txtdnc2.getText());
        x = VB.CDbl(txtX.getText());
        for (jj = 1; jj <= 2; jj++)
         {
        if (jj == 2)
         {
        dnc1 = 0.0;

        dnc2 = 0.0;

        }
        for (ii = 1; ii <= 2; ii++)
         {
        if (ii == 1)
         {
        df = df1;
        delta = dnc1;
}
          else
          {
        df = df2;
        delta = dnc2;
        }
        r = df + delta;
        b = delta / r;
        s = (Math.exp((double)(1.0 / 3.0) * Math.log((double)r / df)) * (1.0 - (2.0 * (b + 1.0) / (9.0 * r)) - (40.0 * b * b / (3.0 * 3.0 * 3.0 * 3.0 * r * r)) + (80.0 * (1.0 + 3.0 * b + 33.0 * b * b - 77.0 * b * b * b) / (Math.exp((double)7.0 * Math.log((double)3.0)) * r * r * r)) + (176.0 * (1.0 + 4.0 * b - 210.0 * b * b + 2380.0 * b * b * b - 2975.0 * b * b * b * b) / (Math.exp((double)9.0 * Math.log((double)3.0)) * r * r * r * r))));
        if (ii == 1)
         {
        ca[1] = s;
}
         else
         {
        cb[1] = s;
        }
        vv = (Math.exp((double)(2.0 / 3.0) * Math.log((double)r / df)) * ((2.0 * (b + 1.0) / (9.0 * r)) + (16.0 * b * b / (3.0 * 3.0 * 3.0 * r * r)) - (8.0 * (13.0 + 39.0 * b + 405.0 * b * b - 1025.0 * b * b * b) / (Math.exp((double)7.0 * Math.log((double)3.0)) * r * r * r)) - (160.0 * (1.0 + 4.0 * b - 87.0 * b * b + 1168.0 * b * b * b - 1544.0 * b * b * b * b) / (Math.exp((double)8.0 * Math.log((double)3.0)) * r * r * r * r))));
        if (ii == 1)
         {
        ca[2] = vv;
}
         else
         {
        cb[2] = vv;
        }
        bb1 = ((r / df) * ((-8.0 * b * b / (3.0 * 3.0 * 3.0 * r * r)) + (32.0 * (1.0 + 3.0 * b + 21.0 * b * b - 62.0 * b * b * b) / (Math.exp((double)6.0 * Math.log((double)3.0)) * r * r * r)) + (32.0 * (8.0 + 32.0 * b - 177.0 * b * b + 4550.0 * b * b * b - 6625.0 * b * b * b * b) / (Math.exp((double)8.0 * Math.log((double)3.0)) * r * r * r * r))));
        if (ii == 1)
         {
        ca[3] = bb1;
}
         else
         {
        cb[3] = bb1;
        }
        bb2 = (Math.exp((double)(4.0 / 3.0) * Math.log((double)r / df)) * ((-16.0 * (1.0 + 3.0 * b + 12.0 * b * b - 44.0 * b * b * b) / (Math.exp((double)6.0 * Math.log((double)3.0)) * r * r * r)) - (256.0 * (1.0 + 4.0 * b + 6.0 * b * b + 274.0 * b * b * b - 458.0 * b * b * b * b) / (Math.exp((double)8.0 * Math.log((double)3.0)) * r * r * r * r))));
        if (ii == 1)
         {
        ca[4] = bb2;
}
         else
         {
        cb[4] = bb2;
        }
        }
        lx = Math.log((double)x);
        av = ca[1] - cb[1] * (Math.exp((double)(1.0 / 3.0) * lx));
        v = ca[2] + cb[2] * (Math.exp((double)(2.0 / 3.0) * lx));
        b1 = (ca[3] - cb[3] * x) / (Math.exp((double)(2.0 / 3.0) * Math.log((double)v)));
        b2 = (ca[4] + cb[4] * (Math.exp((double)(4.0 / 3.0) * lx))) / v * v;
        xx = -1 * av / Math.sqrt((double)v);
        g = 1.1283792 * Math.exp((double)-1 * xx * xx / 2.0);
        z = g / 2.82842712;
        xa = Math.abs(xx);
        if (xa >= 2.5 && xx < 0.0)
         {
        u = 1.0 / (xa + 1.0 / (xa + 2.0 / (xa + 3.0 / (xa + 4.0 / (xa + 5.0 / (xa + 6.0 / (xa + 7.0 / (xa + 8.0 / (xa + 9.0 / (xa + 10.0 / (xa + 11.0 / (xa + 12.0 / xa))))))))))));
        pr = u * z;
        }
        if (xa >= 2.5 && xx >= 0)
         {
        u = 1.0 / (xa + 1.0 / (xa + 2.0 / (xa + 3.0 / (xa + 4.0 / (xa + 5.0 / (xa + 6.0 / (xa + 7.0 / (xa + 8.0 / (xa + 9.0 / (xa + 10.0 / (xa + 11.0 / (xa + 12.0 / xa))))))))))));
        pr = 1.0 - u * z;
        }
        if (xa < 2.5 && xx <= 0)
         {
        et = 1.4142136 / (1.4142136 + 0.3275911 * xa);
        u = g * ((((0.94064607 * et - 1.28782245) * et + 1.25969513) * et - 0.252128668) * et + 0.225836846) * et;
        pr = u / 2.0;

        }
        if (xa < 2.5 && xx > 0)
         {
        et = 1.4142136 / (1.4142136 + 0.3275911 * xa);
        u = g * ((((0.94064607 * et - 1.28782245) * et + 1.25969513) * et - 0.252128668) * et + 0.225836846) * et;
        pr = 1 - u / 2.0;

        }
        y = xx;
        p = pr - z * ((b1 * (y * y - 1.0) / 6.0) + (b2 * y * (y * y - 3.0) / 24.0) + (b1 * b1 * y * (y * y * y * y - 10.0 * y * y + 15.0) / 72.0));
        if (jj == 1)
         {
        ncp = p;
        }
        }
        ncpp = String.valueOf(ncp);
        prdncf.setText("P( F<f ; n1, n2, k1, k2 )=" + (ncpp).substring(0, (6)));
        pp = String.valueOf(p);
        prcentf.setText("P( F<f ; n1,n2 )=" + (pp).substring(0, (6)));
        }
        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_txtX(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtdnc2(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtdnc1(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtdf2(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtdf1(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_prdncf(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_prcentf(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_Label6(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_DNCF(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 == txtX)
            bReturn = routeEvent_txtX(e);
        if (e.target == txtdnc2)
            bReturn = routeEvent_txtdnc2(e);
        if (e.target == txtdnc1)
            bReturn = routeEvent_txtdnc1(e);
        if (e.target == txtdf2)
            bReturn = routeEvent_txtdf2(e);
        if (e.target == txtdf1)
            bReturn = routeEvent_txtdf1(e);
        if (e.target == Command1)
            bReturn = routeEvent_Command1(e);
        if (e.target == prdncf)
            bReturn = routeEvent_prdncf(e);
        if (e.target == prcentf)
            bReturn = routeEvent_prcentf(e);
        if (e.target == Label7)
            bReturn = routeEvent_Label7(e);
        if (e.target == Label6)
            bReturn = routeEvent_Label6(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_DNCF(e);
        
        if (bReturn == true)
            return true;
        else
            return super.handleEvent(e);
    }
}
