Java Reference
In-Depth Information
4
import
java.awt.geom.Line2D;
5
import
java.awt.geom.Point2D;
6
7 /**
8
A car shape that can be positioned
anywhere on the screen.
9 */
10
public class
Car
11 {
12 /**
13
Constructs a car with a given top-left
corner.
14 @param x
the x-coordinate of the
top-left corner
15 @param y
the y-coordinate of the
top-left corner
16 */
17
public
Car(
int
x,
int
y)
18 {
19 xLeft = x;
20 yTop = y;
21 }
22
23 /**
24
Draws the car.
25 @param g2
the graphics context
26 */
27
public void
draw(Graphics2D g2)
28 {
29 Rectangle body
30 =
new
Rectangle(xLeft, yTop +
10
,
60
,
10
);
31 Ellipse2D.Double frontTire
32 =
new
Ellipse2D.Double(xLeft +
10
, yTop +
20
,
10
,
10
);
33 Ellipse2D.Double rearTire
34 =
new
Ellipse2D.Double(xLeft +
40
, yTop +
20
,
10
,
10
);
35
36 //
The bottom of the front windshield
37 Point2D.Double r1
38 =
new
Point2D.Double(xLeft +
10
,
yTop +
10
);
39 //
The front of the roof
40 Point2D.Double r2