1 package org.opentrafficsim.draw.road;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.image.ImageObserver;
6 import java.io.Serializable;
7 import java.rmi.RemoteException;
8
9 import javax.naming.NamingException;
10
11 import org.djunits.unit.LengthUnit;
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
14 import org.opentrafficsim.draw.core.TextAlignment;
15 import org.opentrafficsim.draw.core.TextAnimation;
16 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
17
18 import nl.tudelft.simulation.dsol.animation.Locatable;
19 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class TrafficLightAnimation extends AbstractLineAnimation<TrafficLight>
33 implements ClonableRenderable2DInterface<TrafficLight>, Serializable
34 {
35
36 private static final long serialVersionUID = 20160000L;
37
38
39 private final Text text;
40
41
42
43
44
45
46
47
48 public TrafficLightAnimation(final TrafficLight trafficLight, final SimulatorInterface.TimeDoubleUnit simulator)
49 throws NamingException, RemoteException
50 {
51 super(trafficLight, simulator, 0.9, new Length(0.5, LengthUnit.SI));
52
53 this.text = new Text(trafficLight,
54 trafficLight.getLane().getParentLink().getId() + "." + trafficLight.getLane().getId() + trafficLight.getId(),
55 0.0f, (float) getHalfLength() + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
56 }
57
58
59
60
61 public final Text getText()
62 {
63 return this.text;
64 }
65
66
67
68
69 @Override
70 public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
71 {
72 TrafficLight trafficLight = getSource();
73 Color fillColor;
74 switch (trafficLight.getTrafficLightColor())
75 {
76 case RED:
77 fillColor = Color.red;
78 break;
79
80 case YELLOW:
81 fillColor = Color.yellow;
82 break;
83
84 case GREEN:
85 fillColor = Color.green;
86 break;
87
88 default:
89 fillColor = Color.black;
90 break;
91 }
92
93
94 graphics.setColor(fillColor);
95 super.paint(graphics, observer);
96 }
97
98
99 @Override
100 public final void destroy() throws NamingException
101 {
102 super.destroy();
103 this.text.destroy();
104 }
105
106
107 @Override
108 @SuppressWarnings("checkstyle:designforextension")
109 public ClonableRenderable2DInterface<TrafficLight> clone(final TrafficLight newSource,
110 final SimulatorInterface.TimeDoubleUnit newSimulator) throws NamingException, RemoteException
111 {
112
113 return new TrafficLightAnimation(newSource, newSimulator);
114 }
115
116
117 @Override
118 public final String toString()
119 {
120 return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public class Text extends TextAnimation
137 {
138
139 private static final long serialVersionUID = 20161211L;
140
141
142
143
144
145
146
147
148
149
150
151
152 public Text(final Locatable source, final String text, final float dx, final float dy,
153 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
154 throws RemoteException, NamingException
155 {
156 super(source, text, dx, dy, textPlacement, color, simulator);
157 }
158
159
160 @Override
161 @SuppressWarnings("checkstyle:designforextension")
162 public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
163 throws RemoteException, NamingException
164 {
165 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
166 }
167
168
169 @Override
170 public final String toString()
171 {
172 return "Text []";
173 }
174 }
175
176 }