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