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-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
29   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
30   */
31  public class ConflictAnimation extends AbstractLineAnimation<Conflict> implements Serializable
32  {
33  
34      /** */
35      private static final long serialVersionUID = 20161207L;
36  
37      /**
38       * @param source Conflict; the conflict to draw
39       * @param simulator OtsSimulatorInterface; 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)
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 YIELD:
66                  fillColor = Color.orange;
67                  break;
68  
69              default:
70                  // STOP, ALL_STOP, TURN_ON_RED
71                  fillColor = Color.red;
72                  break;
73          }
74  
75          graphics.setColor(fillColor);
76          super.paint(graphics, observer);
77  
78          Stroke oldStroke = graphics.getStroke();
79  
80          BasicStroke stroke;
81          float factor = conflict.isPermitted() ? .5f : 1f;
82          if (conflict.getConflictType().equals(ConflictType.CROSSING))
83          {
84              stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
85                      new float[] {factor * 1.0f, factor * 2.0f}, 0.0f);
86          }
87          else
88          {
89              stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
90                      new float[] {factor * 1.0f, factor * 0.95f, factor * 0.1f, factor * 0.95f}, 0.0f);
91          }
92          graphics.setStroke(stroke);
93          AffineTransform saveAT = graphics.getTransform();
94          double angle = -getSource().getLocation().getRotZ();
95          if (isRotate() && angle != 0.0)
96          {
97              graphics.rotate(-angle);
98          }
99          if (conflict.getGeometry() != null)
100         {
101             PaintPolygons.paintMultiPolygon(graphics, fillColor, conflict.getLocation(), conflict.getGeometry(), false);
102             
103             /*- This code may be used to visually check conflicts are correctly paired
104             if (conflict.conflictPriority().isPriority())
105             {
106                 graphics.setColor(Color.BLACK);
107                 DirectedPoint from = conflict.getLocation();
108                 DirectedPoint to = conflict.getOtherConflict().getLocation();
109                 graphics.setStroke(new BasicStroke(0.1f));
110                 Line2D line = new Line2D.Double(0, 0, to.x - from.x, from.y - to.y);
111                 graphics.draw(line);
112             }*/
113         }
114         if (isRotate() && angle != 0.0)
115         {
116             graphics.rotate(+angle);
117         }
118         graphics.setStroke(oldStroke);
119         graphics.setTransform(saveAT);
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public final String toString()
125     {
126         return "ConflictAnimation [getSource()=" + getSource() + "]";
127     }
128 
129 }