View Javadoc
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.io.Serializable;
7   import java.rmi.RemoteException;
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.core.dsol.OtsSimulatorInterface;
14  import org.opentrafficsim.draw.core.TextAlignment;
15  import org.opentrafficsim.draw.core.TextAnimation;
16  import org.opentrafficsim.road.network.lane.object.detector.LaneDetector;
17  import nl.tudelft.simulation.dsol.animation.Locatable;
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
19  import nl.tudelft.simulation.naming.context.Contextualized;
20  
21  /**
22   * Detector animation.
23   * <p>
24   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
25   * All rights reserved. <br>
26   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * </p>
28   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
30   */
31  public class DetectorAnimation extends AbstractLineAnimation<LaneDetector> implements Renderable2DInterface<LaneDetector>, Serializable
32  {
33      /** */
34      private static final long serialVersionUID = 20150130L;
35  
36      /** The color of the detector. */
37      private final Color color;
38  
39      /** the Text object to destroy when the animation is destroyed. */
40      private final Text text;
41  
42      /**
43       * Construct a DetectorAnimation.
44       * @param detector Detector; the Sensor to draw
45       * @param simulator OtsSimulatorInterface; the simulator to schedule on
46       * @param color Color; the display color of the detector
47       * @throws NamingException in case of registration failure of the animation
48       * @throws RemoteException in case of remote registration failure of the animation
49       */
50      public DetectorAnimation(final LaneDetector detector, final OtsSimulatorInterface simulator, final Color color)
51              throws NamingException, RemoteException
52      {
53          super(detector, simulator, .9, new Length(0.5, LengthUnit.SI));
54          this.color = color;
55  
56          this.text = new Text(detector,
57                  detector.getLane().getParentLink().getId() + "." + detector.getLane().getId() + detector.getId(), 0.0f,
58                  (float) getHalfLength() + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
59      }
60  
61      /**
62       * @return text.
63       */
64      public final Text getText()
65      {
66          return this.text;
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final void paint(final Graphics2D graphics, final ImageObserver observer)
72      {
73          graphics.setColor(this.color);
74          super.paint(graphics, observer);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public void destroy(final Contextualized contextProvider)
80      {
81          super.destroy(contextProvider);
82          this.text.destroy(contextProvider);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public final String toString()
88      {
89          return "DetectorAnimation [getSource()=" + this.getSource() + "]";
90      }
91  
92      /**
93       * Text animation for the Detector. Separate class to be able to turn it on and off...
94       * <p>
95       * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
96       * <br>
97       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
98       * </p>
99       * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
100      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
101      * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
102      */
103     public class Text extends TextAnimation
104     {
105         /** */
106         private static final long serialVersionUID = 20161211L;
107 
108         /**
109          * @param source Locatable; the object for which the text is displayed
110          * @param text String; the text to display
111          * @param dx float; the horizontal movement of the text, in meters
112          * @param dy float; the vertical movement of the text, in meters
113          * @param textPlacement TextAlignment; where to place the text
114          * @param color Color; the color of the text
115          * @param simulator OtsSimulatorInterface; the simulator
116          * @throws NamingException when animation context cannot be created or retrieved
117          * @throws RemoteException - when remote context cannot be found
118          */
119         public Text(final Locatable source, final String text, final float dx, final float dy,
120                 final TextAlignment textPlacement, final Color color, final OtsSimulatorInterface simulator)
121                 throws RemoteException, NamingException
122         {
123             super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
124         }
125 
126         /** {@inheritDoc} */
127         @Override
128         public final String toString()
129         {
130             return "Text []";
131         }
132     }
133 
134 }