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.Path2D;
6   import java.awt.image.ImageObserver;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  
11  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
12  import nl.tudelft.simulation.language.d3.DirectedPoint;
13  
14  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
15  import org.opentrafficsim.core.network.lane.Lane;
16  
17  import com.vividsolutions.jts.geom.Coordinate;
18  import com.vividsolutions.jts.geom.Geometry;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2014 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version Oct 17, 2014 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   */
28  public class LaneAnimation extends Renderable2D
29  {
30      /** color of the lane. */
31      private final Color color;
32  
33      /**
34       * @param source s
35       * @param simulator s
36       * @param color color of the lane.
37       * @throws NamingException ne
38       * @throws RemoteException re
39       */
40      public LaneAnimation(final Lane source, final OTSSimulatorInterface simulator, final Color color)
41          throws NamingException, RemoteException
42      {
43          super(source, simulator);
44          this.color = color;
45      }
46  
47      /** {@inheritDoc} */
48      @Override
49      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
50      {
51          graphics.setColor(this.color);
52          Lane lane = (Lane) getSource();
53          DirectedPoint p = lane.getLocation();
54          Geometry g = lane.getContour();
55          Coordinate[] coordinates = g.getCoordinates();
56          Path2D.Double path = new Path2D.Double();
57          boolean start = false;
58          for (Coordinate c : coordinates)
59          {
60              if (!start)
61              {
62                  start = true;
63                  path.moveTo(c.x - p.x, -c.y + p.y);
64              }
65              else
66              {
67                  path.lineTo(c.x - p.x, -c.y + p.y);
68              }
69          }
70          path.closePath();
71          graphics.fill(path);
72      }
73  }