View Javadoc
1   package org.opentrafficsim.road.network.lane.object.trafficlight;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.geom.Rectangle2D;
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.dsol.OTSSimulatorInterface;
13  
14  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
15  
16  /**
17   * Draw a traffic light on the road at th place where the cars are expected to stop.
18   * <p>
19   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
23   *          initial version 29 dec. 2014 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   */
27  public class TrafficLightAnimation extends Renderable2D implements Serializable
28  {
29      /** */
30      private static final long serialVersionUID = 20160000L;
31  
32      /** The half width left and right of the center line that is used to draw the block. */
33      private final double halfWidth;
34  
35      /**
36       * Construct the DefaultCarAnimation for a LaneBlock (road block).
37       * @param source the CSEBlock to draw
38       * @param simulator the simulator to schedule on
39       * @throws NamingException in case of registration failure of the animation
40       * @throws RemoteException on communication failure
41       */
42      public TrafficLightAnimation(final TrafficLight source, final OTSSimulatorInterface simulator)
43              throws NamingException, RemoteException
44      {
45          super(source, simulator);
46          this.halfWidth = 0.45 * source.getLane().getWidth(source.getLongitudinalPosition()).getSI();
47      }
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
54      {
55          TrafficLight trafficLight = (TrafficLight) this.getSource();
56          Color fillColor;
57          switch (trafficLight.getTrafficLightColor())
58          {
59              case RED:
60                  fillColor = Color.red;
61                  break;
62  
63              case YELLOW:
64                  fillColor = Color.yellow;
65                  break;
66  
67              case GREEN:
68                  fillColor = Color.green;
69                  break;
70  
71              default:
72                  fillColor = Color.black;
73                  break;
74          }
75          
76          // PaintPolygons.paintMultiPolygon(graphics, fillColor, trafficLight.getLocation(), trafficLight.getGeometry(), false);
77          graphics.setColor(fillColor);
78          Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
79          graphics.fill(rectangle);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final String toString()
85      {
86          return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
87      }
88  
89  }