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