

import java.awt.font.*;
import java.applet.Applet;
import java.util.Random;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.font.FontRenderContext;
import java.awt.*;
/**
 *  Description of the Class
 *
 *@author     jeff
 *@created    May 15, 2003
 */
public class JavaCar extends Applet {
	// Initializes Applet class.
	/**
	 *  Description of the Method
	 *
	 *@param  g  Description of the Parameter
	 */
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		Graphics2D g3 = (Graphics2D) g;

		// Sets background white, instead of default gray.
		Rectangle background = new Rectangle(0, 0, getWidth(), getHeight());
		g2.setColor(Color.white);
		g2.fill(background);

		// Car Body
		Rectangle body = new Rectangle(100, 110, 60, 10);
		Rectangle doorFrame = new Rectangle(129, 100, 2, 10);
		Rectangle blankOldCar = new Rectangle(96, 99, 62, 32);
		// Blanks out old image.

		// Tires
		Ellipse2D.Double rearTire = new Ellipse2D.Double(110, 120, 10, 10);
		Ellipse2D.Double frontTire = new Ellipse2D.Double(140, 120, 10, 10);
		Ellipse2D.Double rHubcap = new Ellipse2D.Double(112, 122, 6, 6);
		Ellipse2D.Double fHubcap = new Ellipse2D.Double(142, 122, 6, 6);

		// 4 Points connecting windshield and Roof
		Point2D.Double r1 = new Point2D.Double(110, 110);
		Point2D.Double r2 = new Point2D.Double(120, 100);
		Point2D.Double r3 = new Point2D.Double(140, 100);
		Point2D.Double r4 = new Point2D.Double(150, 110);

		/// 8 Points defining shape of UT logo
		Point2D.Double l1 = new Point2D.Double(122, 112);
		Point2D.Double l2 = new Point2D.Double(123, 113);
		Point2D.Double l3 = new Point2D.Double(127, 113);
		Point2D.Double l4 = new Point2D.Double(129, 115);
		Point2D.Double l5 = new Point2D.Double(131, 115);
		Point2D.Double l6 = new Point2D.Double(133, 113);
		Point2D.Double l7 = new Point2D.Double(137, 113);
		Point2D.Double l8 = new Point2D.Double(139, 111);

		// 2 Points for tailpipe, and line for tailpipe
		Point2D.Double t1 = new Point2D.Double(100, 120);
		Point2D.Double t2 = new Point2D.Double(95, 123);
		Line2D.Double tailPipe = new Line2D.Double(t1, t2);

		// UT Logo
		Line2D.Double horn1 = new Line2D.Double(l1, l2);
		Line2D.Double horn2 = new Line2D.Double(l2, l3);
		Line2D.Double horn3 = new Line2D.Double(l3, l4);
		Line2D.Double horn4 = new Line2D.Double(l5, l6);
		Line2D.Double horn5 = new Line2D.Double(l6, l7);
		Line2D.Double horn6 = new Line2D.Double(l7, l8);
		Ellipse2D.Double logoHead = new Ellipse2D.Double(128, 114, 4, 6);

		// Smoke
		Ellipse2D.Double smoke = new Ellipse2D.Double(89, 117, 7, 9);

		//Lines for Windshield and Roof
		Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
		Line2D.Double roofTop = new Line2D.Double(r2, r3);
		Line2D.Double rearWindshield = new Line2D.Double(r3, r4);

		// Define Burnt Orange Color
		Color burntOrange = new Color(.9F, 0.28F, 0.0F);

		// Define font used for label at the bottom.
		Font myFont = new Font("SansSerif", Font.BOLD, 16);
		g3.setFont(myFont);
		String message = "UT JavaMobile 1.0";

		// Label
		FontRenderContext context = g3.getFontRenderContext();
		TextLayout layout = new TextLayout(message, myFont, context);
		float messageLength = layout.getAdvance();
		g3.setColor(Color.black);
		g3.drawString(message, (0.5F * (getWidth() - messageLength)), 150);
		int counter = 0;
		int lightCounter = 0;

		double j = 100;
		double acceleration = 1.0;
		double shadesOfGray = 0.7;

		//First traffic signal.
		Point2D.Double f1 = new Point2D.Double(185, 0);
		Point2D.Double f2 = new Point2D.Double(185, 60);
		Line2D.Double lightPole1 = new Line2D.Double(f1, f2);
		Rectangle light1 = new Rectangle(180, 60, 10, 24);
		Ellipse2D.Double rLight1 = new Ellipse2D.Double((181), 61, 6, 6);
		Ellipse2D.Double yLight1 = new Ellipse2D.Double((181), 69, 6, 6);
		Ellipse2D.Double gLight1 = new Ellipse2D.Double((181), 77, 6, 6);
		g2.setColor(Color.black);
		g2.draw(lightPole1);
		g2.draw(light1);
		g2.draw(rLight1);
		g2.draw(yLight1);
		g2.draw(gLight1);
		g2.setColor(Color.red);
		g2.fill(rLight1);
		// Initially, the first light is red.


		//Second Traffic Signal
		Point2D.Double s1 = new Point2D.Double(750, 0);
		Point2D.Double s2 = new Point2D.Double(750, 60);
		Line2D.Double lightPole = new Line2D.Double(s1, s2);
		Rectangle light = new Rectangle(745, 60, 10, 24);
		Ellipse2D.Double rLight = new Ellipse2D.Double((746 - lightCounter), 61, 6, 6);
		Ellipse2D.Double yLight = new Ellipse2D.Double((746 - lightCounter), 69, 6, 6);
		Ellipse2D.Double gLight = new Ellipse2D.Double((746 - lightCounter), 77, 6, 6);
		g2.setColor(Color.black);
		g2.draw(lightPole);
		g2.draw(light);
		g2.draw(rLight);
		g2.draw(yLight);
		g2.draw(gLight);
		g2.setColor(Color.green);
		g2.fill(gLight);
		// Sets color of second signal to green.

		// Initially draw all parts of the car waiting at first traffic signal.
		g2.setColor(burntOrange);
		g2.fill(body);
		g2.fill(doorFrame);
		g2.setColor(Color.black);
		g2.draw(body);
		g2.draw(doorFrame);
		g2.fill(frontTire);
		g2.fill(rearTire);
		g2.draw(frontWindshield);
		g2.draw(roofTop);
		g2.draw(rearWindshield);
		g2.draw(tailPipe);
		g2.setColor(Color.white);
		g2.fill(fHubcap);
		g2.fill(rHubcap);
		g2.draw(horn1);
		g2.draw(horn2);
		g2.draw(horn3);
		g2.draw(horn4);
		g2.draw(horn5);
		g2.draw(horn6);
		g2.fill(logoHead);
		// Draw the UT longhorn logo.
		long wait = System.currentTimeMillis();
		while ((wait + 1900) > System.currentTimeMillis()) {
		}
		g2.setColor(Color.white);
		g2.fill(rLight1);
		g2.setColor(Color.green);
		g2.fill(gLight1);

		while (j < (getWidth() - 100)) {
			// Loop for providing motion via the TRANSLATE method (below).

			// Draw all parts of the car.
			g2.setColor(burntOrange);
			g2.fill(body);
			g2.fill(doorFrame);
			g2.setColor(Color.black);
			g2.draw(body);
			g2.draw(doorFrame);
			g2.fill(frontTire);
			g2.fill(rearTire);
			g2.draw(frontWindshield);
			g2.draw(roofTop);
			g2.draw(rearWindshield);
			g2.draw(tailPipe);
			g2.setColor(Color.white);
			g2.fill(fHubcap);
			g2.fill(rHubcap);
			g2.draw(horn1);
			g2.draw(horn2);
			g2.draw(horn3);
			g2.draw(horn4);
			g2.draw(horn5);
			g2.draw(horn6);
			g2.fill(logoHead);
			// Draw the UT longhorn logo.
			Color myGray = new Color((float) shadesOfGray, (float) shadesOfGray, (float) shadesOfGray);
			// Defines a variable gray color.
			g2.setColor(myGray);
			// Allows changing Smoke Color.
			g2.fill(smoke);
			// Draws little puffs of exhaust.
			shadesOfGray -= 0.004;
			// Increments the color og gray used for exhaust.
			long i = System.currentTimeMillis();
			// Gets current system time.
			while ((i + 90) > System.currentTimeMillis()) {
			}
			// Waits for 90 milliseconds
			g2.setColor(Color.white);
			g2.fill(blankOldCar);
			// Erase old car image.

			if (counter < 35) {
				acceleration = acceleration * 1.1;
			}
			// Calculates acceleration of car based on position.
			else {
				acceleration = acceleration * 0.93;
			}
			// Begins deceleration after a certain point.

			j = j + acceleration;
			counter += 1;
			// These variables keep track of the car's position.
			if (counter == 31) {
				g2.setColor(Color.white);
				// Change traffic signal to yellow.
				Ellipse2D.Double gLight2 = new Ellipse2D.Double((746 - lightCounter), 77, 6, 6);
				g2.fill(gLight2);
				g2.setColor(Color.yellow);
				Ellipse2D.Double yLight2 = new Ellipse2D.Double((746 - lightCounter), 69, 6, 6);
				g2.fill(yLight2);
			}
			if (counter == 48) {
				g2.setColor(Color.white);
				// Change traffic signal to red.
				Ellipse2D.Double yLight2 = new Ellipse2D.Double((746 - lightCounter), 69, 6, 6);
				g2.fill(yLight2);
				g2.setColor(Color.red);
				Ellipse2D.Double rLight2 = new Ellipse2D.Double((746 - lightCounter), 61, 6, 6);
				g2.fill(rLight2);
			}
			g2.translate(((int) acceleration), 0);
			// Provides the illusion of acceleration or deceleration.
			lightCounter += acceleration;
			// Helps keep track of position so the traffic signal can be changed properly.
		}

		// Comment expressing frustration with Austin traffic.
		Font comment = new Font("SansSerif", Font.PLAIN, 9);
		g3.setFont(comment);
		String whyMe = "Why me?";
		FontRenderContext context2 = g3.getFontRenderContext();
		TextLayout layout2 = new TextLayout(whyMe, comment, context2);
		messageLength = layout2.getAdvance();
		g3.setColor(Color.black);
		g3.drawString(whyMe, 100, 80);

		// Draw the car one last time.
		g2.setColor(burntOrange);
		g2.fill(body);
		g2.fill(doorFrame);
		g2.setColor(Color.black);
		g2.draw(body);
		g2.draw(doorFrame);
		g2.fill(frontTire);
		g2.fill(rearTire);
		g2.draw(frontWindshield);
		g2.draw(roofTop);
		g2.draw(rearWindshield);
		g2.draw(tailPipe);
		g2.setColor(Color.white);
		g2.fill(fHubcap);
		g2.fill(rHubcap);
		g2.draw(horn1);
		g2.draw(horn2);
		g2.draw(horn3);
		g2.draw(horn4);
		g2.draw(horn5);
		g2.draw(horn6);
		g2.fill(logoHead);
	}

}






