AbstractLineAnimation.java

  1. package org.opentrafficsim.draw.road;

  2. import java.awt.Graphics2D;
  3. import java.awt.geom.Rectangle2D;
  4. import java.awt.image.ImageObserver;
  5. import java.rmi.RemoteException;

  6. import javax.naming.NamingException;

  7. import org.djunits.value.vdouble.scalar.Length;
  8. import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
  9. import org.opentrafficsim.road.network.lane.object.LaneBasedObject;

  10. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;

  11. /**
  12.  * Abstract class for objects that draw a line perpendicular on the lane.
  13.  * <p>
  14.  * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  15.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  16.  * <p>
  17.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 25 jan. 2017 <br>
  18.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  19.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  20.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  21.  * @param <T> the LaneBasedObject class of the source that indicates the location of the Renderable on the screen
  22.  */
  23. public abstract class AbstractLineAnimation<T extends LaneBasedObject> extends Renderable2D<T>
  24. {

  25.     /** Rectangle to color. */
  26.     private final Rectangle2D rectangle;

  27.     /** Half length, for placement of coupled text labels. */
  28.     private final double halfLength;

  29.     /**
  30.      * Construct the line animation.
  31.      * @param source T; source
  32.      * @param simulator OTSSimulatorInterface; the simulator to schedule on
  33.      * @param length double; length of the line, as fraction of the lane width
  34.      * @param width Length; line width
  35.      * @throws NamingException in case of registration failure of the animation
  36.      * @throws RemoteException in case of remote registration failure of the animation
  37.      */
  38.     public AbstractLineAnimation(final T source, final OTSSimulatorInterface simulator, final double length,
  39.             final Length width) throws NamingException, RemoteException
  40.     {
  41.         super(source, simulator);
  42.         this.halfLength = .5 * length * source.getLane().getWidth(0.0).getSI();
  43.         this.rectangle = new Rectangle2D.Double(-.5 * width.si, -this.halfLength, width.si, 2 * this.halfLength);
  44.     }

  45.     /**
  46.      * Returns half the length.
  47.      * @return half the length
  48.      */
  49.     public final double getHalfLength()
  50.     {
  51.         return this.halfLength;
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     @SuppressWarnings("checkstyle:designforextension")
  56.     public void paint(final Graphics2D graphics, final ImageObserver observer)
  57.     {
  58.         graphics.fill(this.rectangle);
  59.     }

  60. }