View Javadoc
1   package org.opentrafficsim.road.network.animation;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.Shape;
6   import java.awt.geom.Ellipse2D;
7   import java.awt.image.ImageObserver;
8   import java.io.Serializable;
9   import java.rmi.RemoteException;
10  
11  import javax.naming.NamingException;
12  
13  import org.opentrafficsim.core.animation.ClonableRenderable2DInterface;
14  import org.opentrafficsim.core.animation.TextAlignment;
15  import org.opentrafficsim.core.animation.TextAnimation;
16  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
17  import org.opentrafficsim.core.network.animation.PaintLine;
18  import org.opentrafficsim.core.network.animation.PaintPolygons;
19  import org.opentrafficsim.road.network.lane.Lane;
20  
21  import nl.tudelft.simulation.dsol.animation.Locatable;
22  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
23  import nl.tudelft.simulation.language.d2.Angle;
24  import nl.tudelft.simulation.language.d3.DirectedPoint;
25  
26  /**
27   * <p>
28   * Copyright (c) 2013-2016 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: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, 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  public class LaneAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
36  {
37      /** */
38      private static final long serialVersionUID = 20141017L;
39  
40      /** Color of the lane. */
41      private final Color color;
42  
43      /** Whether to draw the center line or not. */
44      private final boolean drawCenterLine;
45  
46      /** the Text object to destroy when the animation is destroyed. */
47      private Text text;
48  
49      /**
50       * Animate a Lane.
51       * @param lane Lane; the lane
52       * @param simulator OTSSimulatorInterface; the simulator
53       * @param color Color of the lane.
54       * @param drawCenterLine boolean; whether to draw the center line or not
55       * @throws NamingException in case of registration failure of the animation
56       * @throws RemoteException on communication failure
57       */
58      public LaneAnimation(final Lane lane, final OTSSimulatorInterface simulator, final Color color,
59              final boolean drawCenterLine) throws NamingException, RemoteException
60      {
61          super(lane, simulator);
62          this.color = color;
63          this.drawCenterLine = drawCenterLine;
64          new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK,
65                  simulator);
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final void paint(final Graphics2D graphics, final ImageObserver observer)
71      {
72          Lane lane = (Lane) getSource();
73          if (this.color != null)
74          {
75              PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
76          }
77  
78          if (this.drawCenterLine)
79          {
80              PaintLine.paintLine(graphics, Color.RED, 0.25, lane.getLocation(), lane.getCenterLine());
81              Shape startCircle = new Ellipse2D.Double(lane.getCenterLine().getFirst().x - lane.getLocation().x - 0.25,
82                      -lane.getCenterLine().getFirst().y + lane.getLocation().y - 0.25, 0.5, 0.5);
83              Shape endCircle = new Ellipse2D.Double(lane.getCenterLine().getLast().x - lane.getLocation().x - 0.25,
84                      -lane.getCenterLine().getLast().y + lane.getLocation().y - 0.25, 0.5, 0.5);
85              graphics.setColor(Color.BLUE);
86              graphics.fill(startCircle);
87              graphics.setColor(Color.RED);
88              graphics.fill(endCircle);
89          }
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public final void destroy() throws NamingException
95      {
96          super.destroy();
97          this.text.destroy();
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     @SuppressWarnings("checkstyle:designforextension")
103     public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
104             throws NamingException, RemoteException
105     {
106         // the constructor also constructs the corresponding Text object
107         return new LaneAnimation((Lane) newSource, newSimulator, this.color, this.drawCenterLine);
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public final String toString()
113     {
114         return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + ", drawCenterLine="
115                 + this.drawCenterLine + "]";
116     }
117 
118     /**
119      * Text animation for the Node. Separate class to be able to turn it on and off...
120      * <p>
121      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
122      * <br>
123      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
124      * </p>
125      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
126      * initial version Dec 11, 2016 <br>
127      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
128      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
129      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
130      */
131     public class Text extends TextAnimation
132     {
133         /** */
134         private static final long serialVersionUID = 20161211L;
135 
136         /**
137          * @param source the object for which the text is displayed
138          * @param text the text to display
139          * @param dx the horizontal movement of the text, in meters
140          * @param dy the vertical movement of the text, in meters
141          * @param textPlacement where to place the text
142          * @param color the color of the text
143          * @param simulator the simulator
144          * @throws NamingException when animation context cannot be created or retrieved
145          * @throws RemoteException - when remote context cannot be found
146          */
147         public Text(final Locatable source, final String text, final float dx, final float dy,
148                 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
149                 throws RemoteException, NamingException
150         {
151             super(source, text, dx, dy, textPlacement, color, simulator);
152         }
153 
154         /** {@inheritDoc} */
155         @Override
156         @SuppressWarnings("checkstyle:designforextension")
157         public DirectedPoint getLocation() throws RemoteException
158         {
159             // draw always on top.
160             DirectedPoint p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
161             double a = Angle.normalizePi(p.getRotZ());
162             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
163             {
164                 a += Math.PI;
165             }
166             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
167         }
168 
169         /** {@inheritDoc} */
170         @Override
171         @SuppressWarnings("checkstyle:designforextension")
172         public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
173                 throws RemoteException, NamingException
174         {
175             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
176         }
177 
178     }
179 
180 }