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.geom.Rectangle2D;
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.opentrafficsim.core.dsol.OTSSimulatorInterface;
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.animation.D2.Renderable2D;
20  
21  /**
22   * <p>
23   * Copyright (c) 2013-2016 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 Renderable2D implements Serializable
32  {
33      // TODO should ConflictAnimation implement the ClonableRenderable2DInterface?
34      
35      /** */
36      private static final long serialVersionUID = 20161207L;
37  
38      /** The half width left and right of the center line that is used to draw the block. */
39      private final double halfWidth;
40  
41      /**
42       * @param source the conflict to draw
43       * @param simulator the simulator to schedule on
44       * @throws NamingException in case of registration failure of the animation
45       * @throws RemoteException on communication failure
46       */
47      public ConflictAnimation(final Conflict source, final OTSSimulatorInterface simulator)
48              throws NamingException, RemoteException
49      {
50          super(source, simulator);
51          this.halfWidth = 0.45 * source.getLane().getWidth(source.getLongitudinalPosition()).getSI();
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
57      {
58          Conflict conflict = (Conflict) this.getSource();
59          Color fillColor;
60          switch (conflict.getConflictRule())
61          {
62              case SPLIT:
63                  fillColor = Color.blue;
64                  break;
65  
66              case PRIORITY:
67                  fillColor = Color.green;
68                  break;
69  
70              case GIVE_WAY:
71                  fillColor = Color.orange;
72                  break;
73  
74              default:
75                  // STOP, ALL_STOP
76                  fillColor = Color.red;
77                  break;
78          }
79  
80          graphics.setColor(fillColor);
81          Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
82          graphics.fill(rectangle);
83  
84          BasicStroke stroke;
85          float factor = conflict.isPermitted() ? .5f : 1f;
86          if (conflict.getConflictType().equals(ConflictType.CROSSING))
87          {
88              stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
89                      new float[] { factor * 1.0f, factor * 2.0f }, 0.0f);
90          }
91          else
92          {
93              stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
94                      new float[] { factor * 1.0f, factor * 0.95f, factor * 0.1f, factor * 0.95f }, 0.0f);
95          }
96          graphics.setStroke(stroke);
97          AffineTransform saveAT = graphics.getTransform();
98          double angle = -getSource().getLocation().getRotZ();
99          if (isRotate() && angle != 0.0)
100         {
101             graphics.rotate(-angle);
102         }
103         if (conflict.getGeometry() != null)
104         {
105             PaintPolygons.paintMultiPolygon(graphics, fillColor, conflict.getLocation(), conflict.getGeometry(), false);
106         }
107         graphics.setTransform(saveAT);
108 
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public final String toString()
114     {
115         return "ConflictAnimation [getSource()=" + getSource() + "]";
116     }
117 
118 }