View Javadoc
1   package org.opentrafficsim.draw.network;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.geom.Ellipse2D;
6   import java.awt.image.ImageObserver;
7   import java.io.Serializable;
8   import java.rmi.RemoteException;
9   
10  import javax.media.j3d.BoundingSphere;
11  import javax.media.j3d.Bounds;
12  import javax.naming.NamingException;
13  
14  import org.djutils.logger.CategoryLogger;
15  import org.opentrafficsim.core.network.Node;
16  import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
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.simulators.SimulatorInterface;
23  import nl.tudelft.simulation.introspection.DelegateIntrospection;
24  import nl.tudelft.simulation.language.d3.DirectedPoint;
25  
26  /**
27   * <p>
28   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
30   * <p>
31   * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
32   * initial version Oct 17, 2014 <br>
33   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34   */
35  @SuppressWarnings("rawtypes")
36  public class NodeAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
37  {
38      /** */
39      private static final long serialVersionUID = 20140000L;
40  
41      /** the Text object to destroy when the animation is destroyed. */
42      private Text text;
43  
44      /** Ensure that node animations are slightly above lane surface. */
45      public static final double ZOFFSET = 0.01;
46  
47      /**
48       * @param node Node; n
49       * @param simulator SimulatorInterface.TimeDoubleUnit; s
50       * @throws NamingException when animation context cannot be found.
51       * @throws RemoteException on communication failure
52       */
53      @SuppressWarnings("unchecked")
54      public NodeAnimation(final Node node, final SimulatorInterface.TimeDoubleUnit simulator)
55              throws NamingException, RemoteException
56      {
57          super(new ElevatedNode(node), simulator);
58          this.text = new Text(node, node.getId(), 0.0f, 3.0f, TextAlignment.CENTER, Color.BLACK, simulator);
59      }
60  
61      /** {@inheritDoc} */
62      @Override
63      public final void paint(final Graphics2D graphics, final ImageObserver observer)
64      {
65          graphics.setColor(Color.BLACK);
66          graphics.draw(new Ellipse2D.Double(-0.5, -0.5, 1.0, 1.0));
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final void destroy() throws NamingException
72      {
73          super.destroy();
74          this.text.destroy();
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      @SuppressWarnings("checkstyle:designforextension")
80      public ClonableRenderable2DInterface clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
81              throws NamingException, RemoteException
82      {
83          // the constructor also constructs the corresponding Text object and ElevatedNode
84          return new NodeAnimation((Node) newSource, newSimulator);
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public final String toString()
90      {
91          return "NodeAnimation [node=" + super.getSource() + "]";
92      }
93  
94      /** Class for elevating the node for animation purposes. */
95      public static class ElevatedNode implements Locatable, DelegateIntrospection
96      {
97          /** the node for introspection. */
98          private final Node node;
99          
100         /** the location of the node to which the animation belongs. */
101         private DirectedPoint location;
102 
103         /** the bounds of the node to which the animation belongs. */
104         private Bounds bounds;
105 
106         /**
107          * @param node Node; the node to which the animation belongs
108          */
109         public ElevatedNode(final Node node)
110         {
111             super();
112             this.node = node;
113             try
114             {
115                 this.location = new DirectedPoint(node.getLocation().x, node.getLocation().y, node.getLocation().z + ZOFFSET);
116                 this.bounds = node.getBounds();
117             }
118             catch (RemoteException exception)
119             {
120                 CategoryLogger.always().error(exception, "Could not construct elevated node for animation");
121                 this.location = new DirectedPoint();
122                 this.bounds = new BoundingSphere();
123             }
124         }
125 
126         /** {@inheritDoc} */
127         @Override
128         public DirectedPoint getLocation() throws RemoteException
129         {
130             return this.location;
131         }
132 
133         /** {@inheritDoc} */
134         @Override
135         public Bounds getBounds() throws RemoteException
136         {
137             return this.bounds;
138         }
139 
140         /** {@inheritDoc} */
141         @Override
142         public Object getParentIntrospectionObject()
143         {
144             return this.node;
145         }
146 
147     }
148 
149     /**
150      * Text animation for the Node. Separate class to be able to turn it on and off...
151      * <p>
152      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
153      * <br>
154      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
155      * </p>
156      * $LastChangedDate: 2018-10-11 22:54:04 +0200 (Thu, 11 Oct 2018) $, @version $Revision: 4696 $, by $Author: averbraeck $,
157      * initial version Dec 11, 2016 <br>
158      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
159      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
160      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
161      */
162     public class Text extends TextAnimation
163     {
164         /** */
165         private static final long serialVersionUID = 20161211L;
166 
167         /**
168          * @param source Locatable; the object for which the text is displayed
169          * @param text String; the text to display
170          * @param dx float; the horizontal movement of the text, in meters
171          * @param dy float; the vertical movement of the text, in meters
172          * @param textPlacement TextAlignment; where to place the text
173          * @param color Color; the color of the text
174          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
175          * @throws NamingException when animation context cannot be created or retrieved
176          * @throws RemoteException - when remote context cannot be found
177          */
178         public Text(final Locatable source, final String text, final float dx, final float dy,
179                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
180                 throws RemoteException, NamingException
181         {
182             super(source, text, dx, dy, textPlacement, color, simulator);
183             setFlip(false);
184             setRotate(false);
185         }
186 
187         /** {@inheritDoc} */
188         @Override
189         @SuppressWarnings("checkstyle:designforextension")
190         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
191                 throws RemoteException, NamingException
192         {
193             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
194         }
195 
196         /** {@inheritDoc} */
197         @Override
198         public final String toString()
199         {
200             return "NodeAnimation.Text []";
201         }
202     }
203 
204 }