/* simulnais.java - jpq - 30/08/99
 * modification et ajout de main() le 12/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class simulnais extends java.applet.Applet {
  static final long serialVersionUID = 180212L;
  controles C;
  dessin D;
  table T;

  private int gparmi (String s, int i) {
    try {
      s = getParameter (s);
      i = Integer.parseInt (s);
    }
    catch (NumberFormatException e) {}
    catch (NullPointerException e) {}
    return i;
  }

  public void init ()
  { setLayout (new BorderLayout ());
    int max = gparmi ("max", 4);
    int Nsimul = gparmi ("ns", 20);
    int dt = gparmi ("dt", 50);
    D = new dessin (max, Nsimul, dt);
    T = new table (D);
    C = new controles (D, T);
    add ("North", C);
    add ("South", T);
    add ("Center", D);
  }

  public void destroy ()
  { remove (D); remove (C); remove (T); }

  public String getAppletInfo ()
  { return "simulnais par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Panel {
  static final long serialVersionUID = 180212L;
  Image img;
  Graphics g;
  int w, h;
  int max, Nsimul, bsup, dt;
  int histogramme [];
  Random r;
  boolean retrace;

  public dessin (int max, int Nsimul, int dt)
  { this.max = max;
    this.Nsimul = Nsimul;
    this.dt = dt;
    r = new Random ();
    retrace = false;
    bsup = max + 1;
    histogramme = new int [bsup];
  
  }

  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 + 1), 5, 40);

    for (int j = 0; j < bsup; j ++)
    { int x = j * (getSize().width - 50) / bsup + 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 (FFG (j), x, getSize().height - 10);
    }
      g.setColor (Color.black);
      g.drawRect (0, 0, getSize().width - 1, getSize().height - 1);
  }

  public String FFG (int j)
  { String s = "";
    for (int k = 0; k < j; k ++) s = s + "F";
    if (j < max) s = s + "G";
    return s;    
  }

  public void update (Graphics g)
  { paint (g); }

  public void paint (Graphics g1) {
    if (img == null || w != getSize().width || h != getSize().height) {
      w = getSize().width;
      h = getSize().height;
	  img = createImage (w, h);
      g = img.getGraphics ();
      g.setColor (Color.white);
      g.fillRect (1, 1, w - 2, h - 2);
      g.setColor (Color.black);
      g.drawRect (0, 0, w - 1, h - 1);
	  retrace = true;
    }
    if (retrace)
    { retrace = false;
      for (int i = 0; i < bsup; i ++) histogramme [i] = 0;
      for (int i = 0; i < Nsimul; i ++)
      { int j = 0;
        for (j = 0; j < max; j ++) if (r.nextDouble () < 0.5) break;
        histogramme [j] ++;
        h (g, i);
        g1.drawImage (img, 0, 0, this);
        try { Thread.sleep (dt); }
        catch (InterruptedException e) {}
      }
    }
    else g1.drawImage (img, 0, 0, this);
//    else h (g, Nsimul -1);
   T.retrace = true;
   T.repaint();
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180212L;
  dessin D;
  table T;
  TextField tNsimul, tdt;
  Button ok;
  Font f;

  private Label ajoutlbl (String s)
  { Label lbl = new Label (s);
    lbl.setBackground (Color.lightGray);
    lbl.setFont (f);
    return lbl;
  }

  private TextField ajouttf (int n)
  { TextField tf = new TextField (Integer.toString (n));
    tf.setFont (f);
    return tf;
  }

  public controles (dessin D, table T)
  { this.D = D;
    this.T = T;
    setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    f = new Font ("Arial, Helvetica", Font.PLAIN, 10);
    add (ajoutlbl ("Enfants : " + Integer.toString (D.max) + " Simulations :"));
    add (tNsimul = ajouttf (5));
    tNsimul.setText (Integer.toString (D.Nsimul));
    add (ok = new Button ("Ok"));
    ok.addActionListener (this);
    ok.setFont (f);
    add (ajoutlbl ("dt :"));
    add (tdt = ajouttf (2));
    tdt.setText (Integer.toString (D.dt));    
  }

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == ok)
    { try { D.Nsimul = Integer.parseInt (tNsimul.getText ()); }
      catch (NumberFormatException nfe) { }
      if (D.Nsimul <= 0) D.Nsimul = 1;
      tNsimul.setText (Integer.toString (D.Nsimul));
      try { D.dt = Integer.parseInt (tdt.getText ()); }
      catch (NumberFormatException nfe) { }
      if (D.dt < 0) D.dt = 0;
      tdt.setText (Integer.toString (D.dt));
      D.retrace = T.retrace = true;
      D.repaint ();
      T.repaint ();
    }
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class table extends Panel {
  static final long serialVersionUID = 180212L;
  TextArea ta;
  dessin D;
  boolean retrace;

  public table (dessin D)
  { this.D = D;
    setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    add (ta = new TextArea ("", 3, 50, TextArea.SCROLLBARS_HORIZONTAL_ONLY));
    retrace = false;
  }

  public void paint (Graphics g)
  { if (retrace)
    { retrace = false;
      String s1 = "famille\t";
      String s2 = "effectif\t";
      for (int i = 0; i < D.bsup; i ++)
      { s1 = s1 + D.FFG (i) + "\t";
        s2 = s2 + Integer.toString (D.histogramme [i]) + "\t";
      }
      ta.setText (s1 + "\n" + s2);
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {
        int w = 400;
        int h = 400;

// Création et lancement de l'applet
        simulnais p = new simulnais();
        p.init ();
        p.start ();

// Création de la fenêtre contenant l'applet
        Frame f = new Frame ("simulnais");
        f.addWindowListener (new fermer ());
        f.add (p);
        f.setSize (w, h);
        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);
        }
    }
}
