/* trar.java - jpq - 24/10/99
 * modification et ajout de main() le 19/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.Random;

public class trar extends java.applet.Applet {
  static final long serialVersionUID = 180219L;
  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 NTR = gparmi ("NTR", 5);
    int NTV = gparmi ("NTV", 3);
    int n = gparmi ("n", 1);
    D = new dessin (NTR, NTV, n);
    C = new controles (D);
    add (C, BorderLayout.NORTH);
    add (D, BorderLayout.CENTER);
  }

  public void destroy ()
  { remove (D); remove (C); }

  public String getAppletInfo ()
  { return "trar par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Canvas {
  static final long serialVersionUID = 180219L;
  Image img;
  Graphics g;
  int NTR, NTV, n, v, r;
  boolean retrace, modeplus;
  double p;
  int L, H;
  Random rnd;

  public dessin (int NTR, int NTV, int n)
  { this.NTR = NTR;
    this.NTV = NTV;
    p = (double)(NTR) / (double)(NTR + NTV);
    this.n = n;
    rnd = new Random ();
  }

  private void affiche (Graphics g, int a)
  { /* for (int i = 0; i < a; i ++)
    { int x = (int)(rnd.nextDouble () * (L - 220)) + 110;
      int y = (int)(rnd.nextDouble () * 20.0) + H - 80;
      g.fillRect (x, y, 3, 3);
    } */
    for (int i = 0; i < a; i ++)
    { int x = (int)(rnd.nextDouble () * (L - 220)) + 110;
      int y = (int)(rnd.nextDouble () * (H - 190)) + 60;
      g.fillRect (x, y, 3, 3);
    }
  }

  private void alea ()
  { if (rnd.nextDouble () <= p) r ++; else v ++; 
  }

  public void update (Graphics g)
  { paint (g); }

  public void paint (Graphics g1)
  { if (img == null || L + 2 != getSize().width || H + 2 != getSize().height)
    { L = getSize().width;
      H = getSize().height;
      img = createImage (L, H);
      g = img.getGraphics ();
      g.setColor (Color.black);
      g.drawRect (0, 0, -- L, -- H);
      L --; H --;
	  retrace = true;
    }
    g.setColor (Color.white);
    g.fillRect (1, 1, L, H);

    g.setColor (Color.blue);
    g.drawLine (L / 2 - 20, H - 100, L / 2 - 20, H - 120);
    g.drawLine (L / 2 - 20, H - 120, 100, H - 120);
    g.drawLine (100, H - 120, 100, 50);
    g.drawLine (100, 50, L - 100, 50);
    g.drawLine (L - 100, 50, L - 100, H - 120);
    g.drawLine (L - 100, H - 120, L / 2 + 20, H - 120);
    g.drawLine (L / 2 + 20, H - 120, L / 2 + 20, H - 100);

    if (retrace)
    { if (modeplus)
      { modeplus = false;
        alea ();
      }
      else
      { v = r = 0;
        for (int i = 0; i < n; i ++) alea ();
      }
      g.setColor (Color.red);
      affiche (g, NTR);
      g.drawString (Integer.toString (r) + " boule(s) rouge(s)", 20, H - 20);
      g.setColor (Color.green);
      affiche (g, NTV);
      g.drawString (Integer.toString (v) + " boule(s) verte(s)", L / 2, H - 20);
    }
    g1.drawImage (img, 0, 0, this);
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180219L;
  dessin D;
  TextField tNTR, tNTV, tn;
  Button ok, plus;

  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), 5);
    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);
    tNTR = ajouttf (f, "rouges :", D.NTR);
    tNTV = ajouttf (f, "vertes :", D.NTV);
    tn = ajouttf (f, "n =", D.n);
    add (ok = new Button ("Ok"));
    ok.addActionListener (this);
    add (plus = new Button ("+"));
    plus.addActionListener (this);
  }

  private int maj (TextField T, int n)
  { try { n = Integer.parseInt (T.getText ()); }
      catch (NumberFormatException nfe) { }
    if (n < 0) n = 0;
    return n;
  }

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == ok)
    { D.NTR = maj (tNTR, D.NTR);
      D.NTV = maj (tNTV, D.NTV);
      tNTR.setText (Integer.toString (D.NTR));
      tNTV.setText (Integer.toString (D.NTV));
      D.n = maj (tn, D.n);
      D.p = (double)(D.NTR) / (double)(D.NTR + D.NTV);
      tn.setText (Integer.toString (D.n));
      D.retrace = true;
      D.repaint ();
    }
    else if (e.getSource () == plus)
    { tNTR.setText (Integer.toString (D.NTR));
      tNTV.setText (Integer.toString (D.NTV));
      tn.setText (Integer.toString (++ D.n));
      D.retrace = D.modeplus = true;
      D.repaint ();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {

        trar a = new trar();
        a.init ();
        a.start ();

        Frame f = new Frame ("trar");
        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);
        }
    }

}
