/* s2des.java - jpq - 30/08/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 s2des extends java.applet.Applet {
  static final long serialVersionUID = 180214L;
  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 Ndes = gparmi ("nd", 2);
    int Nsimul = gparmi ("ns", 100);
    D = new dessin (Ndes, Nsimul);
    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); }

  public String getAppletInfo ()
  { return "s2des par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Panel {
  static final long serialVersionUID = 180214L;
  Image img;
  Graphics g;
  int w, h;
  int Ndes, Nsimul, bsup;
  int histogramme [];
  Random r;
  boolean retrace;

  public dessin (int Ndes, int Nsimul)
  { this.Ndes = Ndes;
    this.Nsimul = Nsimul;
    r = new Random ();
    retrace = false;
    bsup = 5 * Ndes + 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 (Integer.toString (j + Ndes), 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 () * 6.0); }

  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 som = 0;
        for (int j = 0; j < Ndes; j ++) som += c16 ();
        histogramme [som] ++;
        h (g, i);
        g1.drawImage (img, 0, 0, this);
      }
	  T.retrace = true;
      T.repaint ();

    }
    else g1.drawImage (img, 0, 0, this);
//    else h (g, Nsimul -1);
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180214L;
  dessin D;
  table T;
  TextField tNdes, tNsimul;
  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);
    String s = "Lancer de " + Integer.toString (D.Ndes) + " dé";
    if (D.Ndes > 1) s = s + "s";
    add (ajoutlbl (s));
    add (ajoutlbl ("Nb simulations :"));
    add (tNsimul = new TextField (5));
    tNsimul.setFont (f);
    tNsimul.setText (Integer.toString (D.Nsimul));
    add (ok = new Button ("Ok"));
    ok.setFont (f);
    ok.addActionListener (this);
  }

  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));
      D.retrace = T.retrace = true;
      D.repaint ();
      T.repaint ();
    }
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class table extends Panel {
  static final long serialVersionUID = 180214L;
  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 = "somme\t";
      String s2 = "effectif\t";
      for (int i = 0; i < D.bsup; i ++)
      { s1 = s1 + Integer.toString (i + D.Ndes) + "\t";
        s2 = s2 + Integer.toString (D.histogramme [i]) + "\t";
      }
      ta.setText (s1 + "\n" + s2);
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {

        s2des a = new s2des();
        a.init ();
        a.start ();

        Frame f = new Frame ("s2des");
        f.addWindowListener (new fermer ());
        f.add (a);
        f.setSize (400, 400);
        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);
        }
    }

}
