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.rmi.RemoteException;
7 import java.util.function.Supplier;
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.RenderableTextSource;
14 import org.opentrafficsim.draw.TextAlignment;
15 import org.opentrafficsim.draw.road.AbstractLineAnimation.LaneBasedObjectData;
16 import org.opentrafficsim.draw.road.TrafficLightAnimation.TrafficLightData;
17
18 import nl.tudelft.simulation.naming.context.Contextualized;
19
20 /**
21 * Draw a traffic light on the road at th place where the cars are expected to stop.
22 * <p>
23 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25 * </p>
26 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
27 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
28 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
29 */
30 public class TrafficLightAnimation extends AbstractLineAnimation<TrafficLightData>
31 {
32 /** the Text object to destroy when the animation is destroyed. */
33 private final Text text;
34
35 /**
36 * Construct the DefaultCarAnimation for a LaneBlock (road block).
37 * @param trafficLight the traffic light
38 * @param contextualized context provider
39 * @throws NamingException in case of registration failure of the animation
40 * @throws RemoteException on communication failure
41 */
42 public TrafficLightAnimation(final TrafficLightData trafficLight, final Contextualized contextualized)
43 throws NamingException, RemoteException
44 {
45 super(trafficLight, contextualized, new Length(0.5, LengthUnit.SI));
46
47 float halfLength = (float) (trafficLight.getLine().getLength() / 2.0);
48 this.text = new Text(trafficLight, trafficLight::getId, 0.0f, halfLength + 0.2f, TextAlignment.CENTER, Color.BLACK,
49 contextualized);
50 }
51
52 /**
53 * Return text object.
54 * @return text.
55 */
56 public final Text getText()
57 {
58 return this.text;
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 @Override
65 public final void paint(final Graphics2D graphics, final ImageObserver observer)
66 {
67 graphics.setColor(getSource().getColor());
68 super.paint(graphics, observer);
69 }
70
71 @Override
72 public void destroy(final Contextualized contextProvider)
73 {
74 super.destroy(contextProvider);
75 this.text.destroy(contextProvider);
76 }
77
78 @Override
79 public final String toString()
80 {
81 return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
82 }
83
84 /**
85 * Text animation for the TrafficLight. Separate class to be able to turn it on and off...
86 * <p>
87 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
88 * <br>
89 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
90 * </p>
91 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
92 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
93 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
94 */
95 public static class Text extends RenderableTextSource<TrafficLightData, Text>
96 {
97 /**
98 * Constructor.
99 * @param source the object for which the text is displayed
100 * @param text the text to display
101 * @param dx the horizontal movement of the text, in meters
102 * @param dy the vertical movement of the text, in meters
103 * @param textPlacement where to place the text
104 * @param color the color of the text
105 * @param contextualized context provider
106 * @throws NamingException when animation context cannot be created or retrieved
107 * @throws RemoteException - when remote context cannot be found
108 */
109 public Text(final TrafficLightData source, final Supplier<String> text, final float dx, final float dy,
110 final TextAlignment textPlacement, final Color color, final Contextualized contextualized)
111 throws RemoteException, NamingException
112 {
113 super(source, text, dx, dy, textPlacement, color, contextualized, RenderableTextSource.RENDERWHEN10);
114 }
115
116 @Override
117 public final String toString()
118 {
119 return "Text []";
120 }
121 }
122
123 /**
124 * TrafficLightData provides the information required to draw a traffic light.
125 * <p>
126 * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
127 * <br>
128 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
129 * </p>
130 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
131 */
132 public interface TrafficLightData extends LaneBasedObjectData
133 {
134 /**
135 * Returns the traffic light color.
136 * @return traffic light color.
137 */
138 Color getColor();
139 }
140
141 }