DefaultCarAnimation.java

  1. package org.opentrafficsim.core.gtu.animation;

  2. import java.awt.BasicStroke;
  3. import java.awt.Color;
  4. import java.awt.Graphics2D;
  5. import java.awt.geom.Ellipse2D;
  6. import java.awt.geom.Rectangle2D;
  7. import java.awt.image.ImageObserver;
  8. import java.rmi.RemoteException;

  9. import javax.naming.NamingException;

  10. import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;

  11. import org.opentrafficsim.core.car.LaneBasedIndividualCar;
  12. import org.opentrafficsim.core.dsol.OTSSimulatorInterface;

  13. /**
  14.  * Draw a car.
  15.  * <p>
  16.  * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
  17.  * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
  18.  * <p>
  19.  * @version $Revision: 1291 $, $LastChangedDate: 2015-08-23 00:48:01 +0200 (Sun, 23 Aug 2015) $, by $Author: averbraeck $,
  20.  *          initial version 29 dec. 2014 <br>
  21.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  22.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  23.  */
  24. public class DefaultCarAnimation extends Renderable2D
  25. {
  26.     /** The GTUColorer that determines the fill color for the car. */
  27.     private GTUColorer gtuColorer;

  28.     /**
  29.      * Construct the DefaultCarAnimation for a LaneBasedIndividualCar.
  30.      * @param source the Car to draw
  31.      * @param simulator the simulator to schedule on
  32.      * @throws NamingException in case of registration failure of the animation
  33.      * @throws RemoteException in case of remote registration failure of the animation
  34.      */
  35.     public DefaultCarAnimation(final LaneBasedIndividualCar source, final OTSSimulatorInterface simulator)
  36.         throws NamingException, RemoteException
  37.     {
  38.         this(source, simulator, null);
  39.     }

  40.     /**
  41.      * Construct the DefaultCarAnimation for a LaneBasedIndividualCar.
  42.      * @param source the Car to draw
  43.      * @param simulator the simulator to schedule on
  44.      * @param gtuColorer GTUColorer; the GTUColorer that determines what fill color to use
  45.      * @throws NamingException in case of registration failure of the animation
  46.      * @throws RemoteException in case of remote registration failure of the animation
  47.      */
  48.     public DefaultCarAnimation(final LaneBasedIndividualCar source, final OTSSimulatorInterface simulator,
  49.         final GTUColorer gtuColorer) throws NamingException, RemoteException
  50.     {
  51.         super(source, simulator);
  52.         if (null == gtuColorer)
  53.         {
  54.             this.gtuColorer = new IDGTUColorer();
  55.         }
  56.         else
  57.         {
  58.             this.gtuColorer = gtuColorer;
  59.         }
  60.     }

  61.     /**
  62.      * Replace the GTUColorer.
  63.      * @param newGTUColorer GTUColorer; the GTUColorer to use from now on
  64.      */
  65.     public final void setGTUColorer(final GTUColorer newGTUColorer)
  66.     {
  67.         this.gtuColorer = newGTUColorer;
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
  72.     {
  73.         final LaneBasedIndividualCar car = (LaneBasedIndividualCar) getSource();
  74.         final double length = car.getLength().getSI();
  75.         final double width = car.getWidth().getSI();
  76.         graphics.setColor(this.gtuColorer.getColor(car));
  77.         BasicStroke saveStroke = (BasicStroke) graphics.getStroke();
  78.         graphics.setStroke(new BasicStroke(0));
  79.         Rectangle2D rectangle = new Rectangle2D.Double(-length / 2, -width / 2, length, width);
  80.         graphics.draw(rectangle);
  81.         graphics.fill(rectangle);
  82.         // Draw a 1m diameter white disk about 1m before the front to indicate which side faces forward
  83.         graphics.setColor(Color.WHITE);
  84.         Ellipse2D.Double frontIndicator = new Ellipse2D.Double(length / 2 - 1.5d, -0.5d, 1d, 1d);
  85.         graphics.draw(frontIndicator);
  86.         graphics.fill(frontIndicator);
  87.         graphics.setStroke(saveStroke);
  88.     }

  89.     /** {@inheritDoc} */
  90.     @Override
  91.     public final String toString()
  92.     {
  93.         return "DefaultCarAnimation [getSource()=" + this.getSource() + "]";
  94.     }

  95. }