/**
 * axbyc.java - jpq - 05/05/99 - jdk1.2 - double-buffering
 * modification et ajout de main() le 25/12/17 ; modifications le 04, 12 et le 22/09/22
 */

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import geo.*;

public class axbyc extends JFrame {
	static final long serialVersionUID = 220922L;
	static final String tirets = "----------------------------------------";
	static Repere R;
	static Image img;
	static Graphics g1;
	int gw, gh;
	Feuille dessin = new Feuille();

// on definit ici les objets mathématiques
	static PointLibre A, B;
	static Pt L;
	static Segment AL, BL;
	static Droite AB, dj;
	JLabel lb;
	int a, b, c;
	String txtlb;
	int pas = 40;

	public axbyc (String titre) {
		super (titre);
		setLayout (new BorderLayout());
		JPanel jp = new JPanel();
		jp.setBackground (Color.BLACK);
		add (jp, BorderLayout.NORTH);
		jp.add (lb = new JLabel (tirets));
		lb.setFont (new Font ("Arial", Font.PLAIN, 14));
		lb.setBackground (Color.BLACK);
		lb.setForeground (Color.WHITE);
		setBackground (Color.WHITE);
		add (dessin, BorderLayout.CENTER);
	}

	private void affiche (int n) {
		if (n > 0) {
			if (txtlb.length() != 0) txtlb += " + ";
			if (n != 1)
				txtlb += Integer.toString (n);
		} else if (n < 0) {
			txtlb += " - ";
			if (n != -1)
				txtlb += Integer.toString (-n);
		}
	}

protected class Feuille extends Canvas implements MouseListener, MouseMotionListener {
	static final long serialVersionUID = 220922L;

	public Feuille() {
		setBackground (Color.WHITE);
		addMouseMotionListener (this);
		addMouseListener (this);
	}

	public void update (Graphics g) {
		paint (g);
	}

	public void paint (Graphics g) {
		if (img == null || gw != getSize().width || gh != getSize().height) {
			gw = getSize().width;
			gh = getSize().height;
			img = createImage (gw, gh);
			g1 = img.getGraphics();
			g1.setFont (new Font ("Arial", Font.PLAIN, 10));

// on initialise ici les objets mathématiques
			if (R == null) {
				R = new Repere (gw / 2, gh / 2, gw, gh, pas, pas);
				A = new PointLibre (1.0, 3.0);
				B = new PointLibre (-1.0, -1.0);
				AB = new Droite();
				L = new Pt();
				AL = new Segment();
				BL = new Segment(); 
			} else {
				R.MAJ (gw / 2, gh / 2, gw, gh, pas, pas);
			}
		}

// on recalcule les objets liés
		A.MAJ (0.5 * Math.round (2.0 * A.x), 0.5 * Math.round (2.0 * A.y));
		B.MAJ (0.5 * Math.round (2.0 * B.x), 0.5 * Math.round (2.0 * B.y));
		AB.MAJ (A, B);
		if (A.x > B.x)
			L.MAJ (A.x, B.y);
		else
			L.MAJ (B.x, A.y);
		AL.MAJ (A, L);
		BL.MAJ (B, L);

// on recalcule les coefficients a, b, c de l'équation de la droite
		a = (int) ((B.y - A.y) * 4.0);
		b = (int) ((A.x - B.x) * 4.0);
		c = (int) ((B.x * A.y - A.x * B.y) * 4.0);
		if (a < 0) {
			a = - a;
			b = - b;
			c = - c;
		} else if ((a == 0) && (b < 0)) {
			b = - b;
			c = - c;
		}
		boolean sgnb, sgnc;
		if (sgnb = (b < 0))
			b = - b;
		if (sgnc = (c < 0))
			c = - c;
		int n = (a >= b) ? a : b;
		int d = (a >= b) ? b : a;
		while (d > 0)
			d = n % (n = d); /* n facteur commun */
		d = (n >= c) ? c : n;
		n = (n >= c) ? n : c;
		while (d > 0)
			d = n % (n = d);
// pb si n est nul, ie A = B
		a = a / n;
		b = b / n;
		c = c / n;
		if (sgnb)
			b = - b;
		if (sgnc)
			c = - c;
 
// on trace
		g1.setColor (getBackground());
		g1.fillRect (0, 0, R.XMAX, R.YMAX);

		if ((a == 0) && (b == 0)) {
			txtlb = tirets;
		} else {
			txtlb = "";
			if (a != 0) {
				affiche (a);
				txtlb += " x";
			}
			if (b != 0) {
				affiche (b);
				txtlb += " y";
			}
			if (c != 0) {
				affiche (c);
				if (Math.abs (c) == 1)
					txtlb += " 1";
			}
			txtlb += " = 0";
		}
		lb.setText (txtlb);

		g1.setColor (Color.YELLOW);
		R.trace_grille (0.5, 0.5, g1);

		g1.setColor (Color.BLACK);
		R.cadre (g1);

		g1.setColor (Color.RED);
		R.trace (g1);
		R.trace_repere_gradue (1.0, 1.0, g1);

		g1.setColor (Color.GREEN);
		AL.trace ("", R, g1);
		BL.trace ("", R, g1);
		L.traceNom ("L", R, g1);

		g1.setColor (Color.BLUE);
		AB.trace ("", R, g1);

		g1.setColor (Color.RED);
		A.trace ("A", R, g1);
		B.trace ("B", R, g1);

		g.drawImage (img, 0, 0, this);
	}

	public void mousePressed (MouseEvent e) {
		int X = e.getX();
		int Y =  e.getY();
		if (A.deplace = A.zone (X, Y, R)) {
		} else if (B.deplace = B.zone (X, Y, R)) {
		}
	}
 
	public void mouseDragged (MouseEvent e) {
		int X = e.getX();
		int Y = e.getY();
		A.bouge (X, Y, R);
		B.bouge (X, Y, R);
		repaint();
	}

	public void mouseReleased (MouseEvent e) {
		A.deplace = B.deplace = false;
	}

	public void mouseMoved (MouseEvent e) {
		int X = e.getX();
		int Y = e.getY();
		if (A != null && A.zone (X, Y, R) || B != null && B.zone (X, Y, R)) {
			setCursor (new Cursor (Cursor.HAND_CURSOR));
		} else {
			setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
		}
	}

	public void mouseClicked (MouseEvent e){}
	public void mouseEntered (MouseEvent e) {}
	public void mouseExited (MouseEvent e) {}

}	// fin de protected class Feuille

	public static void main (String[] args) {
		axbyc a = new axbyc ("axbyc");
		a.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		a.setSize (600, 600);
		a.setVisible (true);
	}

}
