//******************************************************************************
// cpPoisson.java:	Applet
//
//******************************************************************************

import java.awt.*;
import java.util.*;
import java.applet.*;
import java.net.URL;

//==============================================================================
// Main class for applet cpPoisson.java
//
//==============================================================================
public class cpPoisson extends Applet
{


    // Create java components corresponding to Visual Basic controls.
    //--------------------------------------------------------------------------
    TextField txtT = new TextField("");
    TextField txtTau = new TextField("");
    TextField txtx = new TextField("");
    Button Command2 = new Button("Solve for Probability");
    Button Command1 = new Button("Quit");
    Label txtxLTx = new Label("");
    Label txtxEQx = new Label("");
    Label Label6 = new Label("P( x<X | tau,t )");
    Label Label5 = new Label("P( x=X | tau,t )");
    Label Label1 = new Label("Poisson Distribution");
    Label Label4 = new Label("Number of Time Intervals, t");
    Label Label3 = new Label("Number of Failures, tau");
    Label Label2 = new Label("Critical Value, x");
    Image jpgcpPoisson;
    
    
    // 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(571, 431);
        setBackground(new Color(16777152));

        txtT.setBackground(new Color(255, 255, 255));
        txtT.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtT, 122, 421, 42, 25);

        txtTau.setBackground(new Color(255, 255, 255));
        txtTau.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtTau, 122, 211, 42, 27);

        txtx.setBackground(new Color(255, 255, 255));
        txtx.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtx, 122, 41, 42, 25);

        Command2.setBackground(new Color(16777088));
        Command2.setForeground(new Color(0, 0, 0));
        Command2.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, Command2, 178, 154, 171, 42);

        Command1.setForeground(new Color(0, 0, 0));
        Command1.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, Command1, 373, 24, 106, 42);

        txtxLTx.setBackground(new Color(16777215));
        txtxLTx.setForeground(new Color(0, 0, 0));
        txtxLTx.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtxLTx, 284, 292, 147, 25);

        txtxEQx.setBackground(new Color(16777215));
        txtxEQx.setForeground(new Color(0, 0, 0));
        txtxEQx.setFont(new Font("Dialog", Font.PLAIN, 11));
        addControl(this, txtxEQx, 284, 57, 147, 25);

        Label6.setBackground(new Color(16777088));
        Label6.setForeground(new Color(0, 0, 0));
        Label6.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label6.setAlignment(Label.CENTER);
        addControl(this, Label6, 235, 275, 171, 25);

        Label5.setBackground(new Color(16777088));
        Label5.setForeground(new Color(0, 0, 0));
        Label5.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label5.setAlignment(Label.CENTER);
        addControl(this, Label5, 235, 41, 179, 25);

        Label1.setBackground(new Color(16777088));
        Label1.setForeground(new Color(0, 0, 0));
        Label1.setFont(new Font("Dialog", Font.PLAIN, 24));
        Label1.setAlignment(Label.CENTER);
        addControl(this, Label1, 8, 122, 228, 33);

        Label4.setBackground(new Color(16777088));
        Label4.setForeground(new Color(0, 0, 0));
        Label4.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label4.setAlignment(Label.CENTER);
        addControl(this, Label4, 73, 348, 204, 33);

        Label3.setBackground(new Color(16777088));
        Label3.setForeground(new Color(0, 0, 0));
        Label3.setFont(new Font("Dialog", Font.PLAIN, 16));
        addControl(this, Label3, 73, 146, 179, 33);

        Label2.setBackground(new Color(16777088));
        Label2.setForeground(new Color(0, 0, 0));
        Label2.setFont(new Font("Dialog", Font.PLAIN, 16));
        Label2.setAlignment(Label.CENTER);
        addControl(this, Label2, 73, 8, 123, 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
        {
// 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());
        }
    }



    private void Command2_Click()
    {
        try
        {
        double pr = 0;
        double sum = 0;
        double lambda = 0;
        double t = 0;
        double tau = 0;
        double x = 0;
        int jl = 0;
        double jj = 0;
        double j = 0;
        double factorial = 0;
        t = Integer.valueOf(txtT.getText()).intValue();
        tau = Integer.valueOf(txtTau.getText()).intValue();
        x = Integer.valueOf(txtx.getText()).intValue();
        pr = 0;
        sum = 0;
        j = 0;
        lambda = tau / t;
        for (jl = 0; jl <= x; jl++)
         {
        j = jl;
        if (j > 1)
            {
        factorial = 1;
        for (jj = 1; jj <= j; jj++)
         {
        factorial = factorial * jj;
        }
}
         else
         {
        factorial = 1;
        }
        pr = Math.exp((double)-1 * lambda) * Math.exp((double)j * Math.log((double)lambda)) / factorial;
        sum = sum + pr;
        }
        txtxLTx.setText(String.valueOf(sum));
        txtxEQx.setText(String.valueOf(pr));
        }
        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_txtT(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtTau(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_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_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_txtxLTx(Event e)
    {
        boolean bReturn = false;


        return bReturn;
    }


    // The routeEvent() method is used to route user events to applet components
    //--------------------------------------------------------------------------
    private boolean routeEvent_txtxEQx(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_Label1(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 this Applet
    //--------------------------------------------------------------------------
    private boolean routeEvent_cpPoisson(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 == txtT)
            bReturn = routeEvent_txtT(e);
        if (e.target == txtTau)
            bReturn = routeEvent_txtTau(e);
        if (e.target == txtx)
            bReturn = routeEvent_txtx(e);
        if (e.target == Command2)
            bReturn = routeEvent_Command2(e);
        if (e.target == Command1)
            bReturn = routeEvent_Command1(e);
        if (e.target == txtxLTx)
            bReturn = routeEvent_txtxLTx(e);
        if (e.target == txtxEQx)
            bReturn = routeEvent_txtxEQx(e);
        if (e.target == Label6)
            bReturn = routeEvent_Label6(e);
        if (e.target == Label5)
            bReturn = routeEvent_Label5(e);
        if (e.target == Label1)
            bReturn = routeEvent_Label1(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 == this) 
            bReturn = routeEvent_cpPoisson(e);
        
        if (bReturn == true)
            return true;
        else
            return super.handleEvent(e);
    }
}
