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.image.ImageObserver;
6   import java.io.Serializable;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  
11  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
12  import org.opentrafficsim.core.network.animation.PaintPolygons;
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      /**
33       * Construct the DefaultCarAnimation for a LaneBlock (road block).
34       * @param source the CSEBlock to draw
35       * @param simulator the simulator to schedule on
36       * @throws NamingException in case of registration failure of the animation
37       * @throws RemoteException on communication failure
38       */
39      public TrafficLightAnimation(final TrafficLight source, final OTSSimulatorInterface simulator)
40              throws NamingException, RemoteException
41      {
42          super(source, simulator);
43          // setTranslate(false);
44          // setRotate(false);
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
52      {
53          TrafficLight trafficLight = (TrafficLight) this.getSource();
54          Color fillColor;
55          switch (trafficLight.getTrafficLightColor())
56          {
57              case RED:
58                  fillColor = Color.red;
59                  break;
60  
61              case YELLOW:
62                  fillColor = Color.yellow;
63                  break;
64  
65              case GREEN:
66                  fillColor = Color.green;
67                  break;
68  
69              default:
70                  fillColor = Color.black;
71                  break;
72          }
73  
74          PaintPolygons.paintMultiPolygon(graphics, fillColor, trafficLight.getLocation(), trafficLight.getGeometry(), true);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public final String toString()
80      {
81          return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
82      }
83  
84  }