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
9 import javax.naming.NamingException;
10
11 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
12 import org.opentrafficsim.core.geometry.Bounds;
13 import org.opentrafficsim.core.geometry.DirectedPoint;
14 import org.opentrafficsim.core.geometry.OtsLine3d;
15 import org.opentrafficsim.draw.core.PaintLine;
16 import org.opentrafficsim.draw.core.PaintPolygons;
17 import org.opentrafficsim.draw.core.TextAlignment;
18 import org.opentrafficsim.draw.core.TextAnimation;
19 import org.opentrafficsim.road.network.lane.Lane;
20
21 import nl.tudelft.simulation.dsol.animation.Locatable;
22 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
23 import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
24 import nl.tudelft.simulation.language.d2.Angle;
25 import nl.tudelft.simulation.naming.context.Contextualized;
26
27
28
29
30
31
32
33
34 public class LaneAnimation extends Renderable2D<Lane> implements Renderable2DInterface<Lane>, Serializable
35 {
36
37 private static final long serialVersionUID = 20141017L;
38
39
40 private final Color color;
41
42
43 private final Text text;
44
45
46
47
48
49
50
51
52
53 public LaneAnimation(final Lane lane, final OtsSimulatorInterface simulator, final Color color)
54 throws NamingException, RemoteException
55 {
56 super(lane, simulator);
57 this.color = color;
58 this.text = new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER,
59 Color.BLACK, simulator);
60 new CenterLineAnimation(new CenterLine(lane.getCenterLine()), simulator);
61 }
62
63
64
65
66 public final Text getText()
67 {
68 return this.text;
69 }
70
71
72 @Override
73 public final void paint(final Graphics2D graphics, final ImageObserver observer)
74 {
75 Lane lane = getSource();
76 if (this.color != null)
77 {
78 PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
79 }
80 }
81
82
83 @Override
84 public void destroy(final Contextualized contextProvider)
85 {
86 super.destroy(contextProvider);
87 this.text.destroy(contextProvider);
88 }
89
90
91 @Override
92 public final String toString()
93 {
94 return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + "]";
95 }
96
97
98
99
100 public static class CenterLine implements Locatable
101 {
102
103 private final OtsLine3d centerLine;
104
105
106
107
108
109 CenterLine(final OtsLine3d centerLine)
110 {
111 this.centerLine = centerLine;
112 }
113
114 @Override
115 public final DirectedPoint getLocation()
116 {
117 DirectedPoint dp = this.centerLine.getLocation();
118 return new DirectedPoint(dp.x, dp.y, dp.z + 0.1);
119 }
120
121 @Override
122 public final Bounds getBounds() throws RemoteException
123 {
124 return this.centerLine.getBounds();
125 }
126
127
128
129
130
131 public OtsLine3d getCenterLine()
132 {
133 return this.centerLine;
134 }
135
136 }
137
138
139
140
141 public static class CenterLineAnimation extends Renderable2D<CenterLine>
142 implements Renderable2DInterface<CenterLine>, Serializable
143 {
144
145 private static final Color COLOR = Color.MAGENTA.darker().darker();
146
147
148 private static final long serialVersionUID = 20180426L;
149
150
151
152
153
154
155
156
157 public CenterLineAnimation(final CenterLine centerLine, final OtsSimulatorInterface simulator)
158 throws NamingException, RemoteException
159 {
160 super(centerLine, simulator);
161 }
162
163 @Override
164 public final void paint(final Graphics2D graphics, final ImageObserver observer)
165 {
166 PaintLine.paintLine(graphics, COLOR, 0.1, getSource().getLocation(), getSource().getCenterLine());
167 }
168
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182 public class Text extends TextAnimation
183 {
184
185 private static final long serialVersionUID = 20161211L;
186
187
188
189
190
191
192
193
194
195
196
197
198 public Text(final Locatable source, final String text, final float dx, final float dy,
199 final TextAlignment textPlacement, final Color color, final OtsSimulatorInterface simulator)
200 throws RemoteException, NamingException
201 {
202 super(source, text, dx, dy, textPlacement, color, simulator, TextAnimation.RENDERALWAYS);
203 }
204
205
206 @Override
207 @SuppressWarnings("checkstyle:designforextension")
208 public DirectedPoint getLocation()
209 {
210
211 DirectedPoint p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
212 double a = Angle.normalizePi(p.getRotZ());
213 if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
214 {
215 a += Math.PI;
216 }
217 return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
218 }
219
220
221 @Override
222 public final String toString()
223 {
224 return "Text []";
225 }
226
227 }
228
229 }