View Javadoc
1   package org.opentrafficsim.draw.gtu;
2   
3   import java.awt.Color;
4   import java.awt.Font;
5   import java.awt.Graphics2D;
6   import java.awt.image.ImageObserver;
7   import java.rmi.RemoteException;
8   import java.util.Map;
9   
10  import javax.naming.NamingException;
11  
12  import org.djutils.draw.point.OrientedPoint3d;
13  import org.djutils.logger.CategoryLogger;
14  import org.opentrafficsim.core.geometry.DirectedPoint;
15  import org.opentrafficsim.draw.core.TextAlignment;
16  import org.opentrafficsim.draw.core.TextAnimation;
17  import org.opentrafficsim.road.gtu.generator.GtuGeneratorQueue;
18  
19  import nl.tudelft.simulation.dsol.animation.Locatable;
20  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
21  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface.TimeDoubleUnit;
22  
23  /**
24   * Animator that displays generation queues as numbers.
25   * <p>
26   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
27   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
28   * <p>
29   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 28 dec. 2017 <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   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
33   */
34  public class GtuGeneratorQueueAnimation extends TextAnimation
35  {
36  
37      /** */
38      private static final long serialVersionUID = 20181018L;
39  
40      /** Default font. */
41      private static final Font FONT = new Font("SansSerif", Font.PLAIN, 4);
42  
43      /**
44       * Constructor.
45       * @param source GTUGenerator; generator
46       * @param simulator SimulatorInterface.TimeDoubleUnit; simulator
47       * @throws NamingException when animation context cannot be created or retrieved
48       * @throws RemoteException when remote context cannot be found
49       */
50      public GtuGeneratorQueueAnimation(final GtuGeneratorQueue source, final SimulatorInterface.TimeDoubleUnit simulator)
51              throws NamingException, RemoteException
52      {
53          super(source, "", 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, simulator, TextAnimation.RENDERWHEN1);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public void paint(final Graphics2D graphics, final ImageObserver observer)
59      {
60          try
61          {
62              graphics.setColor(Color.BLACK);
63              graphics.setFont(FONT);
64              OrientedPoint3d p = (OrientedPoint3d) getSource().getLocation();
65              Map<DirectedPoint, Integer> map = ((GtuGeneratorQueue) getSource()).getQueueLengths();
66              for (DirectedPoint lanePosition : map.keySet())
67              {
68                  setText(map.get(lanePosition).toString());
69                  setXY((float) (lanePosition.x - p.x), (float) (lanePosition.y - p.y));
70                  super.paint(graphics, observer);
71              }
72          }
73          catch (RemoteException exception)
74          {
75              CategoryLogger.always().warn(exception);
76          }
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public TextAnimation clone(final Locatable newSource, final TimeDoubleUnit newSimulator)
82              throws RemoteException, NamingException
83      {
84          return new GtuGeneratorQueueAnimation((GtuGeneratorQueue) newSource, newSimulator);
85      }
86  
87  }