GtuGeneratorQueueAnimation.java

  1. package org.opentrafficsim.draw.gtu;

  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.ImageObserver;
  6. import java.rmi.RemoteException;
  7. import java.util.Map;

  8. import javax.naming.NamingException;

  9. import org.opentrafficsim.draw.core.TextAlignment;
  10. import org.opentrafficsim.draw.core.TextAnimation;
  11. import org.opentrafficsim.road.gtu.generator.GtuGeneratorQueue;

  12. import nl.tudelft.simulation.dsol.animation.Locatable;
  13. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
  14. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface.TimeDoubleUnit;
  15. import nl.tudelft.simulation.language.d3.DirectedPoint;

  16. /**
  17.  * Animator that displays generation queues as numbers.
  18.  * <p>
  19.  * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  20.  * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
  21.  * <p>
  22.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 dec. 2017 <br>
  23.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  24.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  25.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  26.  */
  27. public class GtuGeneratorQueueAnimation extends TextAnimation
  28. {

  29.     /** */
  30.     private static final long serialVersionUID = 20181018L;

  31.     /** Default font. */
  32.     private static final Font FONT = new Font("SansSerif", Font.PLAIN, 4);

  33.     /**
  34.      * Constructor.
  35.      * @param source GTUGenerator; generator
  36.      * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
  37.      * @throws NamingException when animation context cannot be created or retrieved
  38.      * @throws RemoteException when remote context cannot be found
  39.      */
  40.     public GtuGeneratorQueueAnimation(final GtuGeneratorQueue source, final SimulatorInterface.TimeDoubleUnit simulator)
  41.             throws NamingException, RemoteException
  42.     {
  43.         super(source, "", 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, simulator, TextAnimation.RENDERWHEN1);
  44.     }

  45.     /** {@inheritDoc} */
  46.     @Override
  47.     public void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
  48.     {
  49.         graphics.setColor(Color.BLACK);
  50.         graphics.setFont(FONT);
  51.         DirectedPoint p = getSource().getLocation();
  52.         Map<DirectedPoint, Integer> map = ((GtuGeneratorQueue) getSource()).getQueueLengths();
  53.         for (DirectedPoint lanePosition : map.keySet())
  54.         {
  55.             setText(map.get(lanePosition).toString());
  56.             setXY((float) (lanePosition.x - p.x), (float) (lanePosition.y - p.y));
  57.             super.paint(graphics, observer);
  58.         }
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public TextAnimation clone(final Locatable newSource, final TimeDoubleUnit newSimulator)
  63.             throws RemoteException, NamingException
  64.     {
  65.         return new GtuGeneratorQueueAnimation((GtuGeneratorQueue) newSource, newSimulator);
  66.     }

  67. }