View Javadoc
1   package org.opentrafficsim.core.network.animation;
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.animation.ClonableRenderable2DInterface;
12  import org.opentrafficsim.core.animation.TextAlignment;
13  import org.opentrafficsim.core.animation.TextAnimation;
14  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
15  import org.opentrafficsim.core.network.Link;
16  
17  import nl.tudelft.simulation.dsol.animation.Locatable;
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  import nl.tudelft.simulation.language.d2.Angle;
20  import nl.tudelft.simulation.language.d3.DirectedPoint;
21  
22  /**
23   * Draws a Link.
24   * <p>
25   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * <p>
28   * $LastChangedDate: 2017-04-29 12:51:08 +0200 (Sat, 29 Apr 2017) $, @version $Revision: 3570 $, by $Author: averbraeck $,
29   * initial version Sep 13, 2014 <br>
30   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
31   */
32  public class LinkAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
33  {
34      /** */
35      private static final long serialVersionUID = 20140000L;
36  
37      /** */
38      private float width;
39  
40      /** the Text object to destroy when the animation is destroyed. */
41      private Text text;
42  
43      /**
44       * @param link Link
45       * @param simulator simulator
46       * @param width width
47       * @throws NamingException for problems with registering in context
48       * @throws RemoteException on communication failure
49       */
50      public LinkAnimation(final Link link, final OTSSimulatorInterface simulator, final float width)
51              throws NamingException, RemoteException
52      {
53          super(link, simulator);
54          this.width = width;
55          this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator);
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
61      {
62          PaintLine.paintLine(graphics, Color.RED, this.width, getSource().getLocation(), ((Link) getSource()).getDesignLine());
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final void destroy() throws NamingException
68      {
69          super.destroy();
70          this.text.destroy();
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      @SuppressWarnings("checkstyle:designforextension")
76      public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
77              throws NamingException, RemoteException
78      {
79          // the constructor also constructs the corresponding Text object
80          return new LinkAnimation((Link) newSource, newSimulator, this.width);
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final String toString()
86      {
87          return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
88      }
89  
90      /**
91       * Text animation for the Link. Separate class to be able to turn it on and off...
92       * <p>
93       * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
94       * <br>
95       * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
96       * </p>
97       * $LastChangedDate: 2017-04-29 12:51:08 +0200 (Sat, 29 Apr 2017) $, @version $Revision: 3570 $, by $Author: averbraeck $,
98       * initial version Dec 11, 2016 <br>
99       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
100      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
101      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
102      */
103     public class Text extends TextAnimation
104     {
105         /** */
106         private static final long serialVersionUID = 20161211L;
107 
108         /**
109          * @param source the object for which the text is displayed
110          * @param text the text to display
111          * @param dx the horizontal movement of the text, in meters
112          * @param dy the vertical movement of the text, in meters
113          * @param textPlacement where to place the text
114          * @param color the color of the text
115          * @param simulator the simulator
116          * @throws NamingException when animation context cannot be created or retrieved
117          * @throws RemoteException - when remote context cannot be found
118          */
119         public Text(final Locatable source, final String text, final float dx, final float dy,
120                 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
121                 throws RemoteException, NamingException
122         {
123             super(source, text, dx, dy, textPlacement, color, simulator);
124         }
125 
126         /** {@inheritDoc} */
127         @Override
128         @SuppressWarnings("checkstyle:designforextension")
129         public DirectedPoint getLocation() throws RemoteException
130         {
131             // draw always on top, and not upside down.
132             DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
133             double a = Angle.normalizePi(p.getRotZ());
134             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
135             {
136                 a += Math.PI;
137             }
138             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
139         }
140 
141         /** {@inheritDoc} */
142         @Override
143         @SuppressWarnings("checkstyle:designforextension")
144         public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
145                 throws RemoteException, NamingException
146         {
147             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
148         }
149 
150         /** {@inheritDoc} */
151         @Override
152         public final String toString()
153         {
154             return "LinkAnimation.Text []";
155         }
156     }
157 
158 }