1
2
3
4
5
6
7
8
9
10
11 package nl.tudelft.simulation.traffic.animation;
12
13 import java.awt.BasicStroke;
14 import java.awt.Color;
15 import java.awt.Dimension;
16 import java.awt.Font;
17 import java.awt.Graphics2D;
18 import java.awt.geom.Arc2D;
19 import java.awt.geom.Point2D;
20 import java.awt.geom.Rectangle2D;
21 import java.awt.image.ImageObserver;
22
23 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
24 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
25 import nl.tudelft.simulation.traffic.track.ArcTrack;
26 import nl.tudelft.simulation.traffic.track.Track;
27
28 /***
29 * This class is used for the animation of a curved track.
30 * <p>
31 * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
32 * University of Technology </a>, the Netherlands.
33 * <p>
34 * See for project information <a href="http://www.simulation.tudelft.nl">
35 * www.simulation.tudelft.nl </a> <br>
36 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
37 * Public License (GPL) </a>, no warranty <br>
38 *
39 * @version 1.0 June 1, 2004 <br>
40 * @author <a
41 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
42 * Verbraeck </a> <br>
43 * <b>Original version: </b> H.W.G. Phaff & J.H. Kwakkel
44 * @see nl.tudelft.simulation.traffic.track.TrackInterface
45 */
46 public class ArcTrackAnimation extends Renderable2D
47 {
48 /*** the dx of the track that is animated */
49 private double dx;
50
51 /*** the dy of the track that is animated */
52 private double dy;
53
54 /*** the track arc */
55 private ArcTrack owner;
56
57 /*** the distance between the tracks of the track that is animated */
58 private double width;
59
60 /*** the center x-point */
61 private double cx;
62
63 /*** the center y-point */
64 private double cy;
65
66 /***
67 * @param owner the owner
68 * @param simulator the simulator
69 * @param width
70 * @throws Exception
71 */
72 public ArcTrackAnimation(final ArcTrack owner,
73 final SimulatorInterface simulator, final double width)
74 throws Exception
75 {
76 super(owner, simulator);
77 this.dx = ((Track) super.source).getdx();
78 this.dy = -((Track) super.source).getdy();
79 this.owner = owner;
80 this.width = width;
81 this.cx = this.owner.getCenter().getX() - this.owner.getLocation().x;
82 this.cy = -(this.owner.getCenter().getY() - this.owner.getLocation().y);
83 }
84
85 /***
86 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2D#paint(java.awt.Graphics2D,
87 * java.awt.image.ImageObserver)
88 */
89 public void paint(final Graphics2D gr, final ImageObserver io)
90 {
91 try
92 {
93 gr.setColor(Color.LIGHT_GRAY);
94 gr.setStroke(new BasicStroke(0.05f));
95 Arc2D arc = new Arc2D.Double();
96 arc.setArcByCenter(this.cx, this.cy, this.owner.getRadius(), 0, 0,
97 Arc2D.PIE);
98 if (this.owner.isCounterClockwise())
99 arc.setAngles(0, 0, this.dx, this.dy);
100 else
101 arc.setAngles(this.dx, this.dy, 0, 0);
102 double angleStart = arc.getAngleStart();
103 double angleExtent = arc.getAngleExtent();
104 if (false)
105 {
106 gr.draw(arc);
107 }
108 gr.setColor(Color.BLUE);
109 gr.setStroke(new BasicStroke(0.1f));
110 arc.setArcByCenter(this.cx, this.cy, this.owner.getRadius(),
111 angleStart, angleExtent, Arc2D.OPEN);
112 gr.draw(arc);
113 gr.setColor(Color.GREEN);
114 gr.setStroke(new BasicStroke(0.1f));
115 arc.setArcByCenter(this.cx, this.cy, this.owner.getRadius()
116 - this.width / 2.0, angleStart, angleExtent, Arc2D.OPEN);
117 gr.draw(arc);
118 arc.setArcByCenter(this.cx, this.cy, this.owner.getRadius()
119 + this.width / 2.0, angleStart, angleExtent, Arc2D.OPEN);
120 gr.draw(arc);
121 if (true)
122 {
123 arc.setArcByCenter(this.cx, this.cy, this.owner.getRadius(),
124 angleStart, angleExtent / 2.0, Arc2D.OPEN);
125 Point2D half = arc.getEndPoint();
126 gr.setColor(Color.BLACK);
127 Font font = new Font("SansSerif", Font.PLAIN, 2)
128 .deriveFont(1.0f);
129 gr.setFont(font);
130 gr.drawString(this.source.toString(), (float) (half.getX()),
131 (float) (half.getY()));
132 }
133 } catch (Exception e)
134 {
135 e.printStackTrace();
136 }
137 }
138
139 /***
140 * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#contains(java.awt.geom.Point2D,
141 * java.awt.geom.Rectangle2D, java.awt.Dimension)
142 */
143 public boolean contains(Point2D pointWorldCoordinates, Rectangle2D extent,
144 Dimension screen)
145 {
146 return false;
147 }
148 }