View Javadoc
1   package org.opentrafficsim.draw.road;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Graphics2D;
6   import java.awt.geom.Path2D;
7   import java.awt.image.ImageObserver;
8   import java.io.Serializable;
9   import java.rmi.RemoteException;
10  
11  import javax.naming.NamingException;
12  
13  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
14  import org.opentrafficsim.core.geometry.OtsGeometryException;
15  import org.opentrafficsim.core.geometry.OtsLine3d;
16  import org.opentrafficsim.road.network.lane.object.detector.TrafficLightDetector;
17  
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  
20  /**
21   * Traffic light detector animation.
22   * <p>
23   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
24   * All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
29   */
30  public class TrafficLightDetectorAnimation extends Renderable2D<TrafficLightDetector> implements Serializable
31  {
32      /** */
33      private static final long serialVersionUID = 20150130L;
34  
35      /** The traffic light detector. */
36      private final TrafficLightDetector detector;
37  
38      /** Path of the detector. */
39      private final Path2D.Float polygon;
40  
41      /**
42       * Construct a TrafficLightDetectorAnimation.
43       * @param detector TrafficLightSensor; the traffic light detector that will be animated
44       * @param simulator OtsSimulatorInterface; the simulator to schedule on
45       * @throws NamingException in case of registration failure of the animation
46       * @throws RemoteException in case of remote registration failure of the animation
47       * @throws OtsGeometryException when the geometry is bad
48       */
49      public TrafficLightDetectorAnimation(final TrafficLightDetector detector, final OtsSimulatorInterface simulator)
50              throws NamingException, RemoteException, OtsGeometryException
51      {
52          super(detector, simulator);
53          this.detector = detector;
54          OtsLine3d coordinates = this.detector.getGeometry();
55          this.polygon = new Path2D.Float();
56          this.polygon.moveTo(coordinates.get(0).x, coordinates.get(0).y);
57          for (int i = 1; i < coordinates.size(); i++)
58          {
59              this.polygon.lineTo(coordinates.get(i).x, coordinates.get(i).y);
60          }
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public final void paint(final Graphics2D graphics, final ImageObserver observer)
66      {
67          graphics.setColor(this.detector.getOccupancy() ? Color.BLUE : Color.BLACK);
68          graphics.setStroke(new BasicStroke(0.2f));
69          graphics.draw(this.polygon);
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public final String toString()
75      {
76          return "TrafficLightDetectorAnimation [getSource()=" + this.getSource() + "]";
77      }
78  
79  }