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.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.animation.D2.Renderable2DInterface;
20 import nl.tudelft.simulation.naming.context.Contextualized;
21
22 /**
23 * Draw a traffic light on the road at th place where the cars are expected to stop.
24 * <p>
25 * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27 * </p>
28 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
30 */
31 public class TrafficLightAnimation extends AbstractLineAnimation<TrafficLight>
32 implements Renderable2DInterface<TrafficLight>, Serializable
33 {
34 /** */
35 private static final long serialVersionUID = 20160000L;
36
37 /** the Text object to destroy when the animation is destroyed. */
38 private final Text text;
39
40 /**
41 * Construct the DefaultCarAnimation for a LaneBlock (road block).
42 * @param trafficLight TrafficLight; the CSEBlock to draw
43 * @param simulator OtsSimulatorInterface; the simulator to schedule on
44 * @throws NamingException in case of registration failure of the animation
45 * @throws RemoteException on communication failure
46 */
47 public TrafficLightAnimation(final TrafficLight trafficLight, final OtsSimulatorInterface simulator)
48 throws NamingException, RemoteException
49 {
50 super(trafficLight, simulator, 0.9, new Length(0.5, LengthUnit.SI));
51
52 this.text = new Text(trafficLight,
53 trafficLight.getLane().getParentLink().getId() + "." + trafficLight.getLane().getId() + trafficLight.getId(),
54 0.0f, (float) getHalfLength() + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
55 }
56
57 /**
58 * @return text.
59 */
60 public final Text getText()
61 {
62 return this.text;
63 }
64
65 /**
66 * {@inheritDoc}
67 */
68 @Override
69 public final void paint(final Graphics2D graphics, final ImageObserver observer)
70 {
71 TrafficLight trafficLight = getSource();
72 Color fillColor;
73 switch (trafficLight.getTrafficLightColor())
74 {
75 case RED:
76 fillColor = Color.red;
77 break;
78
79 case YELLOW:
80 fillColor = Color.yellow;
81 break;
82
83 case GREEN:
84 fillColor = Color.green;
85 break;
86
87 default:
88 fillColor = Color.black;
89 break;
90 }
91
92 // PaintPolygons.paintMultiPolygon(graphics, fillColor, trafficLight.getLocation(), trafficLight.getGeometry(), false);
93 graphics.setColor(fillColor);
94 super.paint(graphics, observer);
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public void destroy(final Contextualized contextProvider)
100 {
101 super.destroy(contextProvider);
102 this.text.destroy(contextProvider);
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public final String toString()
108 {
109 return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
110 }
111
112 /**
113 * Text animation for the TrafficLight. Separate class to be able to turn it on and off...
114 * <p>
115 * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
116 * <br>
117 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
118 * </p>
119 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
120 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
121 * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
122 */
123 public class Text extends TextAnimation
124 {
125 /** */
126 private static final long serialVersionUID = 20161211L;
127
128 /**
129 * @param source Locatable; the object for which the text is displayed
130 * @param text String; the text to display
131 * @param dx float; the horizontal movement of the text, in meters
132 * @param dy float; the vertical movement of the text, in meters
133 * @param textPlacement TextAlignment; where to place the text
134 * @param color Color; the color of the text
135 * @param simulator OtsSimulatorInterface; the simulator
136 * @throws NamingException when animation context cannot be created or retrieved
137 * @throws RemoteException - when remote context cannot be found
138 */
139 public Text(final Locatable source, final String text, final float dx, final float dy,
140 final TextAlignment textPlacement, final Color color, final OtsSimulatorInterface simulator)
141 throws RemoteException, NamingException
142 {
143 super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
144 }
145
146 /** {@inheritDoc} */
147 @Override
148 public final String toString()
149 {
150 return "Text []";
151 }
152 }
153
154 }