001package horstmann.ch06_scene3; 002import java.awt.geom.Ellipse2D; 003import java.awt.geom.Line2D; 004import java.awt.geom.Point2D; 005import java.awt.geom.Rectangle2D; 006 007/** 008 A car shape. 009 */ 010public class CarShape extends CompoundShape 011{ 012 /** 013 Constructs a car shape. 014 @param x the left of the bounding rectangle 015 @param y the top of the bounding rectangle 016 @param width the width of the bounding rectangle 017 */ 018 public CarShape(int x, int y, int width) 019 { 020 Rectangle2D.Double body 021 = new Rectangle2D.Double(x, y + width / 6, 022 width - 1, width / 6); 023 Ellipse2D.Double frontTire 024 = new Ellipse2D.Double(x + width / 6, y + width / 3, 025 width / 6, width / 6); 026 Ellipse2D.Double rearTire 027 = new Ellipse2D.Double(x + width * 2 / 3, 028 y + width / 3, 029 width / 6, width / 6); 030 031 // The bottom of the front windshield 032 Point2D.Double r1 033 = new Point2D.Double(x + width / 6, y + width / 6); 034 // The front of the roof 035 Point2D.Double r2 036 = new Point2D.Double(x + width / 3, y); 037 // The rear of the roof 038 Point2D.Double r3 039 = new Point2D.Double(x + width * 2 / 3, y); 040 // The bottom of the rear windshield 041 Point2D.Double r4 042 = new Point2D.Double(x + width * 5 / 6, y + width / 6); 043 Line2D.Double frontWindshield 044 = new Line2D.Double(r1, r2); 045 Line2D.Double roofTop 046 = new Line2D.Double(r2, r3); 047 Line2D.Double rearWindshield 048 = new Line2D.Double(r3, r4); 049 050 add(body); 051 add(frontTire); 052 add(rearTire); 053 add(frontWindshield); 054 add(roofTop); 055 add(rearWindshield); 056 } 057}