DefaultCarAnimation.java

  1. package org.opentrafficsim.road.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.dsol.OTSSimulatorInterface;
  12. import org.opentrafficsim.core.gtu.animation.GTUColorer;
  13. import org.opentrafficsim.core.gtu.animation.IDGTUColorer;
  14. import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;

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

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

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

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

  71.     /** {@inheritDoc} */
  72.     @Override
  73.     public final void paint(final Graphics2D graphics, final ImageObserver observer)
  74.     {
  75.         final LaneBasedIndividualGTU car = (LaneBasedIndividualGTU) getSource();

  76.         if (car.isDestroyed())
  77.         {
  78.             try
  79.             {
  80.                 destroy();
  81.             }
  82.             catch (Exception e)
  83.             {
  84.                 System.err.println("GTU: " + car.toString());
  85.                 e.printStackTrace();
  86.             }
  87.         }

  88.         final double length = car.getLength().getSI();
  89.         final double l2 = length / 2;
  90.         final double width = car.getWidth().getSI();
  91.         final double w2 = width / 2;
  92.         final double w4 = width / 4;
  93.         graphics.setColor(this.gtuColorer.getColor(car));
  94.         BasicStroke saveStroke = (BasicStroke) graphics.getStroke();
  95.         graphics.setStroke(new BasicStroke(0));
  96.         Rectangle2D rectangle = new Rectangle2D.Double(-l2, -w2, length, width);
  97.         graphics.draw(rectangle);
  98.         graphics.fill(rectangle);
  99.         // Draw a white disk at the front to indicate which side faces forward
  100.         graphics.setColor(Color.WHITE);
  101.         Ellipse2D.Double frontIndicator = new Ellipse2D.Double(l2 - w2 - w4, -w4, w2, w2);
  102.         graphics.draw(frontIndicator);
  103.         graphics.fill(frontIndicator);

  104.         graphics.setColor(Color.YELLOW);
  105.         if (car.getTurnIndicatorStatus() != null && car.getTurnIndicatorStatus().isLeftOrBoth())
  106.         {
  107.             Rectangle2D.Double leftIndicator = new Rectangle2D.Double(l2 - w4, -w2, w4, w4);
  108.             graphics.fill(leftIndicator);
  109.         }
  110.        
  111.         if (car.getTurnIndicatorStatus() != null && car.getTurnIndicatorStatus().isRightOrBoth())
  112.         {
  113.             Rectangle2D.Double rightIndicator = new Rectangle2D.Double(l2 - w4, w2 - w4, w4, w4);
  114.             graphics.fill(rightIndicator);            
  115.         }
  116.        
  117.         graphics.setColor(Color.RED);
  118.         if (car.getAcceleration().si < 0.0)
  119.         {
  120.             Rectangle2D.Double leftBrake = new Rectangle2D.Double(-l2, w2 - w4, w4, w4);
  121.             Rectangle2D.Double rightBrake = new Rectangle2D.Double(-l2, -w2, w4, w4);
  122.             graphics.setColor(Color.RED);
  123.             graphics.fill(leftBrake);
  124.             graphics.fill(rightBrake);
  125.         }
  126.         graphics.setStroke(saveStroke);
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     public final String toString()
  131.     {
  132.         return this.getSource().toString();
  133.     }

  134. }