/* tirageurne.java - jpq - 24/10/99 - 06/02/02
 * ajout de main() le 01/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.Random;


public class tirageurne extends java.applet.Applet {
  static final long serialVersionUID = 180201L;
  controles C;
  dessin D;

  private int gparm (String s, int n) {
    try {
	  s = getParameter (s);
	  n = Integer.parseInt (s);
	}
	catch (NumberFormatException e) {}
    catch (NullPointerException e) {}
	return Math.max (n, 0);
  }

  public void init () {
	setLayout (new BorderLayout ());
    int NTR = gparm ("brouges", 35);
    int NTV = gparm ("bvertes", 65);
    int n = gparm ("n", 10);
    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 = 180201L;
  Image img;
  Graphics g;
  int NTR, NTV, n, v, r;
  boolean retrace, modeplus, dercoulrouge;
  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 ();
    retrace = true;
  }

  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.fillOval (x, y, 7, 7);
    }
  }

  private void alea ()
  { dercoulrouge = rnd.nextDouble () <= p;
    if (dercoulrouge) r ++; else v ++; 
  }

  public void update (Graphics g)
  { paint (g); }

  public void paint (Graphics g1)
  { if (img == null)
    { 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 --;
    }
    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);
    g.drawLine (L / 2 - 25, H - 85, L / 2 - 25, H - 80);
    g.drawLine (L / 2 - 25, H - 80, L / 2 + 25, H - 80);
    g.drawLine (L / 2 + 25, H - 80, L / 2 + 25, H - 85);

    if (retrace)
    { if (modeplus)
      { modeplus = false;
        alea ();
      }
      else
      { v = r = 0;
        for (int i = 0; i < n; i ++) alea ();
      }
      if (n > 0)
      { if (dercoulrouge) g.setColor (Color.red); else g.setColor (Color.green);
        g.fillOval (L /2 - 1, H - 87, 7, 7);
      }
      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 = 180201L;
  dessin D;
  TextField tn;
  Button ok, plus;

  public controles (dessin D)
  { this.D = D;
    setLayout (new FlowLayout());
    setBackground (Color.lightGray);
    Font f = new Font ("Arial", Font.PLAIN, 10);
    Label l = new Label ("n=");
    add (l);
    l.setBackground (Color.lightGray);
    l.setFont (f);
    add ( tn = new TextField (Integer.toString (D.n), 5));
    tn.setFont (f);
    add (ok = new Button ("Ok"));
    ok.addActionListener (this);
    add (plus = new Button ("+"));
    plus.addActionListener (this);
  }

  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == ok)
    { int nn = D.n;
      try { nn = Integer.parseInt (tn.getText ()); }
      catch (NumberFormatException nfe) { }
    if (nn >= 0) D.n = nn;
      tn.setText (Integer.toString (D.n));
      D.retrace = true;
      D.repaint ();
    }
    else if (e.getSource () == plus)
    { tn.setText (Integer.toString (++ D.n));
      D.retrace = D.modeplus = true;
      D.repaint ();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

  public static void main (String [] args) {

    tirageurne t = new tirageurne();
    t.init();
    t.start();

    Frame f = new Frame ("tirageurne");
    f.addWindowListener (new fermer ());
    f.add (t);
    f.setSize (400, 400);
    f.setVisible (true);
  }

  protected static final class fermer extends WindowAdapter {
    public void windowClosing (WindowEvent e) {
      System.exit (0);
    }
  }

}
