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 nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
14  
15  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
16  import org.opentrafficsim.core.network.animation.PaintLine;
17  import org.opentrafficsim.core.network.animation.PaintPolygons;
18  import org.opentrafficsim.road.network.lane.Lane;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2016 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/docs/license.html">OpenTrafficSim License</a>.
24   * <p>
25   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
26   * initial version Oct 17, 2014 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   */
29  public class LaneAnimation extends Renderable2D implements Serializable
30  {
31      /** */
32      private static final long serialVersionUID = 20141017L;
33  
34      /** Color of the lane. */
35      private final Color color;
36  
37      /** Whether to draw the center line or not. */
38      private final boolean drawCenterLine;
39  
40      /**
41       * @param source s
42       * @param simulator s
43       * @param color color of the lane.
44       * @param drawCenterLine whether to draw the center line or not
45       * @throws NamingException ne
46       * @throws RemoteException on communication failure
47       */
48      public LaneAnimation(final Lane source, final OTSSimulatorInterface simulator, final Color color,
49          final boolean drawCenterLine) throws NamingException, RemoteException
50      {
51          super(source, simulator);
52          this.color = color;
53          this.drawCenterLine = drawCenterLine;
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public final void paint(final Graphics2D graphics, final ImageObserver observer)
59      {
60          Lane lane = (Lane) getSource();
61          if (this.color != null)
62          {
63              PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
64          }
65  
66          if (this.drawCenterLine)
67          {
68              PaintLine.paintLine(graphics, Color.RED, 0.25, lane.getLocation(), lane.getCenterLine());
69              Shape startCircle =
70                  new Ellipse2D.Double(lane.getCenterLine().getFirst().x - lane.getLocation().x - 0.25, -lane
71                      .getCenterLine().getFirst().y + lane.getLocation().y - 0.25, 0.5, 0.5);
72              Shape endCircle =
73                  new Ellipse2D.Double(lane.getCenterLine().getLast().x - lane.getLocation().x - 0.25, -lane
74                      .getCenterLine().getLast().y + lane.getLocation().y - 0.25, 0.5, 0.5);
75              graphics.setColor(Color.BLUE);
76              graphics.fill(startCircle);
77              graphics.setColor(Color.RED);
78              graphics.fill(endCircle);
79          }
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final String toString()
85      {
86          return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + ", drawCenterLine=" + this.drawCenterLine + "]";
87      }
88  }