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