1   package org.opentrafficsim.draw.network;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.image.ImageObserver;
6   import java.io.Serializable;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  
11  import org.opentrafficsim.core.geometry.OTSGeometryException;
12  import org.opentrafficsim.core.geometry.OTSLine3D;
13  import org.opentrafficsim.core.geometry.OTSPoint3D;
14  import org.opentrafficsim.core.network.Link;
15  import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
16  import org.opentrafficsim.draw.core.PaintLine;
17  import org.opentrafficsim.draw.core.TextAlignment;
18  import org.opentrafficsim.draw.core.TextAnimation;
19  
20  import nl.tudelft.simulation.dsol.animation.Locatable;
21  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
22  import nl.tudelft.simulation.dsol.logger.SimLogger;
23  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
24  import nl.tudelft.simulation.language.d2.Angle;
25  import nl.tudelft.simulation.language.d3.DirectedPoint;
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  public class LinkAnimation extends Renderable2D<Link> implements ClonableRenderable2DInterface<Link>, Serializable
38  {
39      
40      private static final long serialVersionUID = 20140000L;
41  
42      
43      private float width;
44  
45      
46      private Text text;
47  
48      
49  
50  
51  
52  
53  
54  
55      public LinkAnimation(final Link link, final SimulatorInterface.TimeDoubleUnit simulator, final float width)
56              throws NamingException, RemoteException
57      {
58          super(link, simulator);
59          this.width = width;
60          this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator);
61      }
62  
63      
64      @Override
65      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
66      {
67          Color color = getSource().getLinkType().isConnector() ? Color.BLUE : Color.RED;
68          OTSLine3D designLine = getSource().getDesignLine();
69          PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
70          
71          try
72          {
73              drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
74              drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
75          }
76          catch (OTSGeometryException exception)
77          {
78              
79              SimLogger.always().error(exception);
80          }
81      }
82  
83      
84  
85  
86  
87  
88  
89  
90      private void drawEndPoint(final OTSPoint3D endPoint, final OTSPoint3D nextPoint, final Graphics2D graphics)
91      {
92          
93          double dx = nextPoint.x - endPoint.x;
94          double dy = nextPoint.y - endPoint.y;
95          double length = endPoint.distanceSI(nextPoint);
96          
97          dx *= this.width / length;
98          dy *= this.width / length;
99          try
100         {
101             OTSLine3D line = new OTSLine3D(new OTSPoint3D(endPoint.x - dy, endPoint.y + dx, endPoint.z),
102                     new OTSPoint3D(endPoint.x + dy, endPoint.y - dx, endPoint.z));
103             PaintLine.paintLine(graphics, getSource().getLinkType().isConnector() ? Color.BLUE : Color.RED, this.width / 30,
104                     getSource().getLocation(), line);
105         }
106         catch (OTSGeometryException exception)
107         {
108             SimLogger.always().error(exception);
109         }
110         catch (RemoteException exception)
111         {
112             SimLogger.always().error(exception);
113         }
114     }
115 
116     
117     @Override
118     public final void destroy() throws NamingException
119     {
120         super.destroy();
121         this.text.destroy();
122     }
123 
124     
125     @Override
126     @SuppressWarnings("checkstyle:designforextension")
127     public ClonableRenderable2DInterface<Link> clone(final Link newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
128             throws NamingException, RemoteException
129     {
130         
131         return new LinkAnimation(newSource, newSimulator, this.width);
132     }
133 
134     
135     @Override
136     public final String toString()
137     {
138         return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
139     }
140 
141     
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154     public class Text extends TextAnimation
155     {
156         
157         private static final long serialVersionUID = 20161211L;
158 
159         
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170         public Text(final Locatable source, final String text, final float dx, final float dy,
171                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
172                 throws RemoteException, NamingException
173         {
174             super(source, text, dx, dy, textPlacement, color, simulator);
175         }
176 
177         
178         @Override
179         @SuppressWarnings("checkstyle:designforextension")
180         public DirectedPoint getLocation() throws RemoteException
181         {
182             
183             DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
184             double a = Angle.normalizePi(p.getRotZ());
185             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
186             {
187                 a += Math.PI;
188             }
189             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
190         }
191 
192         
193         @Override
194         @SuppressWarnings("checkstyle:designforextension")
195         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
196                 throws RemoteException, NamingException
197         {
198             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
199         }
200 
201         
202         @Override
203         public final String toString()
204         {
205             return "LinkAnimation.Text []";
206         }
207     }
208 
209 }