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