View Javadoc
1   package org.opentrafficsim.core.network.animation;
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.naming.NamingException;
11  
12  import org.opentrafficsim.core.animation.ClonableRenderable2DInterface;
13  import org.opentrafficsim.core.animation.TextAlignment;
14  import org.opentrafficsim.core.animation.TextAnimation;
15  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
16  import org.opentrafficsim.core.network.Node;
17  
18  import nl.tudelft.simulation.dsol.animation.Locatable;
19  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
20  
21  /**
22   * <p>
23   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
25   * <p>
26   * $LastChangedDate: 2017-04-28 03:35:59 +0200 (Fri, 28 Apr 2017) $, @version $Revision: 3569 $, by $Author: averbraeck $,
27   * initial version Oct 17, 2014 <br>
28   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
29   */
30  public class NodeAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
31  {
32      /** */
33      private static final long serialVersionUID = 20140000L;
34  
35      /** the Text object to destroy when the animation is destroyed. */
36      private Text text;
37  
38      /**
39       * @param node n
40       * @param simulator s
41       * @throws NamingException when animation context cannot be found.
42       * @throws RemoteException on communication failure
43       */
44      public NodeAnimation(final Node node, final OTSSimulatorInterface simulator) throws NamingException, RemoteException
45      {
46          super(node, simulator);
47          this.text = new Text(node, node.getId(), 0.0f, 3.0f, TextAlignment.CENTER, Color.BLACK, simulator);
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public final void paint(final Graphics2D graphics, final ImageObserver observer)
53      {
54          graphics.setColor(Color.BLACK);
55          graphics.draw(new Ellipse2D.Double(-1.0, -1.0, 2.0, 2.0));
56      }
57  
58      /** {@inheritDoc} */
59      @Override
60      public final void destroy() throws NamingException
61      {
62          super.destroy();
63          this.text.destroy();
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      @SuppressWarnings("checkstyle:designforextension")
69      public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
70              throws NamingException, RemoteException
71      {
72          // the constructor also constructs the corresponding Text object
73          return new NodeAnimation((Node) newSource, newSimulator);
74      }
75  
76      /** {@inheritDoc} */
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-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
87       * <br>
88       * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
89       * </p>
90       * $LastChangedDate: 2017-04-28 03:35:59 +0200 (Fri, 28 Apr 2017) $, @version $Revision: 3569 $, by $Author: averbraeck $,
91       * initial version Dec 11, 2016 <br>
92       * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
93       * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
94       * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
95       */
96      public class Text extends TextAnimation
97      {
98          /** */
99          private static final long serialVersionUID = 20161211L;
100 
101         /**
102          * @param source the object for which the text is displayed
103          * @param text the text to display
104          * @param dx the horizontal movement of the text, in meters
105          * @param dy the vertical movement of the text, in meters
106          * @param textPlacement where to place the text
107          * @param color the color of the text
108          * @param simulator the simulator
109          * @throws NamingException when animation context cannot be created or retrieved
110          * @throws RemoteException - when remote context cannot be found
111          */
112         public Text(final Locatable source, final String text, final float dx, final float dy,
113                 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
114                 throws RemoteException, NamingException
115         {
116             super(source, text, dx, dy, textPlacement, color, simulator);
117             setFlip(false);
118             setRotate(false);
119         }
120 
121         /** {@inheritDoc} */
122         @Override
123         @SuppressWarnings("checkstyle:designforextension")
124         public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
125                 throws RemoteException, NamingException
126         {
127             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
128         }
129 
130         /** {@inheritDoc} */
131         @Override
132         public final String toString()
133         {
134             return "NodeAnimation.Text []";
135         }
136     }
137 
138 }