/* integmc.java - jpq - 18/02/02 - 03/03/02
 * détermination d'une valeur approchée d'une intégrale
 * par la méthode de montecarlo
 * modification et ajout de main() le 08/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;

public class integmc extends Applet implements MouseListener, MouseMotionListener, ActionListener {
  static final long serialVersionUID = 180208L;
  Image img;
  Graphics g;
  Repere R;
  pointlibre O, I;
  TextField tf, /* tux, tuy, */ ta, tb, tc, td, tN;
  Button ok, simul, pe;
  StringTokenizer st;
  double [] liste, pile, xalea, yalea;
  int nliste;
  int max, NN /*, dt*/;
  int [] Yf;
  boolean erreur, der, dernbr, resval, bsimul, paffpan, balea [], glisse;
  int X, Y, Xp, Yp, Xret, Yret, N;
  int modepaint = -1;
  int npile;
  Random rnd;
  double ux, uy, a, b, c, d;
  String sux = "ux", suy = "uy";
  pt pta, ptb, ptc, ptd;
  Segment sa, sb, sc, sd;
  Panel p;
  Font f;
  Color gristresclair;
  int X0, Y0, w, h;
//------------------------------------------------------------------------------
  private void analyse ()
  { erreur = false;
    st = new StringTokenizer (tf.getText (), " ()*+-/^,", true);
    nliste = 0;
    parse ();
  }

  private void parse ()
  { der = true;
    int i = 0;
    while (st.hasMoreTokens ())
    { if (p (i) == -1) break;
      i++;
    }
  }

  private int p (int i)
  { int resultat = -1; String s = "";
    while ((st.hasMoreTokens ()) && (s.length () == 0)) s = st.nextToken ().trim ();
    if (s.length () != 0)
    { try { addListe (Double.valueOf (s).doubleValue (), true); resultat = 0; der = false; }
      catch (NumberFormatException excp)
      { s = s.toUpperCase ();
        if (s.equals ("X"))
        { addListe (1.0, false);
          resultat = 0;
          der = false;
        }
        else if (s.equals ("PI"))
       { addListe (-1.0, false);
          resultat = 0;
          der = false;
        }
        else if (s.equals ("E"))
        { addListe (-2.0, false);
          resultat = 0;
          der = false;
        }
        else if (s.equals ("(")) { parse (); resultat = 0; }
        else if (s.equals (",")) { parse (); resultat = 0; }
        else if (s.equals (")")) { der = false; }
        else
        { int op = nop (s);
          if ((op == -22) && (i == 0)) addListe (0.0, true);
          if ((op == 14) || (op == 15))
          { p (i);
            addListe ((double) op, false);
          }
          else
          { int opt = Math.abs (op);
            int opa = (i > 0) ? (int) liste [nliste - 1] : 0;
            int opat = Math.abs (opa); if ((opat < 20) || !der) opat = 0;
            int opb = 0; int opbt = 0;
            if ((opt >= 20) && (opt < opat))
            { nliste--;
              if (nliste >= 1) opb = (int) liste [nliste - 1];
              opbt = Math.abs (opb); if (opbt < 20) opbt = 0;
              if ((opt < opbt) && (opbt < opat)) nliste--;       
            }
            boolean b = der;
            resultat = p (i);
            addListe ((double) op, false);
            der = b;
            if (opt >= 20)
            { if ((opt < opbt) && (opbt < opat)) addListe ((double) opb, false);            
              if (opt < opat) addListe ((double) opa, false);
            }
            der = true;
          }
        }
      }
    }
    return resultat;
  }

  private int nop (String s)
  { int r = 0;
    if (s.equals ("ABS")) r = 2;
    else if (s.equals ("SQRT")) r = 3;
    else if (s.equals ("RAC")) r = 3;
    else if (s.equals ("LN")) r = 4;
    else if (s.equals ("EXP")) r = 5;
    else if (s.equals ("SIN")) r = 6;
    else if (s.equals ("COS")) r = 7;
    else if (s.equals ("TAN")) r = 8;
    else if (s.equals ("ASIN")) r = 9;
    else if (s.equals ("ACOS")) r = 10;
    else if (s.equals ("ATAN")) r = 11;
    else if (s.equals ("INT")) r = 12;
    else if (s.equals ("RND")) r = 13;
    else if (s.equals ("MAX")) r = 14;
    else if (s.equals ("MIN")) r = 15;
    else if (s.equals ("^")) r = 20;
    else if (s.equals ("*")) r = 21;
    else if (s.equals ("/")) r = -21;
    else if (s.equals ("+")) r = 22;
    else if (s.equals ("-")) r = -22;
    if (r == 0) erreur = true;
    return r;
  }

  private void addListe (double x, boolean b) /* b = true : x == nombre */
  { if (nliste >= max - 1)
      if (liste == null)
      { max = 20;
        liste = new double [max];
        pile = new double [max];
      }
      else
      { max *= 2;
        double nf [] = new double [max];
        System.arraycopy (liste, 0, nf, 0, liste.length);
    	  liste = nf;
        pile = new double [max];
      }
    if (b) liste [nliste++] = 0.0;
    liste [nliste++] = x;
  }
//------------------------------------------------------------------------------
  private void ccourbe ()
  { for (int Xf = 0; Xf < R.XMAX; Xf ++)
    { if (!erreur)
      { double d = image (R.Abs (Xf));
        if (resval) Yf [Xf] = R.Iord (d); else Yf [Xf] = -1;
      }
      else Yf [Xf] = -1;
    }
  }

  private double image (double x)
  { resval = true;
    if ((liste == null) || (nliste == 0)) { resval = false; return Double.NaN; }
    else
    { npile = 0;
      int i = 0;
      while ((i < nliste) && (!erreur) && (resval))
      { int id = (int) liste [i++];
        if (id == 0) pile [npile++] = liste [i++];
        else if (id == 1) pile [npile++] = x;
        else if (id == -1) pile [npile++] = Math.PI;
        else if (id == -2) pile [npile++] = Math.E;
        else if ((id >= 2) && (id < 14))
        { if (npile == 0) erreur = true;
          else
          { double d = pile [-- npile];
            switch (id)
            { case 2 : d = Math.abs (d); break;
              case 3 : resval = (d >= 0.0); d = Math.sqrt (d); break;
              case 4 : resval = (d >= 0.0); d = Math.log (d); break;
              case 5 : d = Math.exp (d); break;
              case 6 : d = Math.sin (d); break;
              case 7 : d = Math.cos (d); break;
              case 8 : d = Math.tan (d); break;
              case 9 : resval = (d >= -1.0) && (d <= 1.0); d = Math.asin (d); break;
              case 10 : resval = (d >= -1.0) && (d <= 1.0); d = Math.acos (d); break;
              case 11 : d = Math.atan (d); break;
              case 12 : d = Math.floor (d); break;
              case 13 : d = rnd.nextDouble() * d;
            }
            pile [npile ++] = d;
          }
        }
        else
        { if (npile < 2) erreur = true;
          else
          { double d1 = pile [npile - 2];
            double d2 = pile [--npile];
            switch (id)
            { case 14 : d1 = Math.max (d1, d2); break;
              case 15 : d1 = Math.min (d1, d2); break;
              case 20 : try { d1 = Math.pow (d1, d2); }
                        catch (ArithmeticException ae) {resval = false; }
                        break;
              case 21 : d1 *= d2; break;
              case -21 : resval = (d2 != 0.0); d1 /= d2; break;
              case 22 : d1 += d2; break;
              case -22 : d1 -= d2;
            }
            pile [npile - 1] = d1;
          } 
        }
      }
      if (!resval) return Double.NaN;
      else if (npile != 1) { erreur = true; return Double.NaN; }
      else return pile [0];
    }
  }
//------------------------------------------------------------------------------
  private TextField ajoutf (Panel p, String s, double d)
  { Label lbl = new Label (s);
    lbl.setFont (f);
    p.add (lbl);
    TextField tf = new TextField (Double.toString (d), 5);
    tf.setFont (f);
    p.add (tf);
    return tf;
  }

  private double gparm (String s, double d) {
    try {
	  s = getParameter (s);
	  d = Double.parseDouble (s);
	}
    catch (NumberFormatException e) {}
	catch (NullPointerException e) {}
    return 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 ()
  { f = new Font ("Arial", Font.PLAIN, 10);
    gristresclair = new Color (224, 224, 224);
//    setBackground (Color.lightGray);
    p = new Panel ();
    p.setLayout (new GridLayout (2,1) );
    p.setBackground (Color.lightGray);
    add (p);
    Panel ph = new Panel ();
    Panel pb = new Panel ();
    ph.setBackground (Color.lightGray);
    pb.setBackground (Color.lightGray);
    p.add (ph);
    p.add (pb);
    pb.add (pe = new Button (""));
    pe.setFont (f);

    ux = gparm (sux, 200.0);
    uy = gparm (suy, 50.0);
	X0 = gparmi ("XO", 160);
	Y0 = gparmi ("YO", 350);

    R = new Repere (X0, Y0, 520, 390, ux, uy);
    O = new pointlibre (0.0, 0.0, "O", R);
    I = new pointlibre (1.0, 1.0, "I", R);

    Label lbl = new Label ("f (x) =");
    lbl.setFont (f);
    ph.add (lbl);
    ph.add (tf = new TextField (30));
    tf.setFont (f);
//    tux = ajoutf (ph, sux, ux);
//    tuy = ajoutf (ph, suy, uy);
    a = gparm ("a", 0.0);
    b = gparm ("b", 1.0);
    c = gparm ("c", 0.0);
    d = gparm ("d", 4.0);
    ta = ajoutf (pb, "a", a);
    tb = ajoutf (pb, "b", b);
    tc = ajoutf (pb, "c", c);
    td = ajoutf (pb, "d", d);
    pta = new pt (a, c, "a", R);
    ptb = new pt (b, c, "b", R);
    ptc = new pt (a, c, "c", R);
    ptd = new pt (a, d, "d", R);
    sa = new Segment (a, c, a, d,"", R);
    sb = new Segment (b, c, b, d,"", R);
    sc = new Segment (a, c, b, c,"", R);
    sd = new Segment (a, d, b, d,"", R);
    pb.add (ok = new Button ("Ok"));
    ok.setFont (f);
    lbl = new Label ("N =");
    lbl.setFont (f);
    ph.add (lbl);
    ph.add (tN = new TextField (5));
    tN.setFont (f);
	tN.setText ("1000");
    ph.add (simul = new Button ("simule"));
    simul.setFont (f);
    rnd = new Random ();
    Yf = new int [R.XMAX];
    String s = "4/(x^2+1)";
	tf.setText (s);
	try {
	  s = getParameter ("fonction");
      if ((s != null) && (s.length() != 0)) {
	    tf.setText (s);
        analyse ();
        ccourbe ();
	  } else
	    for (int Xf = 0; Xf < R.XMAX; Xf ++)
		  Yf [Xf] = -1;
    }
	catch (NullPointerException e) {
	  for (int Xf = 0; Xf < R.XMAX; Xf ++)
		  Yf [Xf] = -1;
	}
    
//    dt = 0;
//    s = getParameter ("dt");
//    if (s != null)
//    { try {dt = Integer.parseInt (s); }
//      catch (NumberFormatException nfe) { }
//    }
//    if (dt < 0) dt = 0;
    modepaint = 1;
    addMouseMotionListener (this);
    addMouseListener (this);
    ok.addActionListener (this);
    pe.addActionListener (this);
    simul.addActionListener (this);
  }

  public void update (Graphics g1)
  { paint (g1); }

  public void paint (Graphics g1) {
    if (img == null || w != getSize().width || h != getSize().height) {
	  w = getSize().width;
      h = getSize().height;
      img = createImage (w, h);
	  g = img.getGraphics();
	  R.Repere (R.X0, R.Y0, w, h, R.unitex, R.unitey);
      O.point (0.0, 0.0);
      I.point (1.0, 1.0);
	  modepaint = 0;
	  Yf = new int [R.XMAX];
	  for (int Xf = 0; Xf < R.XMAX; Xf ++)
	    Yf [Xf] = -1;
	}
	g.setFont (f);
    g.setColor (Color.white);
    g.fillRect (0, 0, R.XMAX, R.YMAX);

    boolean bl = (modepaint == 2);
    if (bl) modepaint = 0;

    if (modepaint == 0)
      { if ((O.deplace) || (I.deplace))
        { if (O.deplace) R.Repere (R.Iabs (O.x), R.Iord (O.y), getSize().width, getSize().height, R.unitex, R.unitey);
          else
          { double Ix = I.x; double Iy = I.y;
            if ((Ix > 0.0) && (Iy > 0.0))
            { ux = (double) (R.Iabs (Ix) - R.X0);
              uy = (double) (R.Y0 - R.Iord (Iy));
              R.Repere (R.X0, R.Y0, w, h, ux, uy);
//              tux.setText (Double.toString (ux));
//              tuy.setText (Double.toString (uy));
            }
          }
          O.point (0.0, 0.0);
          I.point (1.0, 1.0);
        }
      ccourbe ();
      }

/*    g.setColor (Color.black);
    if (modepaint == 1)
      if (X > 0)
      { g.drawString ("point mobile", X, Y);
        Xp = X; Yp = Y; X = -1;
      }
      else
      { g.drawLine (Xret - 5, Yret, Xret + 5, Yret);
        g.drawLine (Xret, Yret - 5, Xret, Yret + 5);
      }

    g.drawString ("(" + Double.toString (R.Abs(Xret)) + ", " + Double.toString (R.Ord(Yret)) + ")", 2, getSize().height - 20);
*/
    if (modepaint >= 0)
    { g.setColor (Color.red);
      R.trace (g);
      O.trace (g);
      I.trace (g);
      if (erreur) g.drawString ("Erreur de syntaxe", 2, getSize().height - 10);
      else
      { g.setColor (Color.blue);
        for (int X = 0; X < R.XMAX; X ++)
        { int Y = Yf [X];
          g.drawLine (X, Y, X, Y);
        }
      }
      g.setColor (Color.green);
      sa.Segment (a, c, a, d);
      sb.Segment (b, c, b, d);
      sc.Segment (a, c, b, c);
      sd.Segment (a, d, b, d);
      sa.trace (g);
      sb.trace (g);
      sc.trace (g);
      sd.trace (g);
      pta.point (a, c);
      ptb.point (b, c);
      ptc.point (a, c);
      ptd.point (a, d);
      g.drawString ("a", pta.X, pta.Y + 10);
      g.drawString ("b", ptb.X, ptb.Y + 10);
      g.drawString ("c", ptc.X - 10, ptc.Y);
      g.drawString ("d", ptd.X - 10, ptd.Y);
// teste l'analyseur
//      String z = "";
//      for (int i = 0; i < nliste; i++) z = z + Double.toString (liste [i]) + " ";
//      g.drawString (z, 200, getSize().height - 10);
      if (bsimul)
      { bsimul = false;
        if ((N > 0) && ((xalea == null) || (N > xalea.length)))
        { xalea = new double [N];
          yalea = new double [N];
          balea = new boolean [N];
        }
        NN = 0;
        for (int i = 0; i < N; i ++)
        { double x = a + rnd.nextDouble () * (b - a);
          double y = c + rnd.nextDouble () * (d - c);
          xalea [i] = x;
          yalea [i] = y;
          double y1 = image (x);
          if (!resval) break;
          if (balea [i] = (y < y1))
          { NN ++;
            g.setColor (Color.cyan);
          }
          else g.setColor (Color.green);
          g.fillRect (R.Iabs (x) - 1, R.Iord (y) - 1, 3, 3);
          g1.drawImage (img, 0, 0, this);
//          if (dt > 0)
//          { try { Thread.sleep (dt); }
//            catch (InterruptedException e) { }
//          }
        }
      }
      else if ((N > 0) && (xalea != null) && (! glisse))
      { g.setColor (Color.cyan);
        for (int i = 0; i < N; i ++)
         if (balea [i])
          g.fillRect (R.Iabs (xalea [i]) - 1, R.Iord (yalea [i]) - 1, 3, 3);
        g.setColor (Color.green);
        for (int i = 0; i < N; i ++)
         if (! balea [i])
          g.fillRect (R.Iabs (xalea [i]) - 1, R.Iord (yalea [i]) - 1, 3, 3);
      }
      if (N > 0)
      { g.setColor (Color.black);
        double NNsN = (double)(NN) / (double)(N);
        g.drawString ("n = " + Integer.toString (NN)
                      + "; n / N = " + Double.toString (NNsN)
                      + "; n / N x |(b-a) (d-c)| = "
                      + Double.toString (NNsN * Math.abs((b - a) * (d - c))), 2, getSize().height - 20);
      }
    }

    g.setColor (Color.black);
//    R.cadre (g);
    if (paffpan)
    { g.setColor (Color.black);
      g.fillRect (2, 2, 8, 8);
      g.setColor (gristresclair);
      g.fillRect (2, 2, 7, 7);
      g.setColor (Color.gray);
      g.fillRect (3, 3, 6, 6);
      g.setColor (Color.lightGray);
      g.fillRect (3, 3, 5, 5);
    }
    if (bl) modepaint = 1;
    g1.drawImage (img, 0, 0, this);
  }

// on teste si le clic de souris est proche d'un point libre

  public void mousePressed (MouseEvent e)
  { e.consume ();
    if ((e.getX () < 10) && (e.getY () < 10) && paffpan)
    { p.setVisible (true);
      paffpan = false;
    }

    if (O.zone (e.getX (), e.getY ()) || I.zone (e.getX (), e.getY ()))
    { modepaint = 0;
      X = -1;
    }
  }

  public void mouseDragged (MouseEvent e)
  { e.consume ();
    O.bouge (e.getX (), e.getY ());
    I.bouge (e.getX (), e.getY ());
    glisse = true;
    repaint ();
  }

  public void mouseReleased (MouseEvent e)
  { e.consume ();
    O.deplace = I.deplace = false; modepaint = 1;
    glisse = false;
    repaint ();
  }

  public void mouseMoved (MouseEvent e)
  { /* e.consume ();
    if (modepaint == 1)
    { if (O.zone (e.getX (), e.getY ()) || I.zone (e.getX (), e.getY ()))
      { this.X = e.getX () + 2; this.Y = e.getY () + 2;
        O.deplace = I.deplace = false;
      }
      Xret = e.getX (); Yret = e.getY ();
      repaint ();
    } */
  }

  private double ltf (TextField tf, double d)
  { try {d = new Double (tf.getText ()).doubleValue (); }
    catch (NumberFormatException nfe) { }
    return d;
  }

  public void actionPerformed (ActionEvent e)
  { if ((e.getSource () == ok) || (e.getSource () == simul) || (e.getSource () == pe))
    { /*  double d = ltf (tux, ux);
      if (d > 0.0) ux = d;
      d = ltf (tuy, uy);
      if (d > 0.0) uy = d; */
      a = ltf (ta, a);
      b = ltf (tb, b);
      c = ltf (tc, c);
      d = ltf (td, d);
      int nN = N;
      try { nN = Integer.parseInt (tN.getText ()); }
      catch (NumberFormatException nfe) { }
      if (nN >= 0) N = nN;
//      tux.setText (Double.toString (ux));
//      tuy.setText (Double.toString (uy));
      ta.setText (Double.toString (a));
      tb.setText (Double.toString (b));
      tc.setText (Double.toString (c));
      td.setText (Double.toString (d));
      tN.setText (Integer.toString (N));
      R.Repere (R.X0, R.Y0, getSize().width, getSize().height, ux, uy);
      I.point (1.0, 1.0);
      if (e.getSource () == simul) bsimul = true;
      else if (e.getSource () == ok)
      { bsimul = true;
        N = 0;
      }
      else if (e.getSource () == pe)
      { p.setVisible (false);
        paffpan = true;
      }
      analyse ();
      modepaint = 2;
      repaint ();
    }
  }

  public void mouseClicked (MouseEvent e) { }
  public void mouseEntered (MouseEvent e) { }
  public void mouseExited (MouseEvent e) { }

  public String getAppletInfo ()
  { return "integmc v1 par j.-p. Quelen"; }
////////////////////////////////////////////////////////////////////////////////
protected class Repere
{ public int X0;
  public int Y0;
  public int XMAX;
  public int YMAX;
  public double unitex;
  public double unitey;

  public Repere (int X0,int Y0,int XMAX,int YMAX,double unitex,double unitey)
  { this.X0 = X0; this.Y0 = Y0;
    this.XMAX = XMAX; this.YMAX = YMAX;
    this.unitex = unitex; this.unitey = unitey;
  }

  public void trace (Graphics g)
  { int UX = X0 + new Double (unitex) . intValue ();
    int UY = Y0 - new Double (unitey) . intValue ();
    if ((Y0 >= 0) && (Y0 <= YMAX)) { g.drawLine (0, Y0, XMAX, Y0);
                                        if ((UX >= 0) && (UX <= XMAX))
                                              g.drawLine (UX, Y0 - 2, UX, Y0 + 2);
                                      } 
    if ((X0 >= 0) && (X0 <= XMAX)) { g.drawLine (X0, 0, X0, YMAX);
                                        if ((UY >= 0) && (UY <= YMAX))
                                              g.drawLine (X0 - 2, UY, X0 + 2, UY);
                                      }
    }

  public void cadre (Graphics g)
  { g.drawRect (0, 0, XMAX - 1, YMAX - 1); }

  public int Iabs (double x)
  { return (int) (x * unitex + X0); }

  public int Iord (double y)
  { return (int) (- y * unitey + Y0); }

  public double Abs (int X)
  { return (X - X0) / unitex; }

  public double Ord (int Y)
  { return (Y0 - Y) / unitey; }

  public boolean affiche (int X, int Y)
  { return ((X >= 0) && (X <= XMAX) && (Y >= 0) && (Y <= YMAX)); }

  public void Repere (int X0,int Y0,int XMAX,int YMAX,double unitex,double unitey)
  { this.X0 = X0; this.Y0 = Y0;
    this.XMAX = XMAX; this.YMAX = YMAX;
    this.unitex = unitex; this.unitey = unitey;
  }

}
////////////////////////////////////////////////////////////////////////////////
protected class pt
{ public boolean defini;
  public String Nom;
  public Repere R;
  public int X, Y;
  public double x;
  public double y;

  public pt (double x, double y, String Nom, Repere R)
  { this.Nom = Nom;
    this.R = R;
    this.x = x; this.y = y; 
    majXY (); defini = true; }

/**
 * Met à jour la position (X, Y) du point à l'aide des coordonnées (x, y).
 */

  public void majXY ()
  { X = R.Iabs (x); Y = R.Iord (y); }

  public void trace (Graphics g)
  { if (defini) { g.fillRect (X - 1, Y - 1, 3, 3);
                  if (Nom.length () != 0) g.drawString (Nom, X + 3, Y - 3); } }

 /**
 * Trace ou efface uniquement le Nom du point.
 *

  public void traceNom (Graphics g)
  { if (defini && (Nom.length () != 0)) g.drawString (Nom, X + 3, Y - 3); } */

public void point (double x, double y)
  { this.x = x; this.y = y; majXY (); defini = true; }
}
////////////////////////////////////////////////////////////////////////////////
protected class pointlibre extends pt
{ public int Xp, Yp;
  public boolean deplace;

  public pointlibre (double x, double y, String Nom, Repere R)
  { super (x, y, Nom, R); Xp = this.X; Yp = this.Y; deplace = false; }

  public boolean zone (int X, int Y)
  { if (deplace = (X >=  this.X - 2) && (X <= this.X + 2) && (Y >= this.Y - 2) && (Y <= this.Y + 2))
    Xp = this.X; Yp = this.Y;
    return deplace; }

  public void bouge (int X, int Y)
  { if (deplace && this.R.affiche (X, Y)) { this.X = X; x = this.R.Abs (X);
                                       this.Y = Y; y = this.R.Ord (Y);
                                     }
  }

 public void point ()
 { majXY (); Xp = this.X; Yp = this.Y; }
}
////////////////////////////////////////////////////////////////////////////////
protected class Segment
{ public boolean defini;
  public String Nom;
  public Repere R;
  public int AX, AY, BX, BY;
  public double Ax, Ay, Bx, By;

/*  public Segment (String Nom, Repere R)
  { this.Nom = Nom;
    this.R = R; }

  public Segment (pt A, pt B, String Nom, Repere R)
  { this.Nom = Nom;
    this.R = R;
    Segment (A, B); } */

  public Segment (double Ax, double Ay, double Bx, double By, String Nom, Repere R)
  { this.Nom = Nom;
    this.R = R;
    Segment (Ax, Ay, Bx, By); }

/*  public double Longueur ()
  { if (defini) { double u = Bx - Ax; double v = By - Ay;
                  return Math.sqrt (u * u + v * v);
                }
    else        return -1.0;
  } */

  public void trace (Graphics g)
  { if (defini)
      { g.drawLine (AX, AY, BX, BY);
        if (Nom.length () != 0) g.drawString (Nom, (AX + BX) / 2 + 3, (AY + BY) / 2 - 3);
      }
  }

/*   public boolean zone (int X, int Y)
   { if (defini)
       { int A = BY - AY; int B = AX - BX;
         double d = ((double) Math.abs (A * X + B * Y + AY * BX - AX * BY)) / Math.sqrt ((double) (A * A + B * B));
         if ((d <= 4.0) && ((X - AX) * (X - BX) <= 0) && ((Y - AY) * (Y - BY) <= 0))
           return true;
         else return false;
       }
     return false;
   }

  public void Segment (pt A, pt B)
  { defini = A.defini && B.defini;
    if (defini) { AX = A.X; AY = A.Y; BX = B.X; BY = B.Y;
                  Ax = A.x; Ay = A.y; Bx = B.x; By = B.y;
                }
  } */

  public void Segment (double Ax, double Ay, double Bx, double By)
  { defini = true;
    if (defini) { this.Ax = Ax; this.Ay = Ay; this.Bx = Bx; this.By = By;
                  AX = R.Iabs (Ax); AY = R.Iord (Ay); BX = R.Iabs (Bx); BY = R.Iord (By);
                }
  }
}

////////////////////////////////////////////////////////////////////////////////

	public static void main (String [] args) {
		integmc a = new integmc();
		a.init();
		a.start();

		Frame f = new Frame ("integmc");
		f.addWindowListener (new fermer ());
		f.add (a);
		f.setSize (520, 390);
		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);
		}
	}

}