BusStopAnimation.java

  1. package org.opentrafficsim.draw.road;

  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.image.ImageObserver;
  5. import java.rmi.RemoteException;
  6. import java.util.function.Supplier;

  7. import javax.naming.NamingException;

  8. import org.djunits.unit.LengthUnit;
  9. import org.djunits.value.vdouble.scalar.Length;
  10. import org.opentrafficsim.draw.ClickableLineLocatable;
  11. import org.opentrafficsim.draw.TextAlignment;
  12. import org.opentrafficsim.draw.TextAnimation;
  13. import org.opentrafficsim.draw.road.AbstractLineAnimation.LaneBasedObjectData;
  14. import org.opentrafficsim.draw.road.BusStopAnimation.BusStopData;

  15. import nl.tudelft.simulation.naming.context.Contextualized;

  16. /**
  17.  * Draw BusStopData.
  18.  * <p>
  19.  * Copyright (c) 2013-2024 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="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  22.  * </p>
  23.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  24.  * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  25.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  26.  */
  27. public class BusStopAnimation extends AbstractLineAnimation<BusStopData>
  28. {
  29.     /** */
  30.     private static final long serialVersionUID = 20150130L;

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

  33.     /**
  34.      * Construct a DetectorAnimation.
  35.      * @param laneDetector the lane detector to draw
  36.      * @param contextualized context provider
  37.      * @throws NamingException in case of registration failure of the animation
  38.      * @throws RemoteException in case of remote registration failure of the animation
  39.      */
  40.     public BusStopAnimation(final BusStopData laneDetector, final Contextualized contextualized)
  41.             throws NamingException, RemoteException
  42.     {
  43.         super(laneDetector, contextualized, new Length(0.5, LengthUnit.SI));
  44.         float halfLength = (float) (laneDetector.getLine().getLength() / 2.0);
  45.         this.text = new Text(laneDetector, laneDetector::getId, 0.0f, halfLength + 0.2f, TextAlignment.CENTER,
  46.                 Color.BLACK, contextualized);
  47.     }

  48.     /**
  49.      * @return text.
  50.      */
  51.     public final Text getText()
  52.     {
  53.         return this.text;
  54.     }

  55.     @Override
  56.     public final void paint(final Graphics2D graphics, final ImageObserver observer)
  57.     {
  58.         graphics.setColor(Color.WHITE);
  59.         super.paint(graphics, observer);
  60.     }

  61.     @Override
  62.     public void destroy(final Contextualized contextProvider)
  63.     {
  64.         super.destroy(contextProvider);
  65.         this.text.destroy(contextProvider);
  66.     }

  67.     @Override
  68.     public final String toString()
  69.     {
  70.         return "DetectorAnimation [getSource()=" + this.getSource() + "]";
  71.     }

  72.     /**
  73.      * Text animation for the Detector. Separate class to be able to turn it on and off...
  74.      * <p>
  75.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  76.      * <br>
  77.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  78.      * </p>
  79.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  80.      * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
  81.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  82.      */
  83.     public class Text extends TextAnimation<BusStopData, Text>
  84.     {
  85.         /** */
  86.         private static final long serialVersionUID = 20161211L;

  87.         /**
  88.          * @param source the object for which the text is displayed
  89.          * @param text the text to display
  90.          * @param dx the horizontal movement of the text, in meters
  91.          * @param dy the vertical movement of the text, in meters
  92.          * @param textPlacement where to place the text
  93.          * @param color the color of the text
  94.          * @param contextualized context provider
  95.          * @throws NamingException when animation context cannot be created or retrieved
  96.          * @throws RemoteException - when remote context cannot be found
  97.          */
  98.         public Text(final BusStopData source, final Supplier<String> text, final float dx, final float dy,
  99.                 final TextAlignment textPlacement, final Color color, final Contextualized contextualized)
  100.                 throws RemoteException, NamingException
  101.         {
  102.             super(source, text, dx, dy, textPlacement, color, contextualized, TextAnimation.RENDERWHEN10);
  103.         }

  104.         @Override
  105.         public final String toString()
  106.         {
  107.             return "Text []";
  108.         }
  109.     }

  110.     /**
  111.      * BusStopData provides the information required to draw a bus stop.
  112.      * <p>
  113.      * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  114.      * <br>
  115.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  116.      * </p>
  117.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  118.      */
  119.     public interface BusStopData extends LaneBasedObjectData, ClickableLineLocatable
  120.     {
  121.     }

  122. }