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