/* tablealea.java - jpq - 30/01/99
 * modification et ajout de main() le 14/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class tablealea extends java.applet.Applet implements ActionListener {
  static final long serialVersionUID = 180214L;
  TextField tNsimul, ta, tb;
  TextArea txta;
  double a, b;
  int Nsimul;
  Button efface, unif, entier, normal, poisson;
  Random r;

  private TextField ajouttf (boolean b, Font f, String s, int i, double d)
  { Label l;
    add (l = new Label (s));
    l.setBackground (Color.lightGray);
    l.setFont (f);
    TextField T;
    if (b) T = new TextField (Integer.toString (i), 4);
    else T = new TextField (Double.toString (d), 6);
    add (T);
    T.setFont (f);
    return T;
  }

 private int gparmi (String s, int i) {
    try {
      s = getParameter (s);
      i = Integer.parseInt (s);
    }
    catch (NumberFormatException e) {}
    catch (NullPointerException e) {}
    return i;
  }

 private double gparmd (String s, double d) {
    try {
      s = getParameter (s);
      d = Double.parseDouble (s);
    }
    catch (NumberFormatException e) {}
    catch (NullPointerException e) {}
    return d;
  }

  public void init ()
  { setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    Font f = new Font ("Arial", Font.PLAIN, 10);

    Nsimul = gparmi ("ns", 100);
    tNsimul = ajouttf (true, f, "n = ", Nsimul, 0.0);

    a = gparmd ("a", 0.0);
    ta = ajouttf (false, f, "a = ", 0, a);

    b = gparmd ("b", 1.0);
    tb = ajouttf (false, f, "b = ", 0, b);

    add (efface = new Button ("X"));
    efface.addActionListener (this);
    add (unif = new Button ("U"));
    unif.addActionListener (this);
    add (normal = new Button ("N"));
    normal.addActionListener (this);
    add (entier = new Button ("E"));
    entier.addActionListener (this);
    add (poisson = new Button ("P"));
    poisson.addActionListener (this);
    add (txta = new TextArea ("", 10, 50, TextArea.SCROLLBARS_BOTH));
    r = new Random ();
  }

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () instanceof Button)
    { try { Nsimul = Integer.parseInt (tNsimul.getText ()); }
      catch (NumberFormatException nfe) { }
      if (Nsimul <= 0) Nsimul = 1;
      tNsimul.setText (Integer.toString (Nsimul));
      try { a = new Double(ta.getText()).doubleValue(); } 
      catch (NumberFormatException nfe) { }
      ta.setText (new Double(a).toString());
      try { b = new Double(tb.getText()).doubleValue(); } 
      catch (NumberFormatException nfe) { }
      tb.setText (new Double(b).toString());
      if (e.getSource () == efface) txta.setText ("");
      else if (e.getSource () == unif)
      { for (int i = 1; i <= Nsimul; i ++)
        { double d = r.nextDouble () * (b - a) + a;
          txta.append (new Double(d).toString () + "  ");
          if (i % 20 == 0) txta.append ("\n");
        }
      }
      else if (e.getSource () == normal)
      { for (int i = 1; i <= Nsimul; i ++)
        { double v, w, sq;
          do
          { v = 2.0 * r.nextDouble () - 1.0;
            w = 2.0 * r.nextDouble () - 1.0;
            sq = v * v + w * w;
          }
          while (sq >= 1);
          sq = Math.sqrt (- 2 * Math.log (sq) / sq);
          txta.append (new Double(v * sq * b + a).toString () + "  ");
          txta.append (new Double(w * sq * b + a).toString () + "  ");
          i ++;
          if (i % 20 == 0) txta.append ("\n");
        }
      }
      else if (e.getSource () == entier)
      { for (int i = 1; i <= Nsimul; i ++)
        { int j = (int)(r.nextDouble () * (b - a) + a);
          txta.append (Integer.toString (j) + "  ");
          if (i % 20 == 0) txta.append ("\n");
        }
      }
      else if (e.getSource () == poisson)
      { for (int i = 1; i <= Nsimul; i ++)
        { double expma = Math.exp (- a);
          int j = 0;
          double d = r.nextDouble ();
          while (d >= expma)
          { j ++;
            d *= r.nextDouble ();
          }
          txta.append (Integer.toString (j) + "  ");
          if (i % 20 == 0) txta.append ("\n");
        }
      }
    }
  }

  public String getAppletInfo () {
    return "tablealea par j.-p. Quelen";
  }

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {

        tablealea a = new tablealea();
        a.init ();
        a.start ();

        Frame f = new Frame ("tablealea");
        f.addWindowListener (new fermer ());
        f.add (a);
        f.setSize (450, 250);
        f.setVisible (true);
    }

// Permet la fermeture de la fenêtre contenant l'applet
    protected static final class fermer extends WindowAdapter {
        public void windowClosing (WindowEvent e) {
            System.exit (0);
        }
    }

}
