// (c) Academic Java. All rights reserved.
// See http://academicjava.com/animations/conditions.html for conditions of use

package animations;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import org.academicjava.animation.*;

public class ChryslerCanvas extends ACanvas {

	private int size;

	static private Point2D rotatePoint(int x1, int y1, int x2, int y2, double degrees) {
		// rotate point (x1, y1) about point (x2, y2) by the specified degrees
		AffineTransform at = new AffineTransform();
		at.rotate(Math.toRadians(degrees), x2, y2);
		return at.transform(new Point2D.Double(x1, y1), null);
	}

	static private APolygon createRegularPolygon(Paint paint, int x, int y, int n, int radius) {
		double angle = 360.0 / (double) n;
		int[] xOffs = new int[n];
		int[] yOffs = new int[n];
		for (int i = 0; i < n; i++) {
			Point2D p = rotatePoint(0, -radius, 0, 0, i * angle);
			xOffs[i] = (int) p.getX();
			yOffs[i] = (int) p.getY();
		}
		return new APolygon(paint, x, y, xOffs, yOffs);
	}

	public ChryslerCanvas(Color c, int width, int height) {
		super(c, width, height);
		size = width;
	}

	public void draw(final int canvasAnimationTime) {
		Thread drawer = new Thread() {

			@Override
			public void run() {
				// 4 animops...
				setAnimationTime(canvasAnimationTime/4);
				int n = 5; // number of sides for regular polygon
				int r = 50; // radius of regular polygon
				// the chrysler badge has a pentagon (a 5-sided regular polygon) enclosing a 5-pointed star
				APolygon p = createRegularPolygon(Color.white, size / 2, size / 2, n, r);
				p.setFill(false);
				p.setLineWidth(3);
				add(p);
				p.draw();

				AStar s = new AStar(Color.white, n, 4, r);
				add(s);
				s.draw();

				setConcurrentAnimation(true);
				p.scale(); // 1 op
				s.scale(); // same op
				p.rotate(360); // new op (p above done first cos same apanel)
				s.rotate(360); // same op
			}
		};
		drawer.start();
	}

	public static void main(String[] args) {

		ChryslerCanvas canvas = new ChryslerCanvas(Color.black, 250, 250);
		JFrame frame = new JFrame("Chrysler");
		Container cp = frame.getContentPane();
		cp.add(canvas, BorderLayout.CENTER);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		canvas.draw(4000);
	}
}