package org.opentrafficsim.draw.road;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.ImageObserver;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.naming.NamingException;
import org.djutils.draw.line.PolyLine3d;
import org.djutils.draw.point.Point3d;
import org.opentrafficsim.core.geometry.OTSGeometryUtil;
import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
/**
* Sink sensor animation.
*
* Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.
* All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2015-08-12 16:37:45 +0200 (Wed, 12 Aug 2015) $, @version $Revision: 1240 $, by $Author: averbraeck $,
* initial version Jan 30, 2015
* @author Alexander Verbraeck
* @author Peter Knoppers
*/
public class TrafficLightSensorAnimation extends Renderable2D implements Serializable
{
/** */
private static final long serialVersionUID = 20150130L;
/** The traffic light sensor. */
private final TrafficLightSensor sensor;
/** Path of the detector. */
private final PolyLine3d path;
/**
* Construct a SensorAnimation.
* @param sensor TrafficLightSensor; the traffic light sensor that will be animated
* @param simulator SimulatorInterface.TimeDoubleUnit; the simulator to schedule on
* @throws NamingException in case of registration failure of the animation
* @throws RemoteException in case of remote registration failure of the animation
*/
public TrafficLightSensorAnimation(final TrafficLightSensor sensor, final SimulatorInterface.TimeDoubleUnit simulator)
throws NamingException, RemoteException
{
super(sensor, simulator);
this.sensor = sensor;
PolyLine3d coordinates = this.sensor.getPath();
double dx = this.sensor.getLocation().x;
double dy = this.sensor.getLocation().y;
double dz = this.sensor.getLocation().z;
List points = new ArrayList<>(coordinates.size());
for (Iterator iterator = coordinates.getPoints(); iterator.hasNext();)
{
Point3d p = iterator.next();
points.add(new Point3d(p.x - dx, p.y - dy, p.z - dz));
}
this.path = new PolyLine3d(points);
}
/** {@inheritDoc} */
@Override
public final void paint(final Graphics2D graphics, final ImageObserver observer)
{
graphics.setColor(this.sensor.getOccupancy() ? Color.BLUE : Color.BLACK);
graphics.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
graphics.draw(OTSGeometryUtil.toPath(this.path.project()));
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return "SensorAnimation [getSource()=" + this.getSource() + "]";
}
}