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