/**
 * pliage.java
 * date : 11 et 22/09/22 (reprise d'un programme écrit le 28/09/99)
 * auteur : Jean-Paul QUELEN
 */

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import geo.*;

public class pliage extends JFrame implements ActionListener, Runnable {
	static final long serialVersionUID = 220922L;
	static final int netapemax = 10, nbclmax = 20;
	static final long tempo = 100L;
	static Image img;
	static Graphics g1;
	int gw, gh;

	int netape = 0;
	int nbcl = -1;
	JButton suivant;

// objets géométriques
	static Repere R;
	static Pt A;
	static Pt B;
	static Pt C;
	static Pt D;
	static Segment[] AB = new Segment [netapemax];
	static Segment[] AC = new Segment [netapemax];
	static Segment[] AD = new Segment [netapemax];
	static Segment[] BD = new Segment [netapemax];
	static Segment[] CD = new Segment [netapemax];

	static Thread monpliage;
	Feuille dessin = new Feuille();

	public pliage (String titre) {
		super (titre);
		setLayout (new BorderLayout());
		JPanel jp = new JPanel();
		add (jp, BorderLayout.NORTH);
		jp.add (suivant = new JButton ("suivant"));
		suivant.addActionListener (this);
		add (dessin, BorderLayout.CENTER);
		monpliage = new Thread (this);
		monpliage.start();
	}
	 
protected class Feuille extends Canvas {
	static final long serialVersionUID = 220911L;

	public Feuille() {
		setBackground (Color.WHITE);
	}

	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));

// Au démarrage ou changement de la taille de la fenêtre => nouveau repère et points A, B, C
			double echelle = 0.2 * gh;
			R = new Repere (40, gh - 40, gw, gh, echelle, echelle);
			A = new Pt (0.0, 0.0);
			B = new Pt (6.0, 0.0);
			C = new Pt (3.0, 3.0);
			netape = 0;
			nbcl = -1;
		}

// on recalcule si image fixe ou au début de l'animation
		if (nbcl < 0) {
			AC[netape] = new Segment (A, C);
			Droite dBC = new Droite (B, C);
			Droite dAD = Droite.bissectrice (B, A, C);
			D = Pt.intersection (dBC, dAD);
			AD[netape] = new Segment (A, D);
			CD[netape] = new Segment (C, D);
		}
		AB[netape] = new Segment (A, B);
		BD[netape] = new Segment (B, D);

// on efface
		g1.setColor (getBackground());
		g1.fillRect (0, 0, gw, gh);

// et on trace
		g1.setColor (Color.BLACK);
		R.cadre (g1);

		g1.setColor (Color.BLUE);
		for (int i = 0; i <= netape; i++) {
			AC[i].trace ("", R, g1);
			CD[i].trace ("", R, g1);
		}
		AB[netape].trace ("", R, g1);
		BD[netape].trace ("", R, g1);

		g1.setColor (Color.GREEN);
		for (int i = 0; i < netape; i++) {
			AD[i].trace ("", R, g1);
		}
		if (nbcl > 0) {
			AD[netape].trace ("", R, g1);
		}

/*		g1.setColor (Color.RED);
		A.traceNom ("A", R, g1);
		B.traceNom ("B", R, g1);
		C.traceNom ("C", R, g1);
		D.traceNom ("D", R, g1); */

		g.drawImage (img, 0, 0, this);
	}

} // fin protected class Feuille

	public void run () {
		Thread t = Thread.currentThread();
		Pt E = new Pt();
		while (t == monpliage) {
			if (nbcl >= 0 && nbcl < nbclmax) {
				try {
					Thread.sleep (tempo);
				}
				catch (InterruptedException ie) {}
				if (nbcl == 0) {
					double k = A.distance (B) / A.distance (C);
					E = C.homothetie (A, k);
				}
// repliage => B se déplace de sa position initiale jusqu'à son position sur E, point de (AC) tel que AE = AB
				B.MAJ (B.x + (double)nbcl / nbclmax * (E.x - B.x), B.y + (double)nbcl / nbclmax * (E.y - B.y));
				nbcl++;
				dessin.repaint();
			}
			if (nbcl == nbclmax) {
				nbcl = -1;
				if (netape < netapemax - 1) {
					netape++;
					A.MAJ (D);
					dessin.repaint();
				}
			}
			try {
				Thread.sleep (0L);
			}
			catch (InterruptedException ie) {}
		}
	}

	public void actionPerformed (ActionEvent e) {
		if (e.getSource () == suivant) {
			if (netape < netapemax && nbcl == -1) {
				nbcl = 0;
			}
		}
	}

	public static void main (String[] args) {
		pliage p = new pliage("pliage");
		p.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);		// EXIT_ON_CLOSE pour la fermeture du Thread
		p.setSize (500, 400);
		p.setVisible (true);
	}

}
