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