/* ruine.java - jpq - 9/12/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 ruine extends java.applet.Applet {
  static final long serialVersionUID = 180214L;
  controles C;
  dessin D;

  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 Nsimul = gparmi ("Nsimul", 10);
    int fa = gparmi ("fa", 6);
    int fc = gparmi ("fc", 4);
    D = new dessin (Nsimul, fa, fc);
    C = new controles (D);
    add (C, BorderLayout.NORTH);
    add (D, BorderLayout.CENTER);
  }

  public void destroy ()
  { remove (D); remove (C); }

  public String getAppletInfo ()
  { return "ruine par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Canvas {
  static final long serialVersionUID = 180214L;
  Image img;
  Graphics g;
  int w, h;
  Random rnd;
  int Nsimul, fa, fc;
  boolean retrace, courbe;

  public dessin (int Nsimul, int fa, int fc)
  { this.Nsimul = Nsimul;
    this.fa = fa;
    this.fc = fc;
    rnd = new Random ();
    courbe = true;
  }

  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 (getSize().width, getSize().height);
      g = img.getGraphics ();
	  retrace = true;
    }

    if (retrace)
    { retrace = false;
      int nfa = 0;
      String s = "";
      int xmax = getSize().width - 2;
      int ymax = getSize().height - 2;
      if (courbe) for (int i = 1; i <= Nsimul; i ++)
      { int somme = fa; int total = fa + fc;
        int x = xmax;
        int y = (10 - ymax) * fa / total + ymax;
        int pas = (ymax - 10) / total;
        int x0 = 2 , y0 = y;
        while ((somme > 0) && (somme < total))
        { if (x >= xmax)
          { g.setColor (Color.white);
            g.fillRect (1, 1, xmax, ymax);
            x = 2; x0 = 2;
            g.setColor (Color.blue);
            g.drawString (s, 10, 10);
            g.setColor (Color.black);
          }
          if (rnd.nextDouble () > 0.5) { somme ++; y -= pas; } else { somme --; y += pas; }
          if (x0 != 2) g.drawLine (x0, y0, x, y);
          x0 = x; y0 = y;
          x += 4;
          g1.drawImage (img, 0, 0, this);
        }
        if (somme == total) nfa ++;
        g.setColor (Color.white);
        g.fillRect (1, 1, xmax, 10);
        g.setColor (Color.blue);
        s = "parties gagnées par A : " + Integer.toString (nfa) + "; parties gagnées par C : " + Integer.toString (i - nfa);
        g.drawString (s, 10, 10);
      }
      else for (int i = 1; i <= Nsimul; i ++)
      { int somme = fa; int total = fa + fc;
        while ((somme > 0) && (somme < total)) somme += (rnd.nextDouble () > 0.5) ? 1 : -1;
        if (somme == total) nfa ++;
        g.setColor (Color.white);
        g.fillRect (1, 1, xmax, ymax);
        g.setColor (Color.blue);
        s = "parties gagnées par A : " + Integer.toString (nfa) + "; parties gagnées par C : " + Integer.toString (i - nfa);
        g.drawString (s, 10, 10);
        g1.drawImage (img, 0, 0, this);
      }
    }
      g1.drawImage (img, 0, 0, this);
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180214L;
  dessin D;
  TextField tNsimul, tfa, tfc;
  Button ok, affcourbe;
  String ac = "avec courbe";
  String sc = "sans courbe";

  private Button ajoutbouton (Font f, String s)
  { Button b = new Button (s);
    b.addActionListener (this);
    b.setFont (f);
    add (b);
    return b;
  }

  private TextField ajouttf (Font f, String s, int i)
  { Label l;
    add (l = new Label (s));
    l.setBackground (Color.lightGray);
    l.setFont (f);
    TextField T = new TextField (Integer.toString (i), 4);
    add (T);
    T.setFont (f);
    return T;
  }

  public controles (dessin D)
  { this.D = D;
    setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    Font f = new Font ("Arial", Font.PLAIN, 10);
    tNsimul = ajouttf (f, "Nsimul", D.Nsimul);
    tfa = ajouttf (f, "fortune de A", D.fa);
    tfc = ajouttf (f, "fortune de C", D.fc);
    ok = ajoutbouton (f, "Ok");
    affcourbe = ajoutbouton (f, sc);
  }

  private int maj (TextField tf, int n)
  { try { n = Integer.parseInt (tf.getText ()); }
      catch (NumberFormatException nfe) { }
      if (n < 1) n = 1;
      tf.setText (Integer.toString (n));
      return n;
  }

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == ok)
    { D.Nsimul = maj (tNsimul, D.Nsimul);
      D.fa = maj (tfa, D.fa);
      D.fc = maj (tfc, D.fc);
      D.retrace = true;
    }
    else if (e.getSource () == affcourbe)
    { D.courbe = ! D.courbe;
      if (D.courbe) affcourbe.setLabel (sc); else affcourbe.setLabel (ac);
    }
    D.repaint ();
  }
}

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {

        ruine a = new ruine();
        a.init ();
        a.start ();

        Frame f = new Frame ("ruine");
        f.addWindowListener (new fermer ());
        f.add (a);
        f.setSize (500, 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);
        }
    }

}
