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