View Javadoc
1   package org.opentrafficsim.road.gtu.lane.object.animation;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.geom.Rectangle2D;
6   import java.awt.image.ImageObserver;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  
11  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
12  
13  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
14  import org.opentrafficsim.road.gtu.lane.object.LaneBlockOnOff;
15  
16  /**
17   * Draw a road block.
18   * <p>
19   * Copyright (c) 2013-2015 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * @version $Revision: 1155 $, $LastChangedDate: 2015-07-26 01:01:13 +0200 (Sun, 26 Jul 2015) $, by $Author: averbraeck $,
23   *          initial version 29 dec. 2014 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   */
27  public class DefaultBlockOnOffAnimation extends Renderable2D
28  {
29      /** The half width left and right of the center line that is used to draw the block. */
30      private final double halfWidth;
31  
32      /**
33       * Construct the DefaultCarAnimation for a LaneBlock (road block).
34       * @param source the Car to draw
35       * @param simulator the simulator to schedule on
36       * @throws NamingException in case of registration failure of the animation
37       * @throws RemoteException on communication failure
38       */
39      public DefaultBlockOnOffAnimation(final LaneBlockOnOff source, final OTSSimulatorInterface simulator)
40          throws NamingException, RemoteException
41      {
42          super(source, simulator);
43          this.halfWidth = 0.4 * source.getLane().getWidth(0.0).getSI();
44      }
45  
46      /** {@inheritDoc} */
47      @Override
48      public final void paint(final Graphics2D graphics, final ImageObserver observer) 
49      {
50          if (((LaneBlockOnOff) this.source).isBlocked())
51          {
52              graphics.setColor(Color.RED);
53          }
54          else
55          {
56              graphics.setColor(Color.GREEN);
57          }
58          Rectangle2D rectangle = new Rectangle2D.Double(-0.4, -this.halfWidth, 0.8, 2 * this.halfWidth);
59          graphics.fill(rectangle);
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public final String toString()
65      {
66          return "DefaultBlockOnOffAnimation [getSource()=" + this.getSource() + "]";
67      }
68  
69  }