/**
 * loinormale.java
 *
 * Auteur : Jean-Paul Quelen
 * 22/03/2020
 *
 */

import java.awt.event.*;
import java.awt.*;
import geo.*;
import javax.swing.*;

public class loinormale extends Panel implements MouseListener, MouseMotionListener, ActionListener {
	static final long serialVersionUID = 200322L;
    Image img;
    Graphics gimg;
    int w, h;
    int X, Y, Xp, Yp;
    Repere R;
    Droite xpx;
    PointSurDroite A, B;
    pointlibre O, I;
    Segment S;
    gauss cf;
    int modepaint = -1;
    Button ok;
    TextField ta, tb, tarr;
	double a, b, darrondi;
	Panel pprobas;
	Label probas;
	TextField p1, p2, p3;
	dessin d;
    
////////////////////////////////////////////////////////////////////////////////

	public loinormale() {
		setBackground (Color.WHITE);
		setLayout (new BorderLayout ());
		Panel p = new Panel();
		add (p, BorderLayout.NORTH);
		add (pprobas = new Panel(), BorderLayout.SOUTH);
		pprobas.setBackground (Color.LIGHT_GRAY);
		pprobas.add (probas = new Label ("probas :"));
		probas.setForeground (Color.BLUE);
		pprobas.add (p1 = new TextField ("", 8));
		pprobas.add (p2 = new TextField ("", 8));
		pprobas.add (p3 = new TextField ("", 8));
		probas.setForeground (Color.BLUE);
		p1.setBackground (Color.YELLOW);
		p2.setBackground (Color.GREEN);
		p3.setBackground (Color.YELLOW);
		p.setBackground (Color.LIGHT_GRAY);
		modepaint = 1;
		Font f = new Font ("Arial", Font.PLAIN, 10);
		p.add (new Label ("a ="));
		p.add (ta = new TextField ("0.5", 4));
		a = 0.5;
		p.add (new Label ("  b ="));
		p.add (tb = new TextField ("1.0", 4));
		b = 1.0;
		p.add (new Label ("                    déc:"));
		p.add (tarr = new TextField ("2", 1));
		darrondi = 100.0;
        p.add (ok = new Button ("Ok"));
        ok.addActionListener (this);
		add (d = new dessin(), BorderLayout.CENTER);
		d.addMouseMotionListener (this);
		d.addMouseListener (this);
    }

    public void update (Graphics g) {
		paint (g);
	}

	public void paint (Graphics g) {
		d.repaint();
	}

    private String sarrondi (double d) {
        return Double.toString (Math.floor (d * darrondi) / darrondi);
    }

	private void traceseg (Color couleur, int Xdeb, int Xfin, int RIord0) {
        gimg.setColor (couleur);
        for (int X = Xdeb; X < Xfin; X ++) {
            S.AX = S.BX = X;
			S.AY = RIord0;
			S.BY = R.Iord (cf.image (R.Abs (X)));
			S.defini = true;
            S.trace (gimg);
        }
	}

    public void mousePressed (MouseEvent e) {
        if (O.zone (e.getX (), e.getY ()) || A.zone (e.getX (), e.getY ())
                || I.zone (e.getX (), e.getY ()) || B.zone (e.getX (), e.getY ())) {
			modepaint = 0;
            X = -1;
        }
    }

    public void mouseDragged (MouseEvent e) {
		O.bouge (e.getX (), e.getY ());
        A.bouge (e.getX (), e.getY ());
        B.bouge (e.getX (), e.getY ());
        I.bouge (e.getX (), e.getY ());
		a = A.x;
		b = B.x;
        repaint ();
    }

    public void mouseReleased (MouseEvent e) {
        O.deplace = A.deplace = B.deplace = I.deplace = false;
		modepaint = 1;
    }

    public void mouseMoved (MouseEvent e) {
        if (modepaint == 1) {
			if (O.zone (e.getX (), e.getY ()) || A.zone (e.getX (), e.getY ())
                    || I.zone (e.getX (), e.getY ()) || B.zone (e.getX (), e.getY ())) {
				this.X = e.getX () + 2;
				this.Y = e.getY () + 8;
                O.deplace = A.deplace = B.deplace = I.deplace = false;
            }
            repaint ();
        }
    }

    public void mouseClicked (MouseEvent e) {}
    public void mouseEntered (MouseEvent e) {}
    public void mouseExited (MouseEvent e) {}

    public void actionPerformed (ActionEvent e) {
		if (e.getSource () == ok) {
			try {
				A.x = a = Double.parseDouble (ta.getText());
			}
            catch (NumberFormatException nfe) {}
            try {
				B.x = b = Double.parseDouble (tb.getText());
			}
            catch (NumberFormatException nfe) {}
			int iarr = 2;
            try {
				iarr = Math.max (1, Integer.parseInt (tarr.getText ()));
			}
            catch (NumberFormatException nfe) {}
			ta.setText (Double.toString (A.x));
			tb.setText (Double.toString (B.x));
            tarr.setText (Integer.toString (iarr));
			darrondi = Math.pow (10.0, iarr);
            modepaint = 0;
            repaint ();
        }
    }

////////////////////////////////////////////////////////////////////////////////

protected class dessin extends Canvas {
	static final long serialVersionUID = 200322L;

    public void paint (Graphics g) {
		if (img == null || w != getSize().width || h != getSize().height) {
	        w = getSize().width;
			h = getSize().height;
	        img = createImage (w, h);
            gimg = img.getGraphics();
			modepaint = 0;
			if (R == null) {
				R = new Repere (w / 2, h * 2 / 3, w, h, 50.0, 100.0);
				O = new pointlibre (0.0, 0.0, "O", R);
				xpx = new Droite (0.0, 1.0, 0.0, "", R);
				A = new PointSurDroite (a, 0.0, xpx, "a", R);
				B = new PointSurDroite (b, 0.0, xpx, "b", R);
				I = new pointlibre (1.0, 1.0, "I", R);
				cf = new gauss ("", R);
				S = new Segment ("", R);
			}
			else
				R.Repere (w / 2, h * 2 / 3, w, h, 50.0, 100.0);
		}
        gimg.setColor (getBackground ());
        gimg.fillRect (0, 0, R.XMAX, R.YMAX);
        if (modepaint == 0) {
			if ((O.deplace) || (I.deplace)) {
				if (O.deplace)
					R.Repere (R.Iabs (O.x), R.Iord (O.y), w, h, R.unitex, R.unitey);
                else {
					double Ix = I.x;
					double Iy = I.y;
                    if ((Ix > 0.0) && (Iy > 0.0))
						R.Repere (R.X0, R.Y0, w, h, (double) (R.Iabs (Ix) - R.X0), (double) (R.Y0 - R.Iord (Iy)));
                }
            }
            O.point (0.0, 0.0);
            I.point (1.0, 1.0);
            A.point();
            B.point();
			ta.setText (sarrondi (A.x));
			tb.setText (sarrondi (B.x));
        }
        gimg.setColor (Color.black);
        if (modepaint == 1)
            if (X > 0) {
				gimg.drawString ("point mobile", X, Y);
                Xp = X;
				Yp = Y;
				X = -1;
            }
        int Xdeb = Math.max ((A.X <= B.X) ? A.X : B.X, 0);
        int Xfin = Math.min ((A.X <= B.X) ? B.X : A.X, w);
		int RIord0 = R.Iord (0);
		traceseg (Color.YELLOW, 0, Xdeb, RIord0);
		traceseg (Color.GREEN, Xdeb, Xfin, RIord0);
		traceseg (Color.YELLOW, Xfin, w, RIord0);
        gimg.setColor (Color.red);
        R.trace (gimg);
        O.trace (gimg);
        I.trace (gimg);
        A.trace (gimg);
        B.trace (gimg);
        gimg.setColor (Color.BLUE);
        cf.trace (gimg);
        g.drawImage (img, 0, 0, this);
        double x1 = (A.x <= B.x) ? A.x : B.x;
        x1 = cf.integre (x1);
        double x3 = (A.x <= B.x) ? B.x : A.x;
        x3 = cf.integre (- x3);
		p1.setText (sarrondi (x1));
		p2.setText (sarrondi (1.0 - x1 - x3));
		p3.setText (sarrondi (x3));
    }

}

////////////////////////////////////////////////////////////////////////////////

	public static void main (String [] args) {
		int w = 400;
		int h = 300;
		try {
			if (args.length >= 1)
				w = h = Integer.parseInt (args [0]);
			if (args.length >= 2)
				h = Integer.parseInt (args [1]);
		}
		catch (NumberFormatException e) {}
		loinormale a = new loinormale();

		JFrame f = new JFrame ("Loi normale");
		f.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
		f.add (a);
		f.setSize (w, h);
		f.setVisible (true);
    }

}
