-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficLightView.java
More file actions
157 lines (135 loc) · 4.39 KB
/
Copy pathTrafficLightView.java
File metadata and controls
157 lines (135 loc) · 4.39 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package bs7trafficlight;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class TrafficLightView extends JPanel {
/** default */
private static final long serialVersionUID = 1L;
/** Size of the complete window frame. It is slightly larger than the png's */
public static final int SIZE_X = 726;
/** Size of the view in Y direction */
public static final int SIZE_VIEW_Y = 711;
/** Size of the buttons in Y direction */
public static final int SIZE_BTT_Y = 89;
/** Size of the complete window frame in Y direction */
public static final int SIZE_Y = SIZE_VIEW_Y + SIZE_BTT_Y;
/** Names of all images in the project */
private static final String[] IMG_NAMES = {
"traffic_light.png",
"car_rd.png",
"car_yl.png",
"car_gn.png",
"pers_rd.png",
"pers_gn.png"
};
/** positions of the images in the IMG_NAMES array. This will be needed
* also in the images Array which holds the preloaded files
*/
private static final int IMG_BACKGROUND = 0;
private static final int IMG_CAR_RED = 1;
private static final int IMG_CAR_YEL = 2;
private static final int IMG_CAR_GRN = 3;
private static final int IMG_PER_RED = 4;
private static final int IMG_PER_GRN = 5;
/** location of the images within the project */
private static final String IMG_PATH = "/bs7trafficlight/img/";
/** Preloaded images as BufferedImage objects */
private BufferedImage[] images;
/** holds all images that will be shown in the repaint */
private ArrayList<BufferedImage> visibleImages = new ArrayList<>();
/** controller - only needed for the url generation in the image loader*/
private TrafficLightControl myControl;
/**
* Constructor for preparing the object
* @param myControl Reference to the controller
*/
public TrafficLightView(TrafficLightControl myControl) {
this.myControl = myControl;
validate();
initializeView();
}
/**
* Basic initialization
*/
private void initializeView() {
// load images into the buffer array
getImages();
// place the background on position 0
visibleImages.add(images[IMG_BACKGROUND]);
}
/**
* Load the images based on the IMG_NAMES array into the
* image buffer
*/
private void getImages() {
images = new BufferedImage[IMG_NAMES.length];
for (int i = 0; i < IMG_NAMES.length; i++) {
images[i] = getImage(IMG_PATH + IMG_NAMES[i]);
}
}
/**
* Load a single image from the given (relative) path
* @param fileName File name including path
* @return File loaded as BufferedImage object
*/
public BufferedImage getImage(String fileName) {
// generate a url based on the location of the main class
// (with the main method) and the relative path
URL url = myControl.getClass().getResource(fileName);
BufferedImage myImage = null;
try {
myImage = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
return myImage;
}
/**
* Overwritten paintComponent method. This will be called if the
* system sees a need for repaint or the program requested a repaint
* by calling the repaint method.
*/
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
// only the images in the visibleImages ArrayList will be drawn
for (BufferedImage myImg : visibleImages) {
g2D.drawImage(myImg, null, 0, 0);
}
}
/**
* Requests the active lights from the models and places the corresponding images
* into the visibleImages ArrayList.
* @param carLight Model of the car traffic light
* @param persLight Model of the pedestrian traffic light
*/
public void setImages(CarTrafficLightModel carLight, PersonTrafficLightModel persLight) {
// delete all except background (which is always on index 0)
while(visibleImages.size() > 1) {
visibleImages.remove(1);
}
// check which lights should be active and place the images into the ArrayList
if (carLight.isGreenOn()) {
visibleImages.add(images[IMG_CAR_GRN]);
}
if (carLight.isYellowOn()) {
visibleImages.add(images[IMG_CAR_YEL]);
}
if (carLight.isRedOn()) {
visibleImages.add(images[IMG_CAR_RED]);
}
if (persLight.isGreenOn()) {
visibleImages.add(images[IMG_PER_GRN]);
}
if (persLight.isRedOn()) {
visibleImages.add(images[IMG_PER_RED]);
}
// trigger the view to repaint
repaint();
}
}