NodeAnimation.java

  1. package org.opentrafficsim.draw.network;

  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.geom.Ellipse2D;
  6. import java.awt.geom.GeneralPath;
  7. import java.awt.geom.Path2D;
  8. import java.awt.image.ImageObserver;
  9. import java.rmi.RemoteException;
  10. import java.util.function.Supplier;

  11. import javax.naming.NamingException;

  12. import org.djutils.base.Identifiable;
  13. import org.djutils.draw.point.OrientedPoint2d;
  14. import org.opentrafficsim.base.geometry.OtsLocatable;
  15. import org.opentrafficsim.base.geometry.OtsRenderable;
  16. import org.opentrafficsim.draw.DrawLevel;
  17. import org.opentrafficsim.draw.TextAlignment;
  18. import org.opentrafficsim.draw.TextAnimation;
  19. import org.opentrafficsim.draw.network.NodeAnimation.NodeData;

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

  21. /**
  22.  * Draws NodeData.
  23.  * <p>
  24.  * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  25.  * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  26.  * </p>
  27.  * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  28.  * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  29.  */
  30. public class NodeAnimation extends OtsRenderable<NodeData>
  31. {
  32.     /** */
  33.     private static final long serialVersionUID = 20140000L;

  34.     /** the Text object to destroy when the animation is destroyed. */
  35.     private Text text;

  36.     /**
  37.      * @param node NodeData; node data.
  38.      * @param contextualized Contextualized; context provider
  39.      * @throws NamingException when animation context cannot be found.
  40.      * @throws RemoteException on communication failure
  41.      */
  42.     public NodeAnimation(final NodeData node, final Contextualized contextualized) throws NamingException, RemoteException
  43.     {
  44.         super(node, contextualized);
  45.         this.text = new Text(node, node::getId, 0.0f, 3.0f, TextAlignment.CENTER, Color.BLACK, contextualized,
  46.                 TextAnimation.RENDERWHEN10);
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public final void paint(final Graphics2D graphics, final ImageObserver observer)
  51.     {
  52.         setRendering(graphics);
  53.         graphics.setColor(Color.BLACK);
  54.         graphics.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
  55.         graphics.draw(new Ellipse2D.Double(-0.5, -0.5, 1.0, 1.0));
  56.         double direction = getSource().getLocation().getDirZ();
  57.         if (!Double.isNaN(direction))
  58.         {
  59.             GeneralPath arrow = new GeneralPath(Path2D.WIND_EVEN_ODD, 3);
  60.             arrow.moveTo(0.5, -0.5);
  61.             arrow.lineTo(2, 0);
  62.             arrow.lineTo(0.5, 0.5);
  63.             graphics.draw(arrow);
  64.         }
  65.         resetRendering(graphics);
  66.     }

  67.     /** {@inheritDoc} */
  68.     @Override
  69.     public void destroy(final Contextualized contextProvider)
  70.     {
  71.         super.destroy(contextProvider);
  72.         this.text.destroy(contextProvider);
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public final String toString()
  77.     {
  78.         return "NodeAnimation [node=" + super.getSource() + "]";
  79.     }

  80.     /**
  81.      * Text animation for the Node. Separate class to be able to turn it on and off...
  82.      * <p>
  83.      * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  84.      * <br>
  85.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  86.      * </p>
  87.      * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
  88.      * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
  89.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  90.      */
  91.     public class Text extends TextAnimation<NodeData, Text>
  92.     {
  93.         /** */
  94.         private static final long serialVersionUID = 20161211L;

  95.         /**
  96.          * @param source NodeData; the object for which the text is displayed
  97.          * @param text Supplier&lt;String&gt;; the text to display
  98.          * @param dx float; the horizontal movement of the text, in meters
  99.          * @param dy float; the vertical movement of the text, in meters
  100.          * @param textPlacement TextAlignment; where to place the text
  101.          * @param color Color; the color of the text
  102.          * @param contextualized Contextualized; context provider
  103.          * @param scaleDependentRendering ScaleDependendentRendering; size limiter for text animation
  104.          * @throws NamingException when animation context cannot be created or retrieved
  105.          * @throws RemoteException - when remote context cannot be found
  106.          */
  107.         @SuppressWarnings("checkstyle:parameternumber")
  108.         public Text(final NodeData source, final Supplier<String> text, final float dx, final float dy,
  109.                 final TextAlignment textPlacement, final Color color, final Contextualized contextualized,
  110.                 final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
  111.         {
  112.             super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, contextualized, scaleDependentRendering);
  113.             setFlip(false);
  114.             setRotate(false);
  115.         }

  116.         /** {@inheritDoc} */
  117.         @Override
  118.         public final String toString()
  119.         {
  120.             return "NodeAnimation.Text []";
  121.         }
  122.     }

  123.     /**
  124.      * NodeData provides the information required to draw a node.
  125.      * <p>
  126.      * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
  127.      * <br>
  128.      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  129.      * </p>
  130.      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
  131.      */
  132.     public interface NodeData extends OtsLocatable, Identifiable
  133.     {
  134.         /** {@inheritDoc} */
  135.         @Override
  136.         public OrientedPoint2d getLocation();

  137.         /** {@inheritDoc} */
  138.         @Override
  139.         default double getZ()
  140.         {
  141.             return DrawLevel.NODE.getZ();
  142.         }
  143.     }

  144. }