View Javadoc
1   package org.opentrafficsim.road.network.animation;
2   
3   import java.awt.BasicStroke;
4   import java.awt.Color;
5   import java.awt.Graphics2D;
6   import java.awt.geom.AffineTransform;
7   import java.awt.image.ImageObserver;
8   import java.io.Serializable;
9   import java.rmi.RemoteException;
10  
11  import javax.naming.NamingException;
12  
13  import org.djunits.unit.LengthUnit;
14  import org.djunits.value.vdouble.scalar.Length;
15  import org.opentrafficsim.core.network.animation.PaintPolygons;
16  import org.opentrafficsim.road.network.lane.conflict.Conflict;
17  import org.opentrafficsim.road.network.lane.conflict.ConflictType;
18  
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  
21  /**
22   * <p>
23   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 7 dec. 2016 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   */
31  public class ConflictAnimation extends AbstractLineAnimation<Conflict> implements Serializable
32  {
33      // TODO should ConflictAnimation implement the ClonableRenderable2DInterface?
34  
35      /** */
36      private static final long serialVersionUID = 20161207L;
37  
38      /**
39       * @param source the conflict to draw
40       * @param simulator the simulator to schedule on
41       * @throws NamingException in case of registration failure of the animation
42       * @throws RemoteException on communication failure
43       */
44      public ConflictAnimation(final Conflict source, final SimulatorInterface.TimeDoubleUnit simulator)
45              throws NamingException, RemoteException
46      {
47          super(source, simulator, .9, new Length(0.5, LengthUnit.SI));
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
53      {
54          Conflict conflict = this.getSource();
55          Color fillColor;
56          switch (conflict.conflictPriority())
57          {
58              case SPLIT:
59                  fillColor = Color.blue;
60                  break;
61  
62              case PRIORITY:
63                  fillColor = Color.green;
64                  break;
65  
66              case YIELD:
67                  fillColor = Color.orange;
68                  break;
69  
70              default:
71                  // STOP, ALL_STOP
72                  fillColor = Color.red;
73                  break;
74          }
75  
76          graphics.setColor(fillColor);
77          super.paint(graphics, observer);
78  
79          BasicStroke stroke;
80          float factor = conflict.isPermitted() ? .5f : 1f;
81          if (conflict.getConflictType().equals(ConflictType.CROSSING))
82          {
83              stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
84                      new float[] { factor * 1.0f, factor * 2.0f }, 0.0f);
85          }
86          else
87          {
88              stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
89                      new float[] { factor * 1.0f, factor * 0.95f, factor * 0.1f, factor * 0.95f }, 0.0f);
90          }
91          graphics.setStroke(stroke);
92          AffineTransform saveAT = graphics.getTransform();
93          double angle = -getSource().getLocation().getRotZ();
94          if (isRotate() && angle != 0.0)
95          {
96              graphics.rotate(-angle);
97          }
98          if (conflict.getGeometry() != null)
99          {
100             PaintPolygons.paintMultiPolygon(graphics, fillColor, conflict.getLocation(), conflict.getGeometry(), false);
101         }
102         graphics.setTransform(saveAT);
103 
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public final String toString()
109     {
110         return "ConflictAnimation [getSource()=" + getSource() + "]";
111     }
112 
113 }