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