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.ClonableRenderable2DInterface;
15  import org.opentrafficsim.draw.core.TextAlignment;
16  import org.opentrafficsim.draw.core.TextAnimation;
17  import org.opentrafficsim.road.network.lane.object.sensor.SingleSensor;
18  
19  import nl.tudelft.simulation.dsol.animation.Locatable;
20  import nl.tudelft.simulation.naming.context.Contextualized;
21  
22  /**
23   * Sensor animation.
24   * <p>
25   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
26   * All rights reserved. <br>
27   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
28   * <p>
29   * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
30   * initial version Jan 30, 2015 <br>
31   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
32   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
33   */
34  public class SensorAnimation extends AbstractLineAnimation<SingleSensor>
35          implements ClonableRenderable2DInterface<SingleSensor>, Serializable
36  {
37      /** */
38      private static final long serialVersionUID = 20150130L;
39  
40      /** the position of the sensor on the lane to determine the width of the lane at that point. */
41      private final Length sensorPosition;
42  
43      /** The color of the sensor. */
44      private final Color color;
45  
46      /** the Text object to destroy when the animation is destroyed. */
47      private final Text text;
48  
49      /**
50       * Construct a SensorAnimation.
51       * @param sensor SingleSensor; the Sensor to draw
52       * @param sensorPosition Length; the position of the sensor on the lane to determine the width of the lane at that point
53       * @param simulator OTSSimulatorInterface; the simulator to schedule on
54       * @param color Color; the display color of the sensor
55       * @throws NamingException in case of registration failure of the animation
56       * @throws RemoteException in case of remote registration failure of the animation
57       */
58      public SensorAnimation(final SingleSensor sensor, final Length sensorPosition,
59              final OTSSimulatorInterface simulator, final Color color) throws NamingException, RemoteException
60      {
61          super(sensor, simulator, .9, new Length(0.5, LengthUnit.SI));
62          this.sensorPosition = sensorPosition;
63          this.color = color;
64  
65          this.text = new Text(sensor, sensor.getLane().getParentLink().getId() + "." + sensor.getLane().getId() + sensor.getId(),
66                  0.0f, (float) getHalfLength() + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
67      }
68  
69      /**
70       * @return text.
71       */
72      public final Text getText()
73      {
74          return this.text;
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public final void paint(final Graphics2D graphics, final ImageObserver observer)
80      {
81          graphics.setColor(this.color);
82          super.paint(graphics, observer);
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public void destroy(final Contextualized contextProvider)
88      {
89          super.destroy(contextProvider);
90          this.text.destroy(contextProvider);
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      @SuppressWarnings("checkstyle:designforextension")
96      public ClonableRenderable2DInterface<SingleSensor> clone(final SingleSensor newSource,
97              final OTSSimulatorInterface newSimulator) throws NamingException, RemoteException
98      {
99          // the constructor also constructs the corresponding Text object
100         return new SensorAnimation(newSource, this.sensorPosition, newSimulator, this.color);
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final String toString()
106     {
107         return "SensorAnimation [getSource()=" + this.getSource() + "]";
108     }
109 
110     /**
111      * Text animation for the Sensor. Separate class to be able to turn it on and off...
112      * <p>
113      * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
114      * <br>
115      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
116      * </p>
117      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
118      * initial version Dec 11, 2016 <br>
119      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
120      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
121      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
122      */
123     public class Text extends TextAnimation
124     {
125         /** */
126         private static final long serialVersionUID = 20161211L;
127 
128         /**
129          * @param source Locatable; the object for which the text is displayed
130          * @param text String; the text to display
131          * @param dx float; the horizontal movement of the text, in meters
132          * @param dy float; the vertical movement of the text, in meters
133          * @param textPlacement TextAlignment; where to place the text
134          * @param color Color; the color of the text
135          * @param simulator OTSSimulatorInterface; the simulator
136          * @throws NamingException when animation context cannot be created or retrieved
137          * @throws RemoteException - when remote context cannot be found
138          */
139         public Text(final Locatable source, final String text, final float dx, final float dy,
140                 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
141                 throws RemoteException, NamingException
142         {
143             super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
144         }
145 
146         /** {@inheritDoc} */
147         @Override
148         @SuppressWarnings("checkstyle:designforextension")
149         public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
150                 throws RemoteException, NamingException
151         {
152             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
153         }
154 
155         /** {@inheritDoc} */
156         @Override
157         public final String toString()
158         {
159             return "Text []";
160         }
161     }
162 
163 }