1 package org.opentrafficsim.draw.network;
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.djutils.draw.line.PolyLine3d;
12 import org.djutils.draw.point.Point3d;
13 import org.djutils.logger.CategoryLogger;
14 import org.opentrafficsim.core.geometry.DirectedPoint;
15 import org.opentrafficsim.core.geometry.OTSGeometryException;
16 import org.opentrafficsim.core.geometry.OTSLine3D;
17 import org.opentrafficsim.core.geometry.OTSPoint3D;
18 import org.opentrafficsim.core.network.Link;
19 import org.opentrafficsim.core.network.LinkType;
20 import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
21 import org.opentrafficsim.draw.core.PaintLine;
22 import org.opentrafficsim.draw.core.TextAlignment;
23 import org.opentrafficsim.draw.core.TextAnimation;
24
25 import nl.tudelft.simulation.dsol.animation.Locatable;
26 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
27 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
28 import nl.tudelft.simulation.language.d2.Angle;
29
30
31
32
33
34
35
36
37
38
39
40 public class LinkAnimation extends Renderable2D<Link> implements ClonableRenderable2DInterface<Link>, Serializable
41 {
42
43 private static final long serialVersionUID = 20140000L;
44
45
46 private float width;
47
48
49 private Text text;
50
51
52
53
54
55
56
57
58 public LinkAnimation(final Link link, final SimulatorInterface.TimeDoubleUnit simulator, final float width)
59 throws NamingException, RemoteException
60 {
61 super(link, simulator);
62 this.width = width;
63 this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator,
64 link.getLinkType().getId().equals(LinkType.DEFAULTS.FREEWAY.getId()) ? TextAnimation.RENDERWHEN10
65 : TextAnimation.RENDERWHEN1);
66 }
67
68
69 @Override
70 public final void paint(final Graphics2D graphics, final ImageObserver observer)
71 {
72 try
73 {
74 Color color = getSource().getLinkType().isConnector() ? Color.PINK.darker() : Color.BLUE;
75 OTSLine3D designLine = getSource().getDesignLine();
76 PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
77
78 try
79 {
80 drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
81 drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
82 }
83 catch (OTSGeometryException exception)
84 {
85
86 CategoryLogger.always().error(exception);
87 }
88 }
89 catch (RemoteException e)
90 {
91 CategoryLogger.always().warn(e);
92 }
93 }
94
95
96
97
98
99
100
101
102 private void drawEndPoint(final OTSPoint3D endPoint, final OTSPoint3D nextPoint, final Graphics2D graphics)
103 {
104
105 double dx = nextPoint.x - endPoint.x;
106 double dy = nextPoint.y - endPoint.y;
107 double length = endPoint.distanceSI(nextPoint);
108
109 dx *= this.width / length;
110 dy *= this.width / length;
111 try
112 {
113 PolyLine3d line = new PolyLine3d(new Point3d(endPoint.x - dy, endPoint.y + dx, endPoint.z),
114 new Point3d(endPoint.x + dy, endPoint.y - dx, endPoint.z));
115 PaintLine.paintLine(graphics, getSource().getLinkType().isConnector() ? Color.PINK.darker() : Color.BLUE,
116 this.width / 30, getSource().getLocation(), line);
117 }
118 catch (RemoteException exception)
119 {
120 CategoryLogger.always().error(exception);
121 }
122 }
123
124
125 @Override
126 public final void destroy(final SimulatorInterface<?, ?, ?> simulator)
127 {
128 super.destroy(simulator);
129 this.text.destroy(simulator);
130 }
131
132
133 @Override
134 @SuppressWarnings("checkstyle:designforextension")
135 public ClonableRenderable2DInterface<Link> clone(final Link newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
136 throws NamingException, RemoteException
137 {
138
139 return new LinkAnimation(newSource, newSimulator, this.width);
140 }
141
142
143 @Override
144 public final String toString()
145 {
146 return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
147 }
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162 public class Text extends TextAnimation
163 {
164
165 private static final long serialVersionUID = 20161211L;
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public Text(final Locatable source, final String text, final float dx, final float dy,
180 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator,
181 final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
182 {
183 super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, simulator, null, scaleDependentRendering);
184 }
185
186
187 @Override
188 @SuppressWarnings("checkstyle:designforextension")
189 public DirectedPoint getLocation()
190 {
191
192 DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
193 double a = Angle.normalizePi(p.getRotZ());
194 if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
195 {
196 a += Math.PI;
197 }
198 return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
199 }
200
201
202 @Override
203 @SuppressWarnings("checkstyle:designforextension")
204 public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
205 throws RemoteException, NamingException
206 {
207 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator,
208 super.getScaleDependentRendering());
209 }
210
211
212 @Override
213 public final String toString()
214 {
215 return "LinkAnimation.Text []";
216 }
217 }
218
219 }