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.dsol.OtsSimulatorInterface;
15 import org.opentrafficsim.core.geometry.DirectedPoint;
16 import org.opentrafficsim.core.geometry.OtsGeometryException;
17 import org.opentrafficsim.core.geometry.OtsLine3d;
18 import org.opentrafficsim.core.geometry.OtsPoint3d;
19 import org.opentrafficsim.core.network.Link;
20 import org.opentrafficsim.core.network.LinkType;
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.animation.D2.Renderable2DInterface;
28 import nl.tudelft.simulation.language.d2.Angle;
29 import nl.tudelft.simulation.naming.context.Contextualized;
30
31
32
33
34
35
36
37
38
39 public class LinkAnimation extends Renderable2D<Link> implements Renderable2DInterface<Link>, Serializable
40 {
41
42 private static final long serialVersionUID = 20140000L;
43
44
45 private float width;
46
47
48 private Text text;
49
50
51
52
53
54
55
56
57 public LinkAnimation(final Link link, final OtsSimulatorInterface simulator, final float width)
58 throws NamingException, RemoteException
59 {
60 super(link, simulator);
61 this.width = width;
62 this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator,
63 TextAnimation.RENDERWHEN10);
64 }
65
66
67 @Override
68 public final void paint(final Graphics2D graphics, final ImageObserver observer)
69 {
70 Color color = getSource().isConnector() ? Color.PINK.darker() : Color.BLUE;
71 OtsLine3d designLine = getSource().getDesignLine();
72 PaintLine.paintLine(graphics, color, this.width, getSource().getLocation(), designLine);
73
74 try
75 {
76 drawEndPoint(designLine.getFirst(), designLine.get(1), graphics);
77 drawEndPoint(designLine.getLast(), designLine.get(designLine.size() - 2), graphics);
78 }
79 catch (OtsGeometryException exception)
80 {
81
82 CategoryLogger.always().error(exception);
83 }
84 }
85
86
87
88
89
90
91
92
93 private void drawEndPoint(final OtsPoint3d endPoint, final OtsPoint3d nextPoint, final Graphics2D graphics)
94 {
95
96 double dx = nextPoint.x - endPoint.x;
97 double dy = nextPoint.y - endPoint.y;
98 double length = endPoint.distanceSI(nextPoint);
99
100 dx *= this.width / length;
101 dy *= this.width / length;
102 PolyLine3d line = new PolyLine3d(new Point3d(endPoint.x - dy, endPoint.y + dx, endPoint.z),
103 new Point3d(endPoint.x + dy, endPoint.y - dx, endPoint.z));
104 PaintLine.paintLine(graphics, getSource().isConnector() ? Color.PINK.darker() : Color.BLUE, this.width / 30,
105 getSource().getLocation(), line);
106 }
107
108
109 @Override
110 public void destroy(final Contextualized contextProvider)
111 {
112 super.destroy(contextProvider);
113 this.text.destroy(contextProvider);
114 }
115
116
117 @Override
118 public final String toString()
119 {
120 return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134 public class Text extends TextAnimation
135 {
136
137 private static final long serialVersionUID = 20161211L;
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public Text(final Locatable source, final String text, final float dx, final float dy,
152 final TextAlignment textPlacement, final Color color, final OtsSimulatorInterface simulator,
153 final ScaleDependentRendering scaleDependentRendering) throws RemoteException, NamingException
154 {
155 super(source, text, dx, dy, textPlacement, color, 2.0f, 12.0f, 50f, simulator, null, scaleDependentRendering);
156 }
157
158
159 @Override
160 @SuppressWarnings("checkstyle:designforextension")
161 public DirectedPoint getLocation()
162 {
163
164 DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
165 double a = Angle.normalizePi(p.getRotZ());
166 if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
167 {
168 a += Math.PI;
169 }
170 return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
171 }
172
173
174 @Override
175 public final String toString()
176 {
177 return "LinkAnimation.Text []";
178 }
179 }
180
181 }