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  
25  
26  
27  
28  
29  
30  
31  
32  public class NodeAnimation extends OtsRenderable<NodeData>
33  {
34      
35      private static final long serialVersionUID = 20140000L;
36  
37      
38      private Text text;
39  
40      
41  
42  
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  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94      public class Text extends TextAnimation<NodeData, Text>
95      {
96          
97          private static final long serialVersionUID = 20161211L;
98  
99          
100 
101 
102 
103 
104 
105 
106 
107 
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 
128 
129 
130 
131 
132 
133 
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 }