View Javadoc

1   /*
2    * @(#)BlockTrafficLight.java June 6, 2004
3    * 
4    * Copyright (c) 2003, 2004 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  
11  package nl.tudelft.simulation.traffic.animation;
12  
13  import java.awt.Color;
14  import java.awt.Graphics2D;
15  import java.awt.geom.RoundRectangle2D;
16  import java.awt.image.ImageObserver;
17  import java.rmi.RemoteException;
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20  import nl.tudelft.simulation.event.EventInterface;
21  import nl.tudelft.simulation.event.EventListenerInterface;
22  import nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface;
23  import nl.tudelft.simulation.traffic.vehicle.VehicleSegment;
24  
25  /***
26   * This class animates the physical vehicle <br>
27   * <br>
28   * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
29   * University of Technology </a>, the Netherlands. <br>
30   * See for project information <a href="http://www.simulation.tudelft.nl">
31   * www.simulation.tudelft.nl </a> <br>
32   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
33   * Public License (GPL) </a>, no warranty <br>
34   * 
35   * @version May 31, 2004 <br>
36   * @author <a
37   * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
38   * Verbraeck </a>
39   * 
40   * @see nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface
41   */
42  public class SegmentedVehicleAnimation extends Renderable2D
43          implements
44              EventListenerInterface
45  {
46      /*** the physical vehicle segment */
47      private VehicleSegment segment;
48  
49      /*** the color */
50      private Color color;
51  
52      /***
53       * constructs a new SegmentedVehicleAnimation
54       * 
55       * @param segment
56       * @param simulator the simulator
57       * @param color
58       */
59      public SegmentedVehicleAnimation(final VehicleSegment segment,
60              final SimulatorInterface simulator, final Color color)
61      {
62          super(segment, simulator);
63          try
64          {
65              segment.getVehicle().addListener(this,
66                      VehiclePhysicalInterface.REMOVE_EVENT);
67          } catch (RemoteException e)
68          {
69              e.printStackTrace();
70          }
71          this.segment = segment;
72          this.color = color;
73      }
74  
75      /***
76       * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2D#paint(java.awt.Graphics2D,
77       * java.awt.image.ImageObserver)
78       */
79      public void paint(final Graphics2D gr, final ImageObserver imo)
80      {
81          if (this.segment == null)
82              return;
83          double width = this.segment.getVehicle().getVehicleType().getWidth();
84          double length = this.segment.getLength();
85          gr.setColor(this.color);
86          gr.fill(new RoundRectangle2D.Double(0.0, (-width / 2.0d), length, width,
87                  width / 2.0d, width / 2.0d));
88      }
89  
90      /***
91       * @see nl.tudelft.simulation.event.EventListenerInterface#notify(nl.tudelft.simulation.event.EventInterface)
92       */
93      public void notify(EventInterface event) throws RemoteException
94      {
95          if (event.getType().equals(VehiclePhysicalInterface.REMOVE_EVENT))
96          {
97              super.destroy();
98              this.segment = null;
99          }
100     }
101 }