/**
 * homot1c2.java - jpq - 12/12/98
 * double-buffering
 * 21/03/99 - jdk1.; modification le 05 et 09/09/22
 */

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import geo.*;

public class homot1c2 extends Panel implements MouseListener, MouseMotionListener, ActionListener {
	static final long serialVersionUID = 220909L;
	static Repere R;
	static Image img;
	static Graphics g1;
	int gw, gh;

// on definit ici les objets mathématiques
	static PointLibre O;
	static int n = 4;
	static PointLibre[] Ai = new PointLibre [n];
	static Pt[] Bi = new Pt [n];
	static Pt[] Ci = new Pt [n];
	static Pt O1;
	double k = 1.6, k1 = 2.0, xo = 0.0, yo = 0.0;
	static Polygone pA, pB, pC;
	TextField tfk;
	Button ta, ok;
	Random rnd;
	Label lbl;

//----------------------------------------------------------------------------
	public homot1c2() {
		addMouseMotionListener (this);
		addMouseListener (this);

		lbl = new Label ("rapport k");
		lbl.setFont (new Font ("Arial", Font.PLAIN, 12));
		add (lbl);
		tfk = new TextField ("1.6", 10);
		tfk.setFont (new Font ("Arial", Font.PLAIN, 12));
		add (tfk);
		ok = new Button ("Ok");
		ok.setFont (new Font ("Arial", Font.PLAIN, 12));
		ok.addActionListener (this);
		add (ok);
		ta = new Button ("nouvelle homothétie");
		ta.setFont (new Font ("Arial", Font.PLAIN, 10));
		ta.addActionListener (this);
		add (ta);
		rnd = new Random();
		setBackground (Color.WHITE);
	}

	private double nombre (String s, double d) {
		if (s != null) {
			try {
				d = Double.parseDouble(s);
			}
			catch (NumberFormatException e) {}
		}
		return d;
	}

	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, 12));

// on initialise ici les objets mathématiques
			if (R == null) {
				R = new Repere (gw / 2, gh / 2, gw, gh, 10.0, 10.0);
				O = new PointLibre (1.0, 1.0);
				Ai[0] = new PointLibre (-1.0, -3.0);
				Ai[1] = new PointLibre (3.0, -3.0);
				Ai[2] = new PointLibre (2.0, -1.0);
				Ai[3] = new PointLibre (3.0, 3.0);
				for (int i = 0; i < n; i++) {
					Bi[i] = new Pt();
					Ci[i] = new Pt();
				}
				pA = new Polygone();
				pB = new Polygone();
				pC = new Polygone();
				O1 = new Pt();
			} else {
				R.MAJ (gw / 2, gh / 2, gw, gh, 10.0, 10.0);
			}
		}

// on recalcule les objets liés
		O1.MAJ (xo, yo);
		for (int i = 0; i < n; i++ ) {
			Bi[i] = Ai[i].homothetie (O1, k1);
			Ci[i] = Ai[i].homothetie (O, k);
		}
		pA.MAJ (Ai);
		pB.MAJ (Bi);
		pC.MAJ (Ci);

// on trace
		g1.setColor (getBackground());
		g1.fillRect (0, 0, R.XMAX, R.YMAX);

		g1.setColor (Color.YELLOW);
		pA.trace (false, "", R, g1);

		g1.setColor (Color.CYAN);
		pB.trace (true, "", R, g1);

		g1.setColor (Color.MAGENTA);
		pC.trace (false, "", R, g1);

		g1.setColor (Color.BLACK);
		R.cadre (g1);
//		O1.trace ("", R, g1);			// pour tester uniquement

		g1.setColor (Color.RED);
		O.trace ("", R, g1);
		for (int i = 0; i < n; i ++ )
			Ai[i].trace ("", R, g1);
		boolean exact = (k == k1)
							&& R.X (O.x) < R.X (xo) + 2 && R.X (O.x) > R.X (xo) - 2
							&& R.Y (O.y) < R.Y (yo) + 2 && R.Y (O.y) > R.Y (yo) - 2;

		if (exact) {
			Font f = g1.getFont();
			g1.setFont (new Font (Font.SANS_SERIF, Font.BOLD, 16));
			g1.drawString ("EXACT", 10, R.YMAX - 20);
			g1.setFont (f);
		}

		g.drawImage (img, 0, 0, this);
	}

	public void actionPerformed (ActionEvent e) {
		if (e.getSource() == ok) {
			k = nombre (tfk.getText(), k);
			tfk.setText (Double.toString (k));
		} else if (e.getSource() == ta){
			do
				k1 = ((double)(rnd.nextInt() % 10)) / 10.0;
			while ((k1 == 1.0) || (k1 == 0.0));
			int i = rnd.nextInt() % (getSize().width / 4);
			xo = R.x (i + R.X (0.0));
			i = rnd.nextInt() % (getSize().height / 4);
			yo = R.y (i + R.Y (0.0));
		}
		repaint();
	}

	public void mousePressed (MouseEvent e) {
		int X = e.getX();
		int Y =  e.getY();
		if (O.deplace = O.zone (X, Y, R)) {
		} else {
			for (int i = 0; i < n; i++) {
				if (Ai[i].deplace = Ai[i].zone (X, Y, R)) {
					break;
				}
			}
		}
	}
			
	public void mouseDragged (MouseEvent e) {
		int X = e.getX();
		int Y =  e.getY();
		O.bouge (X, Y, R);
		for (int i = 0; i < n; i++)
			Ai[i].bouge (X, Y, R);
		repaint();
	}

	public void mouseReleased (MouseEvent e) {
		O.deplace = false;
		for (int i = 0; i < n; i++)
			Ai [i].deplace = false;
	}


	public void mouseMoved (MouseEvent e) {
		int X = e.getX();
		int Y = e.getY();
		int i = 0;
		if (!O.zone (X, Y, R)) {
			for (; i < n; i++) {
				if (Ai[i].zone (X, Y, R)) {
					break;
				}
			}
		}
		if (i < n) {
			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) {}

	public static void main (String[] args) {
		homot1c2 h1 = new homot1c2();
		JFrame f = new JFrame ("homot1c2");
		f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		f.add (h1);
		f.setSize (400, 400);
		f.setVisible (true);
	}

}
