View Javadoc
1   package org.opentrafficsim.draw.network;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Graphics2D;
6   import java.awt.geom.Ellipse2D;
7   import java.awt.geom.GeneralPath;
8   import java.awt.geom.Path2D;
9   import java.awt.image.ImageObserver;
10  import java.rmi.RemoteException;
11  import java.util.function.Supplier;
12  
13  import javax.naming.NamingException;
14  
15  import org.djutils.base.Identifiable;
16  import org.djutils.draw.point.OrientedPoint2d;
17  import org.opentrafficsim.base.geometry.OtsLocatable;
18  import org.opentrafficsim.base.geometry.OtsRenderable;
19  import org.opentrafficsim.draw.DrawLevel;
20  import org.opentrafficsim.draw.TextAlignment;
21  import org.opentrafficsim.draw.TextAnimation;
22  import org.opentrafficsim.draw.network.NodeAnimation.NodeData;
23  
24  import nl.tudelft.simulation.naming.context.Contextualized;
25  
26  /**
27   * Draws NodeData.
28   * <p>
29   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * </p>
32   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
33   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
34   */
35  public class NodeAnimation extends OtsRenderable<NodeData>
36  {
37      /** */
38      private static final long serialVersionUID = 20140000L;
39  
40      /** the Text object to destroy when the animation is destroyed. */
41      private Text text;
42  
43      /**
44       * @param node NodeData; node data.
45       * @param contextualized Contextualized; context provider
46       * @throws NamingException when animation context cannot be found.
47       * @throws RemoteException on communication failure
48       */
49      public NodeAnimation(final NodeData node, final Contextualized contextualized) throws NamingException, RemoteException
50      {
51          super(node, contextualized);
52          this.text = new Text(node, node::getId, 0.0f, 3.0f, TextAlignment.CENTER, Color.BLACK, contextualized,
53                  TextAnimation.RENDERWHEN10);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public final void paint(final Graphics2D graphics, final ImageObserver observer)
59      {
60          setRendering(graphics);
61          graphics.setColor(Color.BLACK);
62          graphics.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER));
63          graphics.draw(new Ellipse2D.Double(-0.5, -0.5, 1.0, 1.0));
64          double direction = getSource().getLocation().getDirZ();
65          if (!Double.isNaN(direction))
66          {
67              GeneralPath arrow = new GeneralPath(Path2D.WIND_EVEN_ODD, 3);
68              arrow.moveTo(0.5, -0.5);
69              arrow.lineTo(2, 0);
70              arrow.lineTo(0.5, 0.5);
71              graphics.draw(arrow);
72          }
73          resetRendering(graphics);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public void destroy(final Contextualized contextProvider)
79      {
80          super.destroy(contextProvider);
81          this.text.destroy(contextProvider);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public final String toString()
87      {
88          return "NodeAnimation [node=" + super.getSource() + "]";
89      }
90  
91      /**
92       * Text animation for the Node. Separate class to be able to turn it on and off...
93       * <p>
94       * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
95       * <br>
96       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
97       * </p>
98       * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
99       * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
100      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
101      */
102     public class Text extends TextAnimation<NodeData, Text>
103     {
104         /** */
105         private static final long serialVersionUID = 20161211L;
106 
107         /**
108          * @param source NodeData; the object for which the text is displayed
109          * @param text Supplier&lt;String&gt;; the text to display
110          * @param dx float; the horizontal movement of the text, in meters
111          * @param dy float; the vertical movement of the text, in meters
112          * @param textPlacement TextAlignment; where to place the text
113          * @param color Color; the color of the text
114          * @param contextualized Contextualized; context provider
115          * @param scaleDependentRendering ScaleDependendentRendering; size limiter for text animation
116          * @throws NamingException when animation context cannot be created or retrieved
117          * @throws RemoteException - when remote context cannot be found
118          */
119         @SuppressWarnings("checkstyle:parameternumber")
120         public Text(final NodeData source, final Supplier<String> text, final float dx, final float dy,
121                 final TextAlignment textPlacement, final Color color, final Contextualized contextualized,
122                 final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
123         {
124             super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, contextualized, scaleDependentRendering);
125             setFlip(false);
126             setRotate(false);
127         }
128 
129         /** {@inheritDoc} */
130         @Override
131         public final String toString()
132         {
133             return "NodeAnimation.Text []";
134         }
135     }
136 
137     /**
138      * NodeData provides the information required to draw a node.
139      * <p>
140      * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
141      * <br>
142      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
143      * </p>
144      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
145      */
146     public interface NodeData extends OtsLocatable, Identifiable
147     {
148         /** {@inheritDoc} */
149         @Override
150         public OrientedPoint2d getLocation();
151 
152         /** {@inheritDoc} */
153         @Override
154         default double getZ()
155         {
156             return DrawLevel.NODE.getZ();
157         }
158     }
159 
160 }