/* tirageurne.java - jpq - 14/10/99
 * ajout de main() le 05/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 = 180205L;
  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 Math.max (i, 1);
  }

  public void init ()
  { setLayout (new BorderLayout ());
      int nt = gparmi ("n", 20);
    D = new dessin (311, 414, nt);
    C = new controles (D);
    add (C, BorderLayout.NORTH);
    add (D, BorderLayout.CENTER);
  }

  public void destroy ()
  { remove (D); remove (C); }

  public String getAppletInfo ()
  { return "tirageurne par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Canvas {
  static final long serialVersionUID = 180205L;
  Image img;
  Graphics g;
  int nB, nN, nt, b, n, Somme;
  boolean retrace, modeplus;
  int L, H;
  Random rnd;

  public dessin (int nN, int nB, int nt)
  { this.nN = nN;
    this.nB = nB;
    this.nt = nt;
    rnd = new Random ();
  }

  private void affiche (Graphics g, int a, int b, boolean couleur)
  { for (int i = 0; i < a; i ++)
    { int x = (int)(rnd.nextDouble () * (L - 220)) + 110;
      int y = (int)(rnd.nextDouble () * 20.0) + H - 80;
      if (couleur) g.fillOval (x, y, 7, 7); else g.drawOval (x, y, 7, 7);
    }
    for (int i = a; i < b; i ++)
    { int x = (int)(rnd.nextDouble () * (L - 220)) + 110;
      int y = (int)(rnd.nextDouble () * (H - 190)) + 60;
      if (couleur) g.fillOval (x, y, 7, 7); else g.drawOval (x, y, 7, 7);
    }
  }

  private void alea ()
  { if ((int)(rnd.nextDouble () * Somme --) + 1 <= nB - b) b ++; else n ++; 
  }

  public void update (Graphics g)
  { paint (g); }

  public void paint (Graphics g1)
  { if (img == null || L != getSize().width - 1 || H != getSize().height - 1)
    { 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 (100, H - 80, 100, H - 50);
    g.drawLine (100, H - 50, L - 100, H - 50);
    g.drawLine (L - 100, H - 50, L - 100, H - 80);
    if (retrace)
    { if (modeplus)
      { modeplus = false;
        alea ();
      }
      else
      { Somme = nB + nN;
        b = 0; n = 0;
        for (int i = 0; i < nt; i ++) if ((int)(rnd.nextDouble () * Somme --) + 1 <= nB - b) b ++; else n ++;
      }
      g.setColor (Color.black);
      affiche (g, b, nB, false);
      affiche (g, n, nN, true);
    }
    g1.drawImage (img, 0, 0, this);
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180205L;
  dessin D;
  TextField tnt;
  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);
    tnt = ajouttf (f, "n =", D.nt);
    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 nt = D.nt;
      try { nt = Integer.parseInt (tnt.getText ()); }
      catch (NumberFormatException nfe) { }
      if (nt <= 0) nt = 1;
      else if (nt >= 20) nt = 20;
      D.nt = nt;
      tnt.setText (Integer.toString (nt));
      D.retrace = true;
      D.repaint ();
    }
    else if (e.getSource () == plus)
    { if (D.nt < 20)
      { D.nt ++;
        D.modeplus = true;
        tnt.setText (Integer.toString (D.nt));
        D.retrace = true;
        D.repaint ();
      }
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

	public static void main (String [] args) {

// Création et lancement de l'applet
		tirageurne a = new tirageurne();
		a.init ();
		a.start ();

// Création de la fenêtre contenant l'applet
		Frame f = new Frame ("tirageurne");
		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 we) {
			System.exit (0);
		}
	}

}
