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.dsol.OTSSimulatorInterface;
15 import org.opentrafficsim.core.network.Link;
16
17 import nl.tudelft.simulation.dsol.animation.Locatable;
18 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19 import nl.tudelft.simulation.language.d2.Angle;
20 import nl.tudelft.simulation.language.d3.DirectedPoint;
21
22
23
24
25
26
27
28
29
30
31
32 public class LinkAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
33 {
34
35 private static final long serialVersionUID = 20140000L;
36
37
38 private float width;
39
40
41 private Text text;
42
43
44
45
46
47
48
49
50 public LinkAnimation(final Link link, final OTSSimulatorInterface simulator, final float width)
51 throws NamingException, RemoteException
52 {
53 super(link, simulator);
54 this.width = width;
55 this.text = new Text(link, link.getId(), 0.0f, 1.5f, TextAlignment.CENTER, Color.BLACK, simulator);
56 }
57
58
59 @Override
60 public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
61 {
62 PaintLine.paintLine(graphics, Color.RED, this.width, getSource().getLocation(), ((Link) getSource()).getDesignLine());
63 }
64
65
66 @Override
67 public final void destroy() throws NamingException
68 {
69 super.destroy();
70 this.text.destroy();
71 }
72
73
74 @Override
75 @SuppressWarnings("checkstyle:designforextension")
76 public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
77 throws NamingException, RemoteException
78 {
79
80 return new LinkAnimation((Link) newSource, newSimulator, this.width);
81 }
82
83
84 @Override
85 public final String toString()
86 {
87 return "LinkAnimation [width=" + this.width + ", link=" + super.getSource() + "]";
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public class Text extends TextAnimation
104 {
105
106 private static final long serialVersionUID = 20161211L;
107
108
109
110
111
112
113
114
115
116
117
118
119 public Text(final Locatable source, final String text, final float dx, final float dy,
120 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
121 throws RemoteException, NamingException
122 {
123 super(source, text, dx, dy, textPlacement, color, simulator);
124 }
125
126
127 @Override
128 @SuppressWarnings("checkstyle:designforextension")
129 public DirectedPoint getLocation() throws RemoteException
130 {
131
132 DirectedPoint p = ((Link) getSource()).getDesignLine().getLocationFractionExtended(0.5);
133 double a = Angle.normalizePi(p.getRotZ());
134 if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
135 {
136 a += Math.PI;
137 }
138 return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
139 }
140
141
142 @Override
143 @SuppressWarnings("checkstyle:designforextension")
144 public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
145 throws RemoteException, NamingException
146 {
147 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
148 }
149
150
151 @Override
152 public final String toString()
153 {
154 return "LinkAnimation.Text []";
155 }
156 }
157
158 }