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