1 package org.opentrafficsim.draw.network;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.geom.Ellipse2D;
6 import java.awt.image.ImageObserver;
7 import java.io.Serializable;
8 import java.rmi.RemoteException;
9
10 import javax.media.j3d.BoundingSphere;
11 import javax.media.j3d.Bounds;
12 import javax.naming.NamingException;
13
14 import org.djutils.logger.CategoryLogger;
15 import org.opentrafficsim.core.network.Node;
16 import org.opentrafficsim.draw.core.ClonableRenderable2DInterface;
17 import org.opentrafficsim.draw.core.TextAlignment;
18 import org.opentrafficsim.draw.core.TextAnimation;
19
20 import nl.tudelft.simulation.dsol.animation.Locatable;
21 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
22 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
23 import nl.tudelft.simulation.introspection.DelegateIntrospection;
24 import nl.tudelft.simulation.language.d3.DirectedPoint;
25
26
27
28
29
30
31
32
33
34
35 @SuppressWarnings("rawtypes")
36 public class NodeAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
37 {
38
39 private static final long serialVersionUID = 20140000L;
40
41
42 private Text text;
43
44
45 public static final double ZOFFSET = 0.01;
46
47
48
49
50
51
52
53 @SuppressWarnings("unchecked")
54 public NodeAnimation(final Node node, final SimulatorInterface.TimeDoubleUnit simulator)
55 throws NamingException, RemoteException
56 {
57 super(new ElevatedNode(node), simulator);
58 this.text = new Text(node, node.getId(), 0.0f, 3.0f, TextAlignment.CENTER, Color.BLACK, simulator);
59 }
60
61
62 @Override
63 public final void paint(final Graphics2D graphics, final ImageObserver observer)
64 {
65 graphics.setColor(Color.BLACK);
66 graphics.draw(new Ellipse2D.Double(-0.5, -0.5, 1.0, 1.0));
67 }
68
69
70 @Override
71 public final void destroy() throws NamingException
72 {
73 super.destroy();
74 this.text.destroy();
75 }
76
77
78 @Override
79 @SuppressWarnings("checkstyle:designforextension")
80 public ClonableRenderable2DInterface clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
81 throws NamingException, RemoteException
82 {
83
84 return new NodeAnimation((Node) newSource, newSimulator);
85 }
86
87
88 @Override
89 public final String toString()
90 {
91 return "NodeAnimation [node=" + super.getSource() + "]";
92 }
93
94
95 public static class ElevatedNode implements Locatable, DelegateIntrospection
96 {
97
98 private final Node node;
99
100
101 private DirectedPoint location;
102
103
104 private Bounds bounds;
105
106
107
108
109 public ElevatedNode(final Node node)
110 {
111 super();
112 this.node = node;
113 try
114 {
115 this.location = new DirectedPoint(node.getLocation().x, node.getLocation().y, node.getLocation().z + ZOFFSET);
116 this.bounds = node.getBounds();
117 }
118 catch (RemoteException exception)
119 {
120 CategoryLogger.always().error(exception, "Could not construct elevated node for animation");
121 this.location = new DirectedPoint();
122 this.bounds = new BoundingSphere();
123 }
124 }
125
126
127 @Override
128 public DirectedPoint getLocation() throws RemoteException
129 {
130 return this.location;
131 }
132
133
134 @Override
135 public Bounds getBounds() throws RemoteException
136 {
137 return this.bounds;
138 }
139
140
141 @Override
142 public Object getParentIntrospectionObject()
143 {
144 return this.node;
145 }
146
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 public Text(final Locatable source, final String text, final float dx, final float dy,
179 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
180 throws RemoteException, NamingException
181 {
182 super(source, text, dx, dy, textPlacement, color, simulator);
183 setFlip(false);
184 setRotate(false);
185 }
186
187
188 @Override
189 @SuppressWarnings("checkstyle:designforextension")
190 public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
191 throws RemoteException, NamingException
192 {
193 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
194 }
195
196
197 @Override
198 public final String toString()
199 {
200 return "NodeAnimation.Text []";
201 }
202 }
203
204 }