View Javadoc
1   package org.opentrafficsim.road.network.animation;
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.animation.ClonableRenderable2DInterface;
13  import org.opentrafficsim.core.animation.TextAlignment;
14  import org.opentrafficsim.core.animation.TextAnimation;
15  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
16  import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
17  
18  import nl.tudelft.simulation.dsol.animation.Locatable;
19  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
20  
21  /**
22   * Draw a traffic light on the road at th place where the cars are expected to stop.
23   * <p>
24   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * <p>
27   * @version $Revision: 1401 $, $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, by $Author: averbraeck $,
28   *          initial version 29 dec. 2014 <br>
29   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
31   */
32  public class TrafficLightAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
33  {
34      /** */
35      private static final long serialVersionUID = 20160000L;
36  
37      /** The half width left and right of the center line that is used to draw the block. */
38      private final double halfWidth;
39  
40      /** the Text object to destroy when the animation is destroyed. */
41      private Text text;
42  
43      /**
44       * Construct the DefaultCarAnimation for a LaneBlock (road block).
45       * @param trafficLight the CSEBlock to draw
46       * @param simulator the simulator to schedule on
47       * @throws NamingException in case of registration failure of the animation
48       * @throws RemoteException on communication failure
49       */
50      public TrafficLightAnimation(final TrafficLight trafficLight, final OTSSimulatorInterface simulator)
51              throws NamingException, RemoteException
52      {
53          super(trafficLight, simulator);
54          this.halfWidth = 0.45 * trafficLight.getLane().getWidth(trafficLight.getLongitudinalPosition()).getSI();
55  
56          new Text(trafficLight,
57                  trafficLight.getLane().getParentLink().getId() + "." + trafficLight.getLane().getId() + trafficLight.getId(),
58                  0.0f, (float) this.halfWidth + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
66      {
67          TrafficLight trafficLight = (TrafficLight) this.getSource();
68          Color fillColor;
69          switch (trafficLight.getTrafficLightColor())
70          {
71              case RED:
72                  fillColor = Color.red;
73                  break;
74  
75              case YELLOW:
76                  fillColor = Color.yellow;
77                  break;
78  
79              case GREEN:
80                  fillColor = Color.green;
81                  break;
82  
83              default:
84                  fillColor = Color.black;
85                  break;
86          }
87  
88          // PaintPolygons.paintMultiPolygon(graphics, fillColor, trafficLight.getLocation(), trafficLight.getGeometry(), false);
89          graphics.setColor(fillColor);
90          Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
91          graphics.fill(rectangle);
92      }
93  
94      /** {@inheritDoc} */
95      @Override
96      public final void destroy() throws NamingException
97      {
98          super.destroy();
99          this.text.destroy();
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     @SuppressWarnings("checkstyle:designforextension")
105     public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
106             throws NamingException, RemoteException
107     {
108         // the constructor also constructs the corresponding Text object
109         return new TrafficLightAnimation((TrafficLight) newSource, newSimulator);
110     }
111 
112     /** {@inheritDoc} */
113     @Override
114     public final String toString()
115     {
116         return "TrafficLightAnimation [getSource()=" + this.getSource() + "]";
117     }
118 
119     /**
120      * Text animation for the TrafficLight. Separate class to be able to turn it on and off...
121      * <p>
122      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
123      * <br>
124      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
125      * </p>
126      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
127      * initial version Dec 11, 2016 <br>
128      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
129      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
130      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
131      */
132     public class Text extends TextAnimation
133     {
134         /** */
135         private static final long serialVersionUID = 20161211L;
136 
137         /**
138          * @param source the object for which the text is displayed
139          * @param text the text to display
140          * @param dx the horizontal movement of the text, in meters
141          * @param dy the vertical movement of the text, in meters
142          * @param textPlacement where to place the text
143          * @param color the color of the text
144          * @param simulator the simulator
145          * @throws NamingException when animation context cannot be created or retrieved
146          * @throws RemoteException - when remote context cannot be found
147          */
148         public Text(final Locatable source, final String text, final float dx, final float dy,
149                 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
150                 throws RemoteException, NamingException
151         {
152             super(source, text, dx, dy, textPlacement, color, simulator);
153         }
154 
155         /** {@inheritDoc} */
156         @Override
157         @SuppressWarnings("checkstyle:designforextension")
158         public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
159                 throws RemoteException, NamingException
160         {
161             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
162         }
163     }
164 
165 }