-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoad.java
More file actions
111 lines (91 loc) · 3.27 KB
/
Road.java
File metadata and controls
111 lines (91 loc) · 3.27 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package Model;
import java.awt.*;
import java.util.ArrayList;
public class Road {
public enum Orientation {
HORIZONTAL, VERTICAL
}
private Orientation orientation;
private String id;
private int speedLimit;
private int length;
private int width;
private int[] startLocation;
private int[] endLocation;
private ArrayList<Vehicle> vehiclesOnRoad = new ArrayList<>();
private ArrayList<TrafficLight> lightsOnRoad = new ArrayList<>();
private ArrayList<Road> connectedRoads = new ArrayList<>();
public Road(String id, int speedLimit, int length, int[] startLocation, Orientation orientation) {
this.id = "road_" + id;
this.speedLimit = speedLimit;
this.length = length;
width = 8;
this.orientation = orientation;
this.startLocation = startLocation;
setEndLocation();
}
public void draw(Graphics g, int scale) {
if (orientation == Orientation.HORIZONTAL) {
int[] startLocation = this.startLocation;
int x = startLocation[0] * scale;
int y = startLocation[1] * scale;
int width = length * scale;
int height = this.width * scale;
g.setColor(Color.darkGray);
g.fillRect(x, y, width, height);
//Center Lines
g.setColor(Color.white);
g.fillRect(x, y + (height / 2) - scale / 6, width, scale / 6);
g.fillRect(x, y + (height / 2) + scale / 6, width, scale / 6);
} else if (orientation == Orientation.VERTICAL) {
int[] startLocation = this.startLocation;
int x = startLocation[0] * scale;
int y = startLocation[1] * scale;
int width = this.width * scale;
int height = length * scale;
g.setColor(Color.darkGray);
g.fillRect(x, y, width, height);
// //Center Lines
g.setColor(Color.white);
g.fillRect(x + (width / 2) - scale / 6, y, scale / 6, height);
g.fillRect(x + (width / 2) + scale / 6, y, scale / 6, height);
}
}
String getId() {
return id;
}
public int getSpeedLimit() {
return speedLimit;
}
public int getLength() {
return length;
}
int getWidth() {
return width;
}
private void setEndLocation() {
if (orientation == Orientation.HORIZONTAL) {
this.endLocation = new int[]{this.length + this.startLocation[0], this.startLocation[1]}; //only works for horizontal roads;
} else if (orientation == Orientation.VERTICAL) {
this.endLocation = new int[]{this.startLocation[1], this.length + this.startLocation[0]};
}
}
public int[] getStartLocation() {
return startLocation;
}
public int[] getEndLocation() {
return endLocation;
}
public ArrayList<Vehicle> getVehiclesOnRoad() {
return vehiclesOnRoad;
}
public ArrayList<TrafficLight> getLightsOnRoad() {
return lightsOnRoad;
}
public ArrayList<Road> getConnectedRoads() {
return connectedRoads;
}
Orientation getOrientation() {
return orientation;
}
}