/**
 * Flyby - things fly by
 * 
 * Nina Amenta
 * September 2001
 * Don Fussell March 2007
 */

import java.awt.*;
import java.awt.geom.*;

public class FlybySolution extends AnimationFrame {
	Rectangle2D r, screen;
	Shape xr;
	AffineTransform rotForm = new AffineTransform();
	AffineTransform rXform = new AffineTransform();
	AffineTransform transForm = new AffineTransform();
	Color deepBlue = new Color(0,50,100);

	// Constructor
	public FlybySolution() {   
		super("FlybySolution");

		r = new Rectangle2D.Double(0,0,50,30);
		
		// get a rectangle bounding the screen
		screen = new Rectangle2D.Double(0,0,getWidth(),getHeight());

		// initialize the transforms
		rotForm.setToIdentity();
		transForm.setToTranslation(225,185);
		rXform.setToTranslation(225,185);
		// apply the transform to the rectangle
		xr = rXform.createTransformedShape(r); 

		setVisible(true); 
	}

	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		// Anti-aliasing 
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g2.setColor(deepBlue);
		g2.fill(xr);  // draw the transformed rectangle

	}

	// run may times a second, to move objects around.
	public void timeStep() {

		// update the rotation 
		rotForm.rotate(Math.PI/10,25,15);
		// update the translation
		transForm.translate(10, 0);
		// fix the translation if it goes off the screen
		if (!xr.intersects(screen)) transForm.setToTranslation(0,185);
		// rebuild the transformation matrix
		// first get rid of whatever was already there
		rXform.setToIdentity();
		// put in the translation
		rXform.concatenate(transForm);
		// put in the rotation, note this will be on the right, and performed first
		rXform.concatenate(rotForm);
		xr = rXform.createTransformedShape(r);
	}

	// main method, starts up
	public static void main(String[] args) {
		FlybySolution fly = new FlybySolution();

		// Start the animation in a new thread
		Thread t = new Thread(fly);  // creates the Thread from this object
		t.start();  // calls the run method
	}
}