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