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