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