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