1 package org.opentrafficsim.draw.road;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.image.ImageObserver;
6 import java.io.Serializable;
7 import java.rmi.RemoteException;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import javax.naming.NamingException;
12
13 import org.opentrafficsim.core.geometry.OTSGeometryException;
14 import org.opentrafficsim.core.geometry.OTSLine3D;
15 import org.opentrafficsim.core.geometry.OTSPoint3D;
16 import org.opentrafficsim.road.network.lane.object.sensor.TrafficLightSensor;
17
18 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
20
21
22
23
24
25
26
27
28
29
30
31
32
33 public class TrafficLightSensorAnimation extends Renderable2D<TrafficLightSensor> implements Serializable
34 {
35
36 private static final long serialVersionUID = 20150130L;
37
38
39 private final TrafficLightSensor sensor;
40
41
42 private final OTSLine3D path;
43
44
45
46
47
48
49
50
51
52 public TrafficLightSensorAnimation(final TrafficLightSensor sensor, final SimulatorInterface.TimeDoubleUnit simulator)
53 throws NamingException, RemoteException, OTSGeometryException
54 {
55 super(sensor, simulator);
56 this.sensor = sensor;
57 OTSLine3D coordinates = this.sensor.getPath();
58 double dx = this.sensor.getLocation().x;
59 double dy = this.sensor.getLocation().y;
60 double dz = this.sensor.getLocation().z;
61 List<OTSPoint3D> points = new ArrayList<>(coordinates.size());
62 for (OTSPoint3D p : coordinates.getPoints())
63 {
64 points.add(new OTSPoint3D(p.x - dx, p.y - dy, p.z - dz));
65 }
66 this.path = new OTSLine3D(points);
67 }
68
69
70 @Override
71 public final void paint(final Graphics2D graphics, final ImageObserver observer)
72 {
73 graphics.setColor(this.sensor.getOccupancy() ? Color.BLUE : Color.BLACK);
74 OTSPoint3D prevPoint = null;
75 for (OTSPoint3D p : this.path.getPoints())
76 {
77 if (null != prevPoint)
78 {
79
80 graphics.drawLine((int) prevPoint.x, (int) prevPoint.y, (int) p.x, (int) p.y);
81 }
82 prevPoint = p;
83 }
84 }
85
86
87 @Override
88 public final String toString()
89 {
90 return "SensorAnimation [getSource()=" + this.getSource() + "]";
91 }
92
93 }