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.OrientedPoint2d;
16  import org.opentrafficsim.draw.DrawLevel;
17  import org.opentrafficsim.draw.OtsRenderable;
18  import org.opentrafficsim.draw.PaintPolygons;
19  import org.opentrafficsim.draw.TextAlignment;
20  import org.opentrafficsim.draw.TextAnimation;
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      /** */
38      private static final long serialVersionUID = 20150130L;
39  
40      /** The traffic light detector. */
41      private final TrafficLightDetectorData detector;
42  
43      /** Path of the detector. */
44      private final Set<Path2D.Float> paths;
45  
46      /** the Text object to destroy when the animation is destroyed. */
47      private final Text text;
48  
49      /**
50       * Construct a TrafficLightDetectorAnimation.
51       * @param detector the traffic light detector that will be animated
52       * @param contextualized context provider
53       * @throws NamingException in case of registration failure of the animation
54       * @throws RemoteException in case of remote registration failure of the animation
55       */
56      public TrafficLightDetectorAnimation(final TrafficLightDetectorData detector, final Contextualized contextualized)
57              throws NamingException, RemoteException
58      {
59          super(detector, contextualized);
60          this.detector = detector;
61          this.paths = PaintPolygons.getPaths(this.detector.getContour().getPointList());
62          this.text = new Text(detector, detector::getId, 0.0f, 0.5f + 0.2f, TextAlignment.CENTER, Color.BLACK, contextualized);
63      }
64  
65      @Override
66      public final void paint(final Graphics2D graphics, final ImageObserver observer)
67      {
68          graphics.setStroke(new BasicStroke(0.2f));
69          setRendering(graphics);
70          PaintPolygons.paintPaths(graphics, this.detector.getOccupancy() ? Color.BLUE : Color.BLACK, this.paths, false);
71          resetRendering(graphics);
72      }
73  
74      @Override
75      public void destroy(final Contextualized contextProvider)
76      {
77          super.destroy(contextProvider);
78          this.text.destroy(contextProvider);
79      }
80  
81      @Override
82      public final String toString()
83      {
84          return "TrafficLightDetectorAnimation [getSource()=" + this.getSource() + "]";
85      }
86  
87      /**
88       * Text animation for the Detector. 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://github.com/peter-knoppers">Peter Knoppers</a>
96       * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
97       */
98      public class Text extends TextAnimation<TrafficLightDetectorData, Text> implements DetectorData.Text
99      {
100         /** */
101         private static final long serialVersionUID = 20161211L;
102 
103         /**
104          * @param source the object for which the text is displayed
105          * @param text the text to display
106          * @param dx the horizontal movement of the text, in meters
107          * @param dy the vertical movement of the text, in meters
108          * @param textPlacement where to place the text
109          * @param color the color of the text
110          * @param 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 TrafficLightDetectorData 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         @Override
122         public final String toString()
123         {
124             return "Text []";
125         }
126     }
127 
128     /**
129      * TrafficLightDetectorData provides the information required to draw a traffic light detector.
130      * <p>
131      * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
132      * <br>
133      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
134      * </p>
135      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
136      */
137     public interface TrafficLightDetectorData extends DetectorData, Identifiable
138     {
139         /**
140          * Returns whether the detector is occupied.
141          * @return whether the detector is occupied.
142          */
143         boolean getOccupancy();
144 
145         @Override
146         OrientedPoint2d getLocation();
147 
148         @Override
149         default double getZ()
150         {
151             return DrawLevel.OBJECT.getZ();
152         }
153     }
154 
155 }