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