import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** * A car shape that can be positioned anywhere on the screen. */ public class Car { private double x; private double y; /** * Constructs a car with a given top left corner * * @param x * the x coordinate of the top left corner * @param y * the y coordinate of the top left corner */ public Car(double x, double y) { this.x = x; this.y = y; } /** * Draws the car. * * @param g2 * the graphics context */ public void draw(Graphics2D g2) { final double bodyWidth = 60.0; final double bodyHeight = 10.0; final double bodyX = 0.0; final double bodyY = 10.0; Rectangle2D.Double body = new Rectangle2D.Double(x + bodyX, y + bodyY, bodyWidth, bodyHeight); // Front tire final double frontTireX = 10.0; final double tireY = 20.0; final double tireDiameter = 10.0; Ellipse2D.Double frontTire = new Ellipse2D.Double(x + frontTireX, y + tireY, tireDiameter, tireDiameter); // Rear tire final double rearTireX = 40.0; Ellipse2D.Double rearTire = new Ellipse2D.Double(x + rearTireX, y + tireY, tireDiameter, tireDiameter); // The bottom of the front windshield final double frontWindshieldBottomX = 10.0; Point2D.Double frontWindshieldBottom = new Point2D.Double(x + frontWindshieldBottomX, y + bodyY); // The front of the roof final double frontWindshieldTopX = 20.0; Point2D.Double frontWindshieldTop = new Point2D.Double(x + frontWindshieldTopX, y); // The rear of the roof final double rearWindshieldTopX = 40.0; Point2D.Double rearWindshieldTop = new Point2D.Double(x + rearWindshieldTopX, y); // The bottom of the rear windshield final double rearWindshieldBottomX = 50.0; Point2D.Double rearWindshieldBottom = new Point2D.Double(x + rearWindshieldBottomX, y + bodyY); Line2D.Double frontWindshield = new Line2D.Double( frontWindshieldBottom, frontWindshieldTop); Line2D.Double roofTop = new Line2D.Double(frontWindshieldTop, rearWindshieldTop); Line2D.Double rearWindshield = new Line2D.Double(rearWindshieldTop, rearWindshieldBottom); g2.draw(body); g2.draw(frontTire); g2.draw(rearTire); g2.draw(frontWindshield); g2.draw(roofTop); g2.draw(rearWindshield); } }