package org.opentrafficsim.draw.road; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.image.ImageObserver; import java.io.Serializable; import java.rmi.RemoteException; import javax.naming.NamingException; import org.djunits.unit.SpeedUnit; import org.opentrafficsim.road.network.lane.object.SpeedSign; import nl.tudelft.simulation.dsol.animation.D2.Renderable2D; import nl.tudelft.simulation.dsol.simulators.SimulatorInterface; /** *

* Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License. *

* @version $Revision$, $LastChangedDate$, by $Author$, initial version 20 apr. 2017
* @author Alexander Verbraeck * @author Peter Knoppers * @author Wouter Schakel */ public class SpeedSignAnimation extends Renderable2D implements Serializable { /** */ private static final long serialVersionUID = 20170420L; /** Radius in meters. */ private static final double RADIUS = 1.6; /** Radius in meters. */ private static final double EDGE = 1.3; /** * @param source SpeedSign; speed sign * @param simulator SimulatorInterface.TimeDoubleUnit; simulator * @throws NamingException ne * @throws RemoteException on communication failure */ public SpeedSignAnimation(final SpeedSign source, final SimulatorInterface.TimeDoubleUnit simulator) throws NamingException, RemoteException { super(source, simulator); setRotate(false); } /** {@inheritDoc} */ @Override public final void paint(final Graphics2D g, final ImageObserver arg1) throws RemoteException { Ellipse2D ellipse = new Ellipse2D.Double(-RADIUS, -RADIUS, 2 * RADIUS, 2 * RADIUS); g.setColor(Color.RED); g.fill(ellipse); ellipse = new Ellipse2D.Double(-EDGE, -EDGE, 2 * EDGE, 2 * EDGE); g.setColor(Color.WHITE); g.fill(ellipse); g.setColor(Color.BLACK); int speed = (int) getSource().getSpeed().getInUnit(SpeedUnit.KM_PER_HOUR); if (speed < 100) { g.setFont(new Font("Arial", 0, -1).deriveFont(2.0f)); } else { g.setFont(new Font("Arial narrow", 0, -1).deriveFont(1.85f)); } String str = Integer.toString(speed); Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(str, g); g.drawString(str, (float) -stringBounds.getCenterX(), (float) -stringBounds.getCenterY()); } /** {@inheritDoc} */ @Override public final String toString() { return "SpeedSignAnimation"; } }