View Javadoc
1   package org.opentrafficsim.draw.road;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Graphics2D;
6   import java.awt.geom.Path2D;
7   import java.awt.image.ImageObserver;
8   import java.rmi.RemoteException;
9   import java.util.Set;
10  import java.util.function.Supplier;
11  
12  import javax.naming.NamingException;
13  
14  import org.djutils.base.Identifiable;
15  import org.djutils.draw.point.DirectedPoint2d;
16  import org.opentrafficsim.draw.DrawLevel;
17  import org.opentrafficsim.draw.OtsRenderable;
18  import org.opentrafficsim.draw.PaintPolygons;
19  import org.opentrafficsim.draw.RenderableTextSource;
20  import org.opentrafficsim.draw.TextAlignment;
21  import org.opentrafficsim.draw.road.TrafficLightDetectorAnimation.TrafficLightDetectorData;
22  
23  import nl.tudelft.simulation.naming.context.Contextualized;
24  
25  /**
26   * Traffic light detector animation.
27   * <p>
28   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
29   * All rights reserved. <br>
30   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
34   */
35  public class TrafficLightDetectorAnimation extends OtsRenderable<TrafficLightDetectorData>
36  {
37      /** The traffic light detector. */
38      private final TrafficLightDetectorData detector;
39  
40      /** Path of the detector. */
41      private final Set<Path2D.Float> paths;
42  
43      /** the Text object to destroy when the animation is destroyed. */
44      private final Text text;
45  
46      /**
47       * Construct a TrafficLightDetectorAnimation.
48       * @param detector the traffic light detector that will be animated
49       * @param contextualized context provider
50       * @throws NamingException in case of registration failure of the animation
51       * @throws RemoteException in case of remote registration failure of the animation
52       */
53      public TrafficLightDetectorAnimation(final TrafficLightDetectorData detector, final Contextualized contextualized)
54              throws NamingException, RemoteException
55      {
56          super(detector, contextualized);
57          this.detector = detector;
58          this.paths = PaintPolygons.getPaths(this.detector.getAbsoluteContour().getPointList());
59          this.text = new Text(detector, detector::getId, 0.0f, 0.5f + 0.2f, TextAlignment.CENTER, Color.BLACK, contextualized);
60      }
61  
62      @Override
63      public final void paint(final Graphics2D graphics, final ImageObserver observer)
64      {
65          graphics.setStroke(new BasicStroke(0.2f));
66          setRendering(graphics);
67          PaintPolygons.paintPaths(graphics, this.detector.getOccupancy() ? Color.BLUE : Color.BLACK, this.paths, false);
68          resetRendering(graphics);
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 "TrafficLightDetectorAnimation [getSource()=" + this.getSource() + "]";
82      }
83  
84      /**
85       * Text animation for the Detector. 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<TrafficLightDetectorData, Text> implements DetectorData.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 TrafficLightDetectorData 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      * TrafficLightDetectorData provides the information required to draw a traffic light detector.
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 TrafficLightDetectorData extends DetectorData, Identifiable
133     {
134         /**
135          * Returns whether the detector is occupied.
136          * @return whether the detector is occupied.
137          */
138         boolean getOccupancy();
139 
140         @Override
141         DirectedPoint2d getLocation();
142 
143         @Override
144         default double getZ()
145         {
146             return DrawLevel.OBJECT.getZ();
147         }
148     }
149 
150 }