/* roc.java - jpq - 26/08/07
 * modification et ajout de main() le 14/02/18
 */

import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import geo.*;

public class roc extends Applet implements MouseListener, MouseMotionListener, ActionListener {
  static final long serialVersionUID = 180214L;
  Image img;
  Graphics g;
  int X, Y, Xp, Yp;
  Repere R, R1, R2;
  Droite xpx, xpx1, xpx2;
  pt A1, B, M;
  PointSurDroite A, A2;
  pointlibre O, I, O1, O2;
  Segment S, S1, S2X, S2Y;
  normale cf, cf1;
  int modepaint;
  int gw, gh;
  Button ok;
  TextField tm, ts, tm1, ts1;
  int arrond;
  double darrond, m, s, m1, s1, unite;
  
////////////////////////////////////////////////////////////////////////////////

  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;
  }


  private TextField instaltf (String s, double d)
  { add (new Label (s + " ="));
    TextField tf = new TextField (new Double (d) . toString (), 4);
    add (tf);
    return tf;
  }

  public void init ()
  { setFont (new Font ("Arial", Font.BOLD, 12));
  	setBackground (Color.white);
    modepaint = 1;
    addMouseMotionListener (this);
    addMouseListener (this);
    m = gparm ("m", 1.0);
    tm = instaltf ("m", m);
    s = gparm ("s", 0.4);
    ts = instaltf ("s", s);
    m1 = gparm ("m1", 1.5);
    tm1 = instaltf ("m1", m1);
    s1 = gparm ("s1", 0.4);
    ts1 = instaltf ("s1", s1);
    arrond = gparmi ("dec", 4);
    if (arrond >= 0) darrond = Math.pow (10.0, arrond);
    add (ok = new Button ("Ok"));
    ok.addActionListener (this);
    unite = gparm ("unite", 150.0);
  }

  public void update (Graphics g1)
  { paint (g1); }

  private String sarrondi (double d)
  { if (arrond >= 0) d = Math.floor (d * darrond) / darrond;
    return Double.toString (d);
  }

  public void paint (Graphics g1)
  { if ((img == null) || (gw != getSize().width) || (gh != getSize().height)) {
	  gw = getSize().width;
  	  gh = getSize().height;
	  if (img == null) {
	int OX = gparmi ("OX", 20);
    R = new Repere (OX, gparmi ("OY", 150), getSize().width, getSize().height, unite, unite);
    O = new pointlibre (0.0, 0.0, "O", R);
    xpx = new Droite (0.0, 1.0, 0.0, "", R);
    A = new PointSurDroite (gparm("x", 0.5), 0.0, xpx, "x", R);
    I = new pointlibre (1.0, 1.0, "I", R);
    String sg = "";
	try {
	  sg = getParameter ("nom");
	}
	catch (NullPointerException e) {}
    if (sg == null)
	  sg = "";
    cf = new normale (sg, R, m, s);
    S = new Segment ("", R);
	int O1Y = gparmi ("O1Y", 350);
    R1 = new Repere (OX, O1Y, getSize().width, getSize().height, unite, unite);
    O1 = new pointlibre (0.0, 0.0, "O1", R1);
    xpx1 = new Droite (0.0, 1.0, 0.0, "", R1);
    A1 = new pt (A.x, A.y, "x", R1);
	try {
	  sg = getParameter ("nom1");
	}
    catch (NullPointerException e) {}
    if (sg == null)
	  sg = "";
    cf1 = new normale (sg, R1, m1, s1);
    S1 = new Segment ("", R1);
    double unite2 = gparm ("unite2", 150.0);
	int O2X = gparmi ("O2X", 400);
	int O2Y = gparmi ("O2Y", 250);
    R2 = new Repere (O2X, O2Y, getSize().width, getSize().height, unite2, unite2);
    xpx2 = new Droite (0.0, 1.0, 0.0, "", R2);
    O2 = new pointlibre (0.0, 0.0, "O2", R2);
    A2 = new PointSurDroite (1.0, 0.0, xpx2, "I", R2);
    S2X = new Segment (O2, A2, "", R2);
    S2Y = new Segment (0.0, 0.0, 0.0, 1.0, "", R2);
    B = new pt ("", R2);
    M = new pt ("", R2);

	  }
  	  img = createImage (gw, gh);
      g = img.getGraphics ();
      R.Repere (R.Iabs (O.x), R.Iord (O.y), gw, gh, R.unitex, R.unitey);
      R1.Repere (R1.Iabs (O1.x), R1.Iord (O1.y), gw, gh, R.unitex, R.unitey);
    }
    g.setColor (Color.white);
    g.fillRect (0, 0, R.XMAX, R.YMAX);

    if ((modepaint == 0) || (modepaint == 2))
    { if ((O.deplace) || (I.deplace) || (O1.deplace))
      { if (O.deplace) R.Repere (R.Iabs (O.x), R.Iord (O.y), getSize().width, getSize().height, R.unitex, R.unitey);
        else if (O1.deplace) R1.Repere (R1.Iabs (O1.x), R1.Iord (O1.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))
          { double dx = (double) (R.Iabs (Ix) - R.X0);
          	double dy = (double) (R.Y0 - R.Iord (Iy));
          	R.Repere (R.X0, R.Y0, getSize().width, getSize().height, dx, dy);
          	R1.Repere (R1.X0, R1.Y0, getSize().width, getSize().height, dx, dy);
          }
        }
      }
      else
      if (O2.deplace)
           R2.Repere (R2.Iabs (O2.x), R2.Iord (O2.y), getSize().width, getSize().height, R2.unitex, R2.unitey);
      else
      if (A2.deplace)
      { double d = (double) (R2.Iabs (A2.x) - R2.X0);
      	if (d > 10.0) R2.Repere (R2.Iabs (O2.x), R2.Iord (O2.y), getSize().width, getSize().height, d, d);
      }
      O.point (0.0, 0.0);
      I.point (1.0, 1.0);
      A.point ();
      O1.point (0.0, 0.0);
      A1.point (A.x, A.y);
      O2.point (0.0, 0.0);
      A2.point (1.0, 0.0);
      S2X.Segment (O2, A2);
      S2Y.Segment (0.0, 0.0, 0.0, 1.0);
      
      if (modepaint == 2) modepaint = 1;
    }

    g.setColor (Color.black);
    if (modepaint == 1)
      if (X > 0)
      { g.drawString ("point mobile", X, Y);
        Xp = X; Yp = Y; X = -1;
      }
    g.setColor (Color.cyan);
    int Xdeb = Math.max (A.X, 0);
    int Xfin = getSize().width;
    S.defini = true;
    for (int X = Xdeb; X < Xfin; X ++)
    { int Y = R.Iord (cf.image (R.Abs (X)));
    	S.Segment (X, R.Y0, X, Y);
      S.trace (g);
    }
    S2X.trace (g);

    g.setColor (Color.yellow);
    Xdeb = Math.max (A1.X, 0);
    Xfin = getSize().width;
    S1.defini = true;
    for (int X = Xdeb; X < Xfin; X ++)
    { int Y = R1.Iord (cf1.image (R1.Abs (X)));
    	S1.Segment (X, R1.Y0, X, Y);
      S1.trace (g);
    }
    S2Y.trace (g);


    g.setColor (Color.red);
    R.trace (g);
    O.trace (g);
    I.trace (g);
    A.trace (g);
    R1.trace (g);
    O1.trace (g);
    O2.trace (g);
    A2.trace (g);

    g.setColor (Color.blue);
    cf.trace (g);

    g.setColor (Color.green);
    cf1.trace (g);

    g.setColor (Color.lightGray);
    Xdeb = Math.max (A1.X, 0);
    Xfin = getSize().width * 2;
    for (int X = 0; X < Xfin; X ++)
    { double x = R2.Abs(X);
    	M.point (1.0 - cf.integre (x), 1.0 - cf1.integre (x));
      M.trace (g);
    }

    g.setColor (Color.black);
    double cfi = 1.0 - cf.integre (A.x);
    double cfi1 = 1.0 - cf1.integre (A1.x);
    B.point (cfi, cfi1);
    B.trace (g);
    A1.trace (g);
    R.cadre (g);

    double fx = cf.image (A.x);
    double gx = cf1.image (A1.x);
    g.drawString ("x = " + sarrondi (A.x) + "          f (x) = " + sarrondi (fx) + "     Proba = " + sarrondi (cfi) + "          g (x) = " + sarrondi (gx) + "     Proba = " + sarrondi (cfi1), 10, getSize().height - 10);
    g1.drawImage (img, 0, 0, this);
  }

  public void mousePressed (MouseEvent e)
  { e.consume ();
    if (O.zone (e.getX (), e.getY ()) || A.zone (e.getX (), e.getY ())
        || I.zone (e.getX (), e.getY ()) || O1.zone (e.getX (), e.getY ())
        || O2.zone (e.getX (), e.getY ()) || A2.zone (e.getX (), e.getY ()))
    { modepaint = 0;
      X = -1;
    }
  }

  public void mouseDragged (MouseEvent e)
  { e.consume ();
    O.bouge (e.getX (), e.getY ());
    A.bouge (e.getX (), e.getY ());
    I.bouge (e.getX (), e.getY ());
    O1.bouge (e.getX (), e.getY ());
    O2.bouge (e.getX (), e.getY ());
    A2.bouge (e.getX (), e.getY ());
    repaint ();
  }

  public void mouseReleased (MouseEvent e)
  { e.consume ();
    O.deplace = A.deplace = I.deplace = O1.deplace = O2.deplace = A2.deplace = false;
    modepaint = 1;
  }

  public void mouseMoved (MouseEvent e)
  { e.consume ();
    if (modepaint == 1)
    { if (O.zone (e.getX (), e.getY ()) || A.zone (e.getX (), e.getY ())
          || I.zone (e.getX (), e.getY ()) || O1.zone (e.getX (), e.getY ())
          || O2.zone (e.getX (), e.getY ()) || A2.zone (e.getX (), e.getY ()))
      { this.X = e.getX () + 2; this.Y = e.getY () + 8;
        O.deplace = A.deplace = I.deplace = O1.deplace = O2.deplace = A2.deplace = false;
      }
      repaint ();
    }
  }

  public void mouseClicked (MouseEvent e) { }
  public void mouseEntered (MouseEvent e) { }
  public void mouseExited (MouseEvent e) { }

  private double lectparm (TextField tf, double d)
  { double dd = d;
  	try { dd = new Double(tf.getText()).doubleValue(); }
    catch (NumberFormatException nfe) { }
    if (dd >= 0.0) d = dd;
    tf.setText (new Double (d) . toString());
    return d;
  }


  public void actionPerformed (ActionEvent e)
  { if (e.getSource () == ok)
    { m = lectparm (tm, m);
      s = lectparm (ts, s);
      cf.parametres (m, s);
      m1 = lectparm (tm1, m1);
      s1 = lectparm (ts1, s1);
      cf1.parametres (m1, s1);
      modepaint = 2;
      repaint ();
    }
  }

  public String getAppletInfo ()
  { return "roc par j.-p. Quelen"; }

////////////////////////////////////////////////////////////////////////////////

    public static void main (String [] args) {

        roc a = new roc();
        a.init ();
        a.start ();

        Frame f = new Frame ("roc");
        f.addWindowListener (new fermer ());
        f.add (a);
        f.setSize (600, 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);
        }
    }
}
