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