View Javadoc
1   package org.opentrafficsim.animation.road;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.image.ImageObserver;
6   import java.util.function.Supplier;
7   
8   import org.djunits.unit.LengthUnit;
9   import org.djunits.value.vdouble.scalar.Length;
10  import org.opentrafficsim.animation.RenderableTextSource;
11  import org.opentrafficsim.animation.TextAlignment;
12  import org.opentrafficsim.animation.road.AbstractLineAnimation.LaneBasedObjectData;
13  import org.opentrafficsim.animation.road.TrafficLightAnimation.Text;
14  import org.opentrafficsim.animation.road.TrafficLightAnimation.TrafficLightData;
15  
16  import nl.tudelft.simulation.naming.context.Contextualized;
17  
18  /**
19   * Draw a traffic light on the road at th place where the cars are expected to stop.
20   * <p>
21   * Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * </p>
24   * @author Alexander Verbraeck
25   * @author Peter Knoppers
26   * @author Wouter Schakel
27   */
28  public class TrafficLightAnimation extends AbstractLineAnimation<TrafficLightData, Text>
29  {
30  
31      /**
32       * Construct the DefaultCarAnimation for a LaneBlock (road block).
33       * @param trafficLight the traffic light
34       * @param contextualized context provider
35       */
36      public TrafficLightAnimation(final TrafficLightData trafficLight, final Contextualized contextualized)
37      {
38          super(trafficLight, contextualized, new Length(0.5, LengthUnit.SI));
39      }
40  
41      @Override
42      protected Text createText(final TrafficLightData source, final Contextualized contextualized, final String prefix)
43      {
44          float halfLength = (float) (source.getLine().getLength() / 2.0);
45          return new Text(source, source::getId, 0.0f, halfLength + 0.2f, contextualized);
46      }
47  
48      @Override
49      public final void paint(final Graphics2D graphics, final ImageObserver observer)
50      {
51          graphics.setColor(getSource().getColor());
52          super.paint(graphics, observer);
53      }
54  
55      @Override
56      public final String toString()
57      {
58          return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
59      }
60  
61      /**
62       * Text animation for the TrafficLight.
63       */
64      public static class Text extends RenderableTextSource<TrafficLightData, Text>
65      {
66          /**
67           * Constructor.
68           * @param source the object for which the text is displayed
69           * @param text the text to display
70           * @param dx the horizontal movement of the text, in meters
71           * @param dy the vertical movement of the text, in meters
72           * @param contextualized context provider
73           */
74          public Text(final TrafficLightData source, final Supplier<String> text, final float dx, final float dy,
75                  final Contextualized contextualized)
76          {
77              super(source, text, dx, dy, TextAlignment.CENTER, Color.BLACK, contextualized, RenderableTextSource.RENDERWHEN10);
78          }
79  
80          @Override
81          public final String toString()
82          {
83              return "Text []";
84          }
85      }
86  
87      /**
88       * TrafficLightData provides the information required to draw a traffic light.
89       */
90      public interface TrafficLightData extends LaneBasedObjectData
91      {
92          /**
93           * Returns the traffic light color.
94           * @return traffic light color
95           */
96          Color getColor();
97      }
98  
99  }