1 package org.opentrafficsim.road.network.animation;
2
3 import java.awt.Color;
4 import java.awt.Graphics2D;
5 import java.awt.Shape;
6 import java.awt.geom.Ellipse2D;
7 import java.awt.image.ImageObserver;
8 import java.io.Serializable;
9 import java.rmi.RemoteException;
10
11 import javax.naming.NamingException;
12
13 import org.opentrafficsim.core.animation.ClonableRenderable2DInterface;
14 import org.opentrafficsim.core.animation.TextAlignment;
15 import org.opentrafficsim.core.animation.TextAnimation;
16 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
17 import org.opentrafficsim.core.network.animation.PaintLine;
18 import org.opentrafficsim.core.network.animation.PaintPolygons;
19 import org.opentrafficsim.road.network.lane.Lane;
20
21 import nl.tudelft.simulation.dsol.animation.Locatable;
22 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
23 import nl.tudelft.simulation.language.d2.Angle;
24 import nl.tudelft.simulation.language.d3.DirectedPoint;
25
26
27
28
29
30
31
32
33
34
35 public class LaneAnimation extends Renderable2D implements ClonableRenderable2DInterface, Serializable
36 {
37
38 private static final long serialVersionUID = 20141017L;
39
40
41 private final Color color;
42
43
44 private final boolean drawCenterLine;
45
46
47 private Text text;
48
49
50
51
52
53
54
55
56
57
58 public LaneAnimation(final Lane lane, final OTSSimulatorInterface simulator, final Color color,
59 final boolean drawCenterLine) throws NamingException, RemoteException
60 {
61 super(lane, simulator);
62 this.color = color;
63 this.drawCenterLine = drawCenterLine;
64 new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK,
65 simulator);
66 }
67
68
69 @Override
70 public final void paint(final Graphics2D graphics, final ImageObserver observer)
71 {
72 Lane lane = (Lane) getSource();
73 if (this.color != null)
74 {
75 PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
76 }
77
78 if (this.drawCenterLine)
79 {
80 PaintLine.paintLine(graphics, Color.RED, 0.25, lane.getLocation(), lane.getCenterLine());
81 Shape startCircle = new Ellipse2D.Double(lane.getCenterLine().getFirst().x - lane.getLocation().x - 0.25,
82 -lane.getCenterLine().getFirst().y + lane.getLocation().y - 0.25, 0.5, 0.5);
83 Shape endCircle = new Ellipse2D.Double(lane.getCenterLine().getLast().x - lane.getLocation().x - 0.25,
84 -lane.getCenterLine().getLast().y + lane.getLocation().y - 0.25, 0.5, 0.5);
85 graphics.setColor(Color.BLUE);
86 graphics.fill(startCircle);
87 graphics.setColor(Color.RED);
88 graphics.fill(endCircle);
89 }
90 }
91
92
93 @Override
94 public final void destroy() throws NamingException
95 {
96 super.destroy();
97 this.text.destroy();
98 }
99
100
101 @Override
102 @SuppressWarnings("checkstyle:designforextension")
103 public ClonableRenderable2DInterface clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
104 throws NamingException, RemoteException
105 {
106
107 return new LaneAnimation((Lane) newSource, newSimulator, this.color, this.drawCenterLine);
108 }
109
110
111 @Override
112 public final String toString()
113 {
114 return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + ", drawCenterLine="
115 + this.drawCenterLine + "]";
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public class Text extends TextAnimation
132 {
133
134 private static final long serialVersionUID = 20161211L;
135
136
137
138
139
140
141
142
143
144
145
146
147 public Text(final Locatable source, final String text, final float dx, final float dy,
148 final TextAlignment textPlacement, final Color color, final OTSSimulatorInterface simulator)
149 throws RemoteException, NamingException
150 {
151 super(source, text, dx, dy, textPlacement, color, simulator);
152 }
153
154
155 @Override
156 @SuppressWarnings("checkstyle:designforextension")
157 public DirectedPoint getLocation() throws RemoteException
158 {
159
160 DirectedPoint p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
161 double a = Angle.normalizePi(p.getRotZ());
162 if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
163 {
164 a += Math.PI;
165 }
166 return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
167 }
168
169
170 @Override
171 @SuppressWarnings("checkstyle:designforextension")
172 public TextAnimation clone(final Locatable newSource, final OTSSimulatorInterface newSimulator)
173 throws RemoteException, NamingException
174 {
175 return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
176 }
177
178 }
179
180 }