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

package animations;

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

public class StarsAndStripesCanvas extends ACanvas {

	private int pw; // panelWidth

	public StarsAndStripesCanvas(Color c, int width, int height) {
		super(c, width, height);
		pw = width;
	}

	public void draw() {
		setAnimationTime(700);

		double w = 3 * pw / 4, h = 7 * w / 13; // width/height of flag
		double sH = h / 13;	// stripe height
		double y = pw / 2 - 5 * sH; // y of top white stripe of flag

		ABox red = new ABox(Color.red, w, h);
		add(red);
		red.draw();

		for (int i = 0; i < 6; i++) {
			ABox stripe = new ABox(Color.white, pw / 2, y + (i * 2 * sH), w, sH);
			add(stripe);
			stripe.draw();
		}

		double uw = w * 4 / 10, uh = sH * 7; // union width/height
		ABox b = new ABox(new Color(0, 0, 128), pw / 2 - w / 2 + uw / 2, pw / 2 - 3 * sH, uw, uh);
		add(b);
		b.draw();

		double gH = uh / 10, gW = uw / 12; // vertical/horizontal gaps between stars
		double x1 = pw / 2 - w / 2 + gW, y1 = y - sH + gH / 4; // coords of first star

		// speed things up for the stars ...
		setAnimationTime(200);

		Color starColor = Color.white;
		for (int r = 0; r < 5; r++) {
			for (int c = 0; c < 6; c++) {
				AStar star = new AStar(starColor);
				double starWidth = (double) star.getWidth();
				double scaleFactor = gW / starWidth;
				star.scale(scaleFactor);
				star.moveTo(x1 + c * 2 * gW, y1 + r * 2 * gH);
				add(star);
				star.draw();
			}
		}

		x1 += gW;
		y1 += gH;
		for (int r = 0; r < 4; r++) {
			for (int c = 0; c < 5; c++) {
				AStar star = new AStar(starColor);
				double starWidth = (double) star.getWidth();
				double scaleFactor = gW / starWidth;
				star.scale(scaleFactor);
				star.moveTo(x1 + c * 2 * gW, y1 + r * 2 * gH);
				add(star);
				star.draw();
			}
		}

		setAnimationTime(2000);

		ABox outline = new ABox(Color.black, pw / 2, pw / 2, w, h);
		add(outline);
		outline.setFill(false);
		outline.setLineWidth(1);
		outline.draw();
	}

	public static void main(String[] args) {
		StarsAndStripesCanvas canvas = new StarsAndStripesCanvas(Color.white, 300, 300);
		JFrame frame = new JFrame("StarsAndStripes");
		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();
	}
}