1 package org.opentrafficsim.road.network.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.io.Serializable;
8 import java.rmi.RemoteException;
9
10 import javax.naming.NamingException;
11
12 import org.djunits.value.vdouble.scalar.Length;
13 import org.opentrafficsim.core.animation.ClonableRenderable2DInterface;
14 import org.opentrafficsim.core.animation.TextAlignment;
15 import org.opentrafficsim.core.animation.TextAnimation;
16 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
17 import org.opentrafficsim.road.network.lane.object.sensor.SingleSensor;
18
19 import nl.tudelft.simulation.dsol.animation.Locatable;
20 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
21
22
23
24
25
26
27
28
29
30
31
32
33
34 public class SensorAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
35 {
36
37 private static final long serialVersionUID = 20150130L;
38
39
40 private final Length sensorPosition;
41
42
43 private final double halfWidth;
44
45
46 private final Color color;
47
48
49 private Text text;
50
51
52
53
54
55
56
57
58
59
60 public SensorAnimation(final SingleSensor sensor, final Length sensorPosition, final OTSSimulatorInterface simulator,
61 final Color color) throws NamingException, RemoteException
62 {
63 super(sensor, simulator);
64 this.halfWidth = 0.45 * sensor.getLane().getWidth(sensorPosition).getSI();
65 this.sensorPosition = sensorPosition;
66 this.color = color;
67
68 new Text(sensor, sensor.getLane().getParentLink().getId() + "." + sensor.getLane().getId() + sensor.getId(), 0.0f,
69 (float) this.halfWidth + 0.2f, TextAlignment.CENTER, Color.BLACK, simulator);
70 }
71
72
73 @Override
74 public final void paint(final Graphics2D graphics, final ImageObserver observer)
75 {
76 graphics.setColor(this.color);
77 Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
78 graphics.fill(rectangle);
79 }
80
81
82 @Override
83 public final void destroy() throws NamingException
84 {
85 super.destroy();
86 this.text.destroy();
87 }
88
89
90 @Override
91 @SuppressWarnings("checkstyle:designforextension")
92 public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
93 throws NamingException, RemoteException
94 {
95
96 return new SensorAnimation((SingleSensor) newSource, this.sensorPosition, newSimulator, this.color);
97 }
98
99
100 @Override
101 public final String toString()
102 {
103 return "SensorAnimation [getSource()=" + this.getSource() + "]";
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119 public class Text extends TextAnimation
120 {
121
122 private static final long serialVersionUID = 20161211L;
123
124
125
126
127
128
129
130
131
132
133
134
135 public Text(final Locatable source, final String text, final float dx, final float dy,
136 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
137 throws RemoteException, NamingException
138 {
139 super(source, text, dx, dy, textPlacement, color, simulator);
140 }
141
142
143 @Override
144 @SuppressWarnings("checkstyle:designforextension")
145 public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
146 throws RemoteException, NamingException
147 {
148 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
149 }
150 }
151
152 }