01: import java.awt.*; 02: import java.awt.geom.*; 03: import java.util.*; 04: 05: /** 06: A car that can be moved around. 07: */ 08: public class CarShape implements MoveableShape 09: { 10: /** 11: Constructs a car item. 12: @param x the left of the bounding rectangle 13: @param y the top of the bounding rectangle 14: @param width the width of the bounding rectangle 15: */ 16: public CarShape(int x, int y, int width) 17: { 18: this.x = x; 19: this.y = y; 20: this.width = width; 21: } 22: 23: public void translate(double dx, double dy) 24: { 25: x += dx; 26: y += dy; 27: } 28: 29: public void draw(Graphics2D g2) 30: { 31: Rectangle2D.Double body 32: = new Rectangle2D.Double(x, y + width / 6, 33: width - 1, width / 6); 34: Ellipse2D.Double frontTire 35: = new Ellipse2D.Double(x + width / 6, y + width / 3, 36: width / 6, width / 6); 37: Ellipse2D.Double rearTire 38: = new Ellipse2D.Double(x + width * 2 / 3, y + width / 3, 39: width / 6, width / 6); 40: 41: // the bottom of the front windshield 42: Point2D.Double r1 43: = new Point2D.Double(x + width / 6, y + width / 6); 44: // the front of the roof 45: Point2D.Double r2 46: = new Point2D.Double(x + width / 3, y); 47: // the rear of the roof 48: Point2D.Double r3 49: = new Point2D.Double(x + width * 2 / 3, y); 50: // the bottom of the rear windshield 51: Point2D.Double r4 52: = new Point2D.Double(x + width * 5 / 6, y + width / 6); 53: Line2D.Double frontWindshield 54: = new Line2D.Double(r1, r2); 55: Line2D.Double roofTop 56: = new Line2D.Double(r2, r3); 57: Line2D.Double rearWindshield 58: = new Line2D.Double(r3, r4); 59: 60: g2.draw(body); 61: g2.draw(frontTire); 62: g2.draw(rearTire); 63: g2.draw(frontWindshield); 64: g2.draw(roofTop); 65: g2.draw(rearWindshield); 66: } 67: 68: private int x; 69: private int y; 70: private int width; 71: }