ConflictAnimation.java
package org.opentrafficsim.road.network.animation;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.ImageObserver;
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.naming.NamingException;
import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
import org.opentrafficsim.core.network.animation.PaintPolygons;
import org.opentrafficsim.road.network.lane.conflict.Conflict;
import org.opentrafficsim.road.network.lane.conflict.ConflictType;
import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
/**
* <p>
* Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
* BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
* <p>
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 7 dec. 2016 <br>
* @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
* @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
* @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
*/
public class ConflictAnimation extends Renderable2D implements Serializable
{
// TODO should ConflictAnimation implement the ClonableRenderable2DInterface?
/** */
private static final long serialVersionUID = 20161207L;
/** The half width left and right of the center line that is used to draw the block. */
private final double halfWidth;
/**
* @param source the conflict to draw
* @param simulator the simulator to schedule on
* @throws NamingException in case of registration failure of the animation
* @throws RemoteException on communication failure
*/
public ConflictAnimation(final Conflict source, final OTSSimulatorInterface simulator)
throws NamingException, RemoteException
{
super(source, simulator);
this.halfWidth = 0.45 * source.getLane().getWidth(source.getLongitudinalPosition()).getSI();
}
/** {@inheritDoc} */
@Override
public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
{
Conflict conflict = (Conflict) this.getSource();
Color fillColor;
switch (conflict.getConflictRule())
{
case SPLIT:
fillColor = Color.blue;
break;
case PRIORITY:
fillColor = Color.green;
break;
case GIVE_WAY:
fillColor = Color.orange;
break;
default:
// STOP, ALL_STOP
fillColor = Color.red;
break;
}
graphics.setColor(fillColor);
Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
graphics.fill(rectangle);
BasicStroke stroke;
float factor = conflict.isPermitted() ? .5f : 1f;
if (conflict.getConflictType().equals(ConflictType.CROSSING))
{
stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { factor * 1.0f, factor * 2.0f }, 0.0f);
}
else
{
stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
new float[] { factor * 1.0f, factor * 0.95f, factor * 0.1f, factor * 0.95f }, 0.0f);
}
graphics.setStroke(stroke);
AffineTransform saveAT = graphics.getTransform();
double angle = -getSource().getLocation().getRotZ();
if (isRotate() && angle != 0.0)
{
graphics.rotate(-angle);
}
if (conflict.getGeometry() != null)
{
PaintPolygons.paintMultiPolygon(graphics, fillColor, conflict.getLocation(), conflict.getGeometry(), false);
}
graphics.setTransform(saveAT);
}
/** {@inheritDoc} */
@Override
public final String toString()
{
return "ConflictAnimation [getSource()=" + getSource() + "]";
}
}