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.rmi.RemoteException;
9   
10  import javax.naming.NamingException;
11  
12  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
13  
14  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
15  import org.opentrafficsim.core.network.animation.PaintLine;
16  import org.opentrafficsim.core.network.animation.PaintPolygons;
17  import org.opentrafficsim.road.network.lane.Lane;
18  
19  /**
20   * <p>
21   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * <p>
24   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
25   * initial 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      /** Whether to draw the center line or not. */
34      private final boolean drawCenterLine;
35  
36      /**
37       * @param source s
38       * @param simulator s
39       * @param color color of the lane.
40       * @param drawCenterLine whether to draw the center line or not
41       * @throws NamingException ne
42       * @throws RemoteException on communication failure
43       */
44      public LaneAnimation(final Lane source, final OTSSimulatorInterface simulator, final Color color,
45          final boolean drawCenterLine) throws NamingException, RemoteException
46      {
47          super(source, simulator);
48          this.color = color;
49          this.drawCenterLine = drawCenterLine;
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public final void paint(final Graphics2D graphics, final ImageObserver observer)
55      {
56          Lane lane = (Lane) getSource();
57          if (this.color != null)
58          {
59              PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
60          }
61  
62          if (this.drawCenterLine)
63          {
64              PaintLine.paintLine(graphics, Color.RED, 0.25, lane.getLocation(), lane.getCenterLine());
65              Shape startCircle =
66                  new Ellipse2D.Double(lane.getCenterLine().getFirst().x - lane.getLocation().x - 0.25, -lane
67                      .getCenterLine().getFirst().y + lane.getLocation().y - 0.25, 0.5, 0.5);
68              Shape endCircle =
69                  new Ellipse2D.Double(lane.getCenterLine().getLast().x - lane.getLocation().x - 0.25, -lane
70                      .getCenterLine().getLast().y + lane.getLocation().y - 0.25, 0.5, 0.5);
71              graphics.setColor(Color.BLUE);
72              graphics.fill(startCircle);
73              graphics.setColor(Color.RED);
74              graphics.fill(endCircle);
75          }
76      }
77  }