View Javadoc
1   package org.opentrafficsim.road.network.animation;
2   
3   import java.awt.Color;
4   import java.awt.Font;
5   import java.awt.Graphics2D;
6   import java.awt.geom.Ellipse2D;
7   import java.awt.geom.Rectangle2D;
8   import java.awt.image.ImageObserver;
9   import java.io.Serializable;
10  import java.rmi.RemoteException;
11  
12  import javax.naming.NamingException;
13  
14  import org.djunits.unit.SpeedUnit;
15  import org.opentrafficsim.road.network.lane.object.SpeedSign;
16  
17  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
18  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
24   * <p>
25   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2017 <br>
26   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
27   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
28   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
29   */
30  
31  public class SpeedSignAnimation extends Renderable2D<SpeedSign> implements Serializable
32  {
33  
34      /** */
35      private static final long serialVersionUID = 20170420L;
36  
37      /** Radius in meters. */
38      private static final double RADIUS = 1.6;
39  
40      /** Radius in meters. */
41      private static final double EDGE = 1.3;
42  
43      /**
44       * @param source speed sign
45       * @param simulator simulator
46       * @throws NamingException ne
47       * @throws RemoteException on communication failure
48       */
49      public SpeedSignAnimation(final SpeedSign source, final SimulatorInterface.TimeDoubleUnit simulator)
50              throws NamingException, RemoteException
51      {
52          super(source, simulator);
53          setRotate(false);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public final void paint(final Graphics2D g, final ImageObserver arg1) throws RemoteException
59      {
60          Ellipse2D ellipse = new Ellipse2D.Double(-RADIUS, -RADIUS, 2 * RADIUS, 2 * RADIUS);
61          g.setColor(Color.RED);
62          g.fill(ellipse);
63          ellipse = new Ellipse2D.Double(-EDGE, -EDGE, 2 * EDGE, 2 * EDGE);
64          g.setColor(Color.WHITE);
65          g.fill(ellipse);
66          g.setColor(Color.BLACK);
67          int speed = (int) getSource().getSpeed().getInUnit(SpeedUnit.KM_PER_HOUR);
68          if (speed < 100)
69          {
70              g.setFont(new Font("Arial", 0, -1).deriveFont(2.0f));
71          }
72          else
73          {
74              g.setFont(new Font("Arial narrow", 0, -1).deriveFont(1.85f));
75          }
76          String str = Integer.toString(speed);
77          Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(str, g);
78          g.drawString(str, (float) -stringBounds.getCenterX(), (float) -stringBounds.getCenterY());
79      }
80  
81      /** {@inheritDoc} */
82      @Override
83      public final String toString()
84      {
85          return "SpeedSignAnimation";
86      }
87  
88  }