View Javadoc
1   package org.opentrafficsim.draw.road;
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.core.dsol.OtsSimulatorInterface;
16  import org.opentrafficsim.road.network.lane.object.SpeedSign;
17  
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
27   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
28   */
29  
30  public class SpeedSignAnimation extends Renderable2D<SpeedSign> implements Serializable
31  {
32  
33      /** */
34      private static final long serialVersionUID = 20170420L;
35  
36      /** Radius in meters. */
37      private static final double RADIUS = 1.6;
38  
39      /** Radius in meters. */
40      private static final double EDGE = 1.3;
41  
42      /**
43       * @param source SpeedSign; speed sign
44       * @param simulator OtsSimulatorInterface; simulator
45       * @throws NamingException ne
46       * @throws RemoteException on communication failure
47       */
48      public SpeedSignAnimation(final SpeedSign source, final OtsSimulatorInterface simulator)
49              throws NamingException, RemoteException
50      {
51          super(source, simulator);
52          setRotate(false);
53      }
54  
55      /** {@inheritDoc} */
56      @Override
57      public final void paint(final Graphics2D g, final ImageObserver arg1)
58      {
59          Ellipse2D ellipse = new Ellipse2D.Double(-RADIUS, -RADIUS, 2 * RADIUS, 2 * RADIUS);
60          g.setColor(Color.RED);
61          g.fill(ellipse);
62          ellipse = new Ellipse2D.Double(-EDGE, -EDGE, 2 * EDGE, 2 * EDGE);
63          g.setColor(Color.WHITE);
64          g.fill(ellipse);
65          g.setColor(Color.BLACK);
66          int speed = (int) getSource().getSpeed().getInUnit(SpeedUnit.KM_PER_HOUR);
67          if (speed < 100)
68          {
69              g.setFont(new Font("Arial", 0, -1).deriveFont(2.0f));
70          }
71          else
72          {
73              g.setFont(new Font("Arial narrow", 0, -1).deriveFont(1.85f));
74          }
75          String str = Integer.toString(speed);
76          Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(str, g);
77          g.drawString(str, (float) -stringBounds.getCenterX(), (float) -stringBounds.getCenterY());
78      }
79  
80      /** {@inheritDoc} */
81      @Override
82      public final String toString()
83      {
84          return "SpeedSignAnimation";
85      }
86  
87  }