/* d2.java - 18/06/01 - 04/02/02 - 24/11/02 - 04/02/03
 * simulation d'un lancer de dé, diagramme
 * ajout de main() le 31/01/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class d2 extends java.applet.Applet {
  static final long serialVersionUID = 180131L;
  controles C;
  dessin D;
  table T;

  private int gparm (String s, int i)
  { 
    if (s != null) {
	  try {
	    s = getParameter (s);
	    i = Integer.parseInt (s);
	  }
      catch (NumberFormatException nfe) {}
	  catch (NullPointerException e) {}
      if (i <= 0)
	    i = 1;
    }
    return i;
  }

  public void init ()
  { setLayout (new BorderLayout ());
    D = new dessin (gparm ("nf", 6), gparm ("ns", 100), gparm ("N", 1));
    T = new table (D);
    C = new controles (D, T);
    add ("North", C);
    add ("East", T);
    add ("Center", D);
  }

  public void destroy ()
  { remove (D); remove (C); }

  public String getAppletInfo ()
  { return "d2.java par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Panel {
  static final long serialVersionUID = 180131L;
  Image img;
  Graphics g;
  int nf, ns, N;
  int histogramme [];
  Random r;
  String donnees;
  boolean retrace;

  public dessin (int nf, int ns, int N)
  { this.nf = nf;
    this.ns = ns;
    this.N = N;
    r = new Random ();
    retrace = false;
    histogramme = new int [nf];
  }

  private void h (Graphics g, int i)
  { g.setColor (Color.white);
    g.fillRect (1, 1, getSize().width - 2, getSize().height - 2);
    g.setColor (Color.yellow);
    g.drawLine (20, getSize().height - 40, getSize().width, getSize().height - 40);
    g.drawLine (20, 40, 20, getSize().height - 40);
    g.setColor (Color.blue);
    g.drawString ("0", 5, getSize().height - 40);
    g.drawString (Integer.toString (i), 5, 40);

    for (int j = 0; j < nf; j ++)
    { int x = j * (getSize().width - 50) / nf + 50;
      int y = getSize().height - 40 - histogramme [j] * (getSize().height - 80) / (i + 1);
      g.setColor (Color.black);
      g.drawLine (x, y, x, getSize().height - 40);
      g.setColor (Color.blue);
      g.drawString (Integer.toString (j), x, getSize().height - 10);
    }
      g.setColor (Color.black);
      g.drawRect (0, 0, getSize().width - 1, getSize().height - 1);
  }

  private int c16 ()
  { return (int)(r.nextDouble () * nf);
  }

  public void update (Graphics g)
  { paint (g);
  }

  public void paint (Graphics g1)
  {  if (img == null)
      { img = createImage (getSize().width, getSize().height);
        g = img.getGraphics ();
        g.setColor (Color.white);
        g.fillRect (1, 1, getSize().width - 2, getSize().height - 2);
        g.setColor (Color.black);
        g.drawRect (0, 0, getSize().width - 1, getSize().height - 1);
      }
    if (retrace)
    { retrace = false;
      donnees = "";
      for (int ii = 0; ii < N; ii ++)
      { for (int i = 0; i < nf; i ++) histogramme [i] = 0;
        for (int i = 0; i < ns; i ++) histogramme [c16 ()] ++;
        double khi2 = 0.0;
        for (int i = 0; i < nf; i ++)
        { double diff = histogramme [i] - (double)(ns) / nf;
          khi2 += diff * diff;
        }
        khi2 /= ns * ns;
        h (g, ns);
        g.drawString (Integer.toString (ii + 1) + "; " + "d2 = " + khi2, 50, 10);
        g1.drawImage (img, 0, 0, this);
        donnees += khi2 + "\n";
      }
    }
    else g1.drawImage (img, 0, 0, this);
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180131L;
  dessin D;
  table T;
  TextField tns, tN, tnf;
  Button ok;
  Font f;

  private Label ajoutlbl (String s)
  { Label lbl = new Label (s);
    lbl.setBackground (Color.lightGray);
    lbl.setFont (f);
    return lbl;
  }

  public controles (dessin D, table T)
  { this.D = D;
    this.T = T;
    setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    f = new Font ("Arial", Font.PLAIN, 10);
    add (tns = new TextField (Integer.toString (D.ns), 5));
    tns.setFont (f);
    add (ajoutlbl ("lancers d'un dé à"));
    add (tnf = new TextField (Integer.toString (D.nf), 2));
    tnf.setFont (f);
    add (ajoutlbl ("faces;  expérience répétée"));
    add (tN = new TextField (Integer.toString (D.N), 5));
    tN.setFont (f);
    add (ajoutlbl ("fois"));
    add (ok = new Button ("Ok"));
    ok.setFont (f);
    ok.addActionListener (this);
  }

  private int prsi (TextField tf, int i)
  { int j = i;
    try {j = Integer.parseInt (tf.getText ()); }
    catch (NumberFormatException nfe) { }
    if (j > 0) i = j;
    tf.setText (Integer.toString (i));
    return i;
  }

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == ok)
    { D.ns = prsi (tns, D.ns);
      D.N = prsi (tN, D.N);
      D.nf = prsi (tnf, D.nf);
      if (D.nf > D.histogramme.length) D.histogramme = new int [D.nf];
      D.retrace = T.retrace = true;
      D.repaint ();
      T.repaint ();
    }
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class table extends Panel {
  static final long serialVersionUID = 180131L;
  TextArea ta;
  dessin D;
  boolean retrace;

  public table (dessin D)
  { this.D = D;
    setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    add (ta = new TextArea ("", 20, 10, TextArea.SCROLLBARS_VERTICAL_ONLY));
    retrace = false;
  }

  public void paint (Graphics g)
  { if (retrace)
    { retrace = false;
      ta.setText (D.donnees.replace ('.', ','));
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

  public static void main (String [] args) {
    int w = 500;
    int h = 400;

    try {
      if (args.length >= 1) {
        w = h = Integer.parseInt (args [0]);
      }
      if (args.length >= 2) {
        h = Integer.parseInt (args [1]);
      }
    }
    catch (NumberFormatException e) {}

    d2 d = new d2 ();
    d.init();
    d.start();

    Frame f = new Frame ("d2");
    f.addWindowListener (new fermer ());
    f.add (d);
    f.setSize (w, h);
    f.setVisible (true);
  }

  protected static final class fermer extends WindowAdapter {
    public void windowClosing (WindowEvent e) {
      System.exit (0);
    }
  }

}
