package org.opentrafficsim.draw.road;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.image.ImageObserver;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import org.djunits.value.vdouble.scalar.Length;
import org.opentrafficsim.core.geometry.OTSGeometryUtil;
import org.opentrafficsim.road.network.lane.object.LaneBasedObject;
import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
/**
* Abstract class for objects that draw a line perpendicular on the lane.
*
* 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.
*
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
* @param the LaneBasedObject class of the source that indicates the location of the Renderable on the screen
*/
public abstract class AbstractLineAnimation extends Renderable2D
{
/** */
private static final long serialVersionUID = 20170125L;
/** Half length, for placement of coupled text labels. */
private final double halfWidth;
/** The shape (should be constant, as LaneBasedObjects don't change shape). */
private final Shape shape;
/**
* Construct the line animation.
* @param source T; source
* @param simulator SimulatorInterface.TimeDoubleUnit; the simulator to schedule on
* @param fractionalWidth double; length of the line, as fraction of the lane width
* @param width Length; line width
* @throws NamingException in case of registration failure of the animation
* @throws RemoteException in case of remote registration failure of the animation
*/
public AbstractLineAnimation(final T source, final SimulatorInterface.TimeDoubleUnit simulator,
final double fractionalWidth, final Length width) throws NamingException, RemoteException
{
super(source, simulator);
this.halfWidth = .5 * fractionalWidth * source.getLane().getWidth(0.0).getSI();
this.shape = OTSGeometryUtil.toPath(source.getGeometry().project());
}
/**
* Returns half the length.
* @return half the length
*/
public final double getHalfLength()
{
return this.halfWidth;
}
/** {@inheritDoc} */
@Override
public void paint(final Graphics2D graphics, final ImageObserver observer)
{
// graphics.fill(this.rectangle);
graphics.setStroke(new BasicStroke(0.25f));
graphics.draw(this.shape);
graphics.fill(this.shape);
}
}