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   import java.util.ArrayList;
9   import java.util.List;
10  
11  import javax.naming.NamingException;
12  
13  import org.opentrafficsim.core.geometry.OTSGeometryException;
14  import org.opentrafficsim.core.geometry.OTSLine3D;
15  import org.opentrafficsim.core.geometry.OTSPoint3D;
16  import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
17  
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  
21  /**
22   * sink sensor animation.
23   * <p>
24   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
25   * All rights reserved. <br>
26   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * <p>
28   * $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
29   * initial version Jan 30, 2015 <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 TrafficLightSensorAnimation extends Renderable2D<TrafficLightSensor> implements Serializable
34  {
35      /** */
36      private static final long serialVersionUID = 20150130L;
37  
38      /** The traffic light sensor. */
39      private final TrafficLightSensor sensor;
40  
41      /** Path of the detector. */
42      private final OTSLine3D path;
43  
44      /**
45       * Construct a SensorAnimation.
46       * @param sensor TrafficLightSensor; the traffic light sensor that will be animated
47       * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator to schedule on
48       * @throws NamingException in case of registration failure of the animation
49       * @throws RemoteException in case of remote registration failure of the animation
50       * @throws OTSGeometryException when the geometry is bad
51       */
52      public TrafficLightSensorAnimation(final TrafficLightSensor sensor, final SimulatorInterface.TimeDoubleUnit simulator)
53              throws NamingException, RemoteException, OTSGeometryException
54      {
55          super(sensor, simulator);
56          this.sensor = sensor;
57          OTSLine3D coordinates = this.sensor.getPath();
58          double dx = this.sensor.getLocation().x;
59          double dy = this.sensor.getLocation().y;
60          double dz = this.sensor.getLocation().z;
61          List<OTSPoint3D> points = new ArrayList<>(coordinates.size());
62          for (OTSPoint3D p : coordinates.getPoints())
63          {
64              points.add(new OTSPoint3D(p.x - dx, p.y - dy, p.z - dz));
65          }
66          this.path = new OTSLine3D(points);
67      }
68  
69      /** {@inheritDoc} */
70      @Override
71      public final void paint(final Graphics2D graphics, final ImageObserver observer)
72      {
73          graphics.setColor(this.sensor.getOccupancy() ? Color.BLUE : Color.BLACK);
74          OTSPoint3D prevPoint = null;
75          for (OTSPoint3D p : this.path.getPoints())
76          {
77              if (null != prevPoint)
78              {
79                  // System.out.println("Drawing sensor line from " + prevPoint + " to " + p);
80                  graphics.drawLine((int) prevPoint.x, (int) prevPoint.y, (int) p.x, (int) p.y);
81              }
82              prevPoint = p;
83          }
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      public final String toString()
89      {
90          return "SensorAnimation [getSource()=" + this.getSource() + "]";
91      }
92  
93  }