SensorAnimation.java

  1. package org.opentrafficsim.road.network.animation;

  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.ImageObserver;
  5. import java.io.Serializable;
  6. import java.rmi.RemoteException;

  7. import javax.naming.NamingException;

  8. import org.djunits.unit.LengthUnit;
  9. import org.djunits.value.vdouble.scalar.Length;
  10. import org.opentrafficsim.core.animation.ClonableRenderable2DInterface;
  11. import org.opentrafficsim.core.animation.TextAlignment;
  12. import org.opentrafficsim.core.animation.TextAnimation;
  13. import org.opentrafficsim.road.network.lane.object.sensor.SingleSensor;

  14. import nl.tudelft.simulation.dsol.animation.Locatable;
  15. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;

  16. /**
  17.  * Sensor animation.
  18.  * <p>
  19.  * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
  20.  * All rights reserved. <br>
  21.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * <p>
  23.  * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
  24.  * initial version Jan 30, 2015 <br>
  25.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  26.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  27.  */
  28. public class SensorAnimation extends AbstractLineAnimation<SingleSensor>
  29.         implements ClonableRenderable2DInterface<SingleSensor>, Serializable
  30. {
  31.     /** */
  32.     private static final long serialVersionUID = 20150130L;

  33.     /** the position of the sensor on the lane to determine the width of the lane at that point. */
  34.     private final Length sensorPosition;

  35.     /** The color of the sensor. */
  36.     private final Color color;

  37.     /** the Text object to destroy when the animation is destroyed. */
  38.     private final Text text;

  39.     /**
  40.      * Construct a SensorAnimation.
  41.      * @param sensor Sensor; the Sensor to draw
  42.      * @param sensorPosition Length; the position of the sensor on the lane to determine the width of the lane at that point
  43.      * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator to schedule on
  44.      * @param color Color; the display color of the sensor
  45.      * @throws NamingException in case of registration failure of the animation
  46.      * @throws RemoteException in case of remote registration failure of the animation
  47.      */
  48.     public SensorAnimation(final SingleSensor sensor, final Length sensorPosition, final SimulatorInterface.TimeDoubleUnit simulator,
  49.             final Color color) throws NamingException, RemoteException
  50.     {
  51.         super(sensor, simulator, .9, new Length(0.5, LengthUnit.SI));
  52.         this.sensorPosition = sensorPosition;
  53.         this.color = color;

  54.         this.text = new Text(sensor, sensor.getLane().getParentLink().getId() + "." + sensor.getLane().getId() + sensor.getId(),
  55.                 0.0f, (float) getHalfLength() + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
  56.     }

  57.     /**
  58.      * @return text.
  59.      */
  60.     public final Text getText()
  61.     {
  62.         return this.text;
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
  67.     {
  68.         graphics.setColor(this.color);
  69.         super.paint(graphics, observer);
  70.     }

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public final void destroy() throws NamingException
  74.     {
  75.         super.destroy();
  76.         this.text.destroy();
  77.     }

  78.     /** {@inheritDoc} */
  79.     @Override
  80.     @SuppressWarnings("checkstyle:designforextension")
  81.     public ClonableRenderable2DInterface<SingleSensor> clone(final SingleSensor newSource,
  82.             final SimulatorInterface.TimeDoubleUnit newSimulator) throws NamingException, RemoteException
  83.     {
  84.         // the constructor also constructs the corresponding Text object
  85.         return new SensorAnimation(newSource, this.sensorPosition, newSimulator, this.color);
  86.     }

  87.     /** {@inheritDoc} */
  88.     @Override
  89.     public final String toString()
  90.     {
  91.         return "SensorAnimation [getSource()=" + this.getSource() + "]";
  92.     }

  93.     /**
  94.      * Text animation for the Sensor. Separate class to be able to turn it on and off...
  95.      * <p>
  96.      * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  97.      * <br>
  98.      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
  99.      * </p>
  100.      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
  101.      * initial version Dec 11, 2016 <br>
  102.      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  103.      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  104.      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  105.      */
  106.     public class Text extends TextAnimation
  107.     {
  108.         /** */
  109.         private static final long serialVersionUID = 20161211L;

  110.         /**
  111.          * @param source Locatable; the object for which the text is displayed
  112.          * @param text String; the text to display
  113.          * @param dx float; the horizontal movement of the text, in meters
  114.          * @param dy float; the vertical movement of the text, in meters
  115.          * @param textPlacement TextAlignment; where to place the text
  116.          * @param color Color; the color of the text
  117.          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
  118.          * @throws NamingException when animation context cannot be created or retrieved
  119.          * @throws RemoteException - when remote context cannot be found
  120.          */
  121.         public Text(final Locatable source, final String text, final float dx, final float dy,
  122.                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
  123.                 throws RemoteException, NamingException
  124.         {
  125.             super(source, text, dx, dy, textPlacement, color, simulator);
  126.         }

  127.         /** {@inheritDoc} */
  128.         @Override
  129.         @SuppressWarnings("checkstyle:designforextension")
  130.         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
  131.                 throws RemoteException, NamingException
  132.         {
  133.             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
  134.         }

  135.         /** {@inheritDoc} */
  136.         @Override
  137.         public final String toString()
  138.         {
  139.             return "Text []";
  140.         }
  141.     }

  142. }