/* calculpi.java - jpq - 02/12/99
 * modification et ajout de main() le 09/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.Random;


public class calculpi extends java.applet.Applet {
  static final long serialVersionUID = 180209L;
  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 n = gparmi ("n", 10);
    D = new dessin (n);
    C = new controles (D);
    add (C, BorderLayout.NORTH);
    add (D, BorderLayout.CENTER);
  }

  public void destroy ()
  { remove (D); remove (C); }

  public String getAppletInfo ()
  { return "calculpi par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////
protected class dessin extends Canvas {
  static final long serialVersionUID = 180209L;
  Image img;
  Graphics g;
  int n, L, H, C, C2, pi;
  boolean retrace, modeplus;
  Random rnd;

  public dessin (int n)
  { this.n = n;
    rnd = new Random ();
    setFont (new Font ("Arial", Font.PLAIN, 10));
  }

  private void alea (int n)
  { double x = rnd.nextDouble ();
    double y = rnd.nextDouble ();
    if (x * x + y * y <= 1)
    { pi ++;
      g.setColor (Color.blue);
    }
    else g.setColor (Color.green);
    g.fillRect ((int)(x * C) - 1, C + 20 - (int)(y * C), 3, 3);
    g.setColor (Color.white);
    g.fillRect (0, 0, L, 20);
    g.setColor (Color.black);
    double q = (double)(pi * 4) / (double)(n);
    g.drawString ("n = " + Integer.toString (n) + "; p = " + Integer.toString (pi) + "; 4 p / n = " + Double.toString (q), 10, 15);
  }

  public void update (Graphics g1)
  { paint (g1); }

  public void paint (Graphics g1)
  { if (img == null || L != getSize().width || H != getSize().height)
    { L = getSize().width; 
      H = getSize().height;
      img = createImage (L, H);
      g = img.getGraphics ();
      C = Math.min (L, H) - 22;
      C2 = C + C;
      retrace = true;
    }
    if (retrace)
    { retrace = false;
      g.setColor (Color.white);
      g.fillRect (0, 0, L, H);

      g.setColor (Color.black);
      g.drawRect (0, 20, C, C);

      g.setColor (Color.blue);
      g.drawArc (- C, 20, C2, C2, 0, 90);
      
      pi = 0;
      for (int i = 1; i <= n; i ++)
      { alea (i);
        g1.drawImage (img, 0, 0, this);
      }
    }
    else if (modeplus)
    { modeplus = false;
      alea (n);
    }
    g1.drawImage (img, 0, 0, this);
  }
}
////////////////////////////////////////////////////////////////////////////////
protected class controles extends Panel implements ActionListener {
  static final long serialVersionUID = 180209L;
  dessin D;
  TextField 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);
    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.n = maj (tn, D.n);
      tn.setText (Integer.toString (D.n));
      D.retrace = true;
      D.repaint ();
    }
    else if (e.getSource () == plus)
    { D.n ++;
      tn.setText (Integer.toString (D.n));
      D.modeplus = true;
      D.repaint ();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {
        int w = 340;
        int h = 380;

// Création et lancement de l'applet
        calculpi p = new calculpi ();
        p.init ();
        p.start ();

// Création de la fenêtre contenant l'applet
        Frame f = new Frame ("calculpi");
        f.addWindowListener (new fermer ());
        f.add (p);
        f.setSize (w, h);
        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);
        }
    }

}
