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