ConflictAnimation.java

  1. package org.opentrafficsim.road.network.animation;

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

  9. import javax.naming.NamingException;

  10. import org.djunits.unit.LengthUnit;
  11. import org.djunits.value.vdouble.scalar.Length;
  12. import org.opentrafficsim.core.network.animation.PaintPolygons;
  13. import org.opentrafficsim.road.network.lane.conflict.Conflict;
  14. import org.opentrafficsim.road.network.lane.conflict.ConflictType;

  15. import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;

  16. /**
  17.  * <p>
  18.  * Copyright (c) 2013-2018 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/node/13">OpenTrafficSim License</a>.
  20.  * <p>
  21.  * @version $Revision$, $LastChangedDate$, by $Author$, initial version 7 dec. 2016 <br>
  22.  * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
  23.  * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
  24.  * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
  25.  */
  26. public class ConflictAnimation extends AbstractLineAnimation<Conflict> implements Serializable
  27. {
  28.     // TODO should ConflictAnimation implement the ClonableRenderable2DInterface?

  29.     /** */
  30.     private static final long serialVersionUID = 20161207L;

  31.     /**
  32.      * @param source the conflict 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 ConflictAnimation(final Conflict source, final SimulatorInterface.TimeDoubleUnit simulator)
  38.             throws NamingException, RemoteException
  39.     {
  40.         super(source, simulator, .9, new Length(0.5, LengthUnit.SI));
  41.     }

  42.     /** {@inheritDoc} */
  43.     @Override
  44.     public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
  45.     {
  46.         Conflict conflict = this.getSource();
  47.         Color fillColor;
  48.         switch (conflict.conflictPriority())
  49.         {
  50.             case SPLIT:
  51.                 fillColor = Color.blue;
  52.                 break;

  53.             case PRIORITY:
  54.                 fillColor = Color.green;
  55.                 break;

  56.             case YIELD:
  57.                 fillColor = Color.orange;
  58.                 break;

  59.             default:
  60.                 // STOP, ALL_STOP
  61.                 fillColor = Color.red;
  62.                 break;
  63.         }

  64.         graphics.setColor(fillColor);
  65.         super.paint(graphics, observer);

  66.         BasicStroke stroke;
  67.         float factor = conflict.isPermitted() ? .5f : 1f;
  68.         if (conflict.getConflictType().equals(ConflictType.CROSSING))
  69.         {
  70.             stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
  71.                     new float[] { factor * 1.0f, factor * 2.0f }, 0.0f);
  72.         }
  73.         else
  74.         {
  75.             stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
  76.                     new float[] { factor * 1.0f, factor * 0.95f, factor * 0.1f, factor * 0.95f }, 0.0f);
  77.         }
  78.         graphics.setStroke(stroke);
  79.         AffineTransform saveAT = graphics.getTransform();
  80.         double angle = -getSource().getLocation().getRotZ();
  81.         if (isRotate() && angle != 0.0)
  82.         {
  83.             graphics.rotate(-angle);
  84.         }
  85.         if (conflict.getGeometry() != null)
  86.         {
  87.             PaintPolygons.paintMultiPolygon(graphics, fillColor, conflict.getLocation(), conflict.getGeometry(), false);
  88.         }
  89.         graphics.setTransform(saveAT);

  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public final String toString()
  94.     {
  95.         return "ConflictAnimation [getSource()=" + getSource() + "]";
  96.     }

  97. }