-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCar.java
More file actions
87 lines (75 loc) · 2.15 KB
/
Copy pathCar.java
File metadata and controls
87 lines (75 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
A car shape that can be positioned anywhere on the screen.
*/
public class Car
{
public static int WIDTH = 60;
public static int HEIGHT = 30;
private int xLeft;
private int yTop;
/**
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(int x, int y)
{
xLeft = x;
yTop = y;
}
/**
Moves the car.
@param dx the amount to move in x-direction
@param dy the amount to move in y-direction
*/
public void move(double dx, double dy)
{
xLeft += dx;
yTop += dy;
}
/**
Draws the car using methods in AWT
@param g2 the graphics context
*/
public void draw(Graphics2D g2)
{
//draw the roads for the cars
g2.setStroke(new BasicStroke(25));
Rectangle road1 = new Rectangle (50,140,1150,20);
Rectangle road2 = new Rectangle (50,340,1150,20);
g2.draw(road1);
g2.draw(road2);
g2.setColor(Color.RED);
// draw the car
g2.setStroke(new BasicStroke(5));
Rectangle body = new Rectangle(xLeft, yTop + 10, 60, 10);
Ellipse2D.Double frontTire = new Ellipse2D.Double(xLeft + 10, yTop + 20, 10, 10);
Ellipse2D.Double rearTire = new Ellipse2D.Double(xLeft + 40, yTop + 20, 10, 10);
// front view
Point2D.Double r1 = new Point2D.Double(xLeft + 10, yTop + 10);
// roof of the car
Point2D.Double r2 = new Point2D.Double(xLeft + 20, yTop);
Point2D.Double r3 = new Point2D.Double(xLeft + 40, yTop);
//rear view
Point2D.Double r4 = new Point2D.Double(xLeft + 50, yTop + 10);
Line2D.Double frontWindshield = new Line2D.Double(r1, r2);
Line2D.Double roofTop = new Line2D.Double(r2, r3);
Line2D.Double rearWindshield = new Line2D.Double(r3, r4);
g2.draw(body);
g2.draw(frontTire);
g2.draw(rearTire);
g2.draw(frontWindshield);
g2.draw(roofTop);
g2.draw(rearWindshield);
g2.setColor(Color.BLACK);
}
}