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