View Javadoc

1   /*
2    * Created on Nov 12, 2003
3    * 
4    * Copyright (c) 2003, 2004 Delft University of Technology Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
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.Line2D;
19  import java.awt.geom.Point2D;
20  import java.awt.geom.Rectangle2D;
21  import java.awt.image.ImageObserver;
22  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
23  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
24  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
25  import nl.tudelft.simulation.traffic.track.Track;
26  import nl.tudelft.simulation.traffic.track.TrackInterface;
27  
28  /***
29   * This class is used for the animation of a straight 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 StraightTrackAnimation 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 distance betwen the tracks of the track that is animated */
55      private double width;
56  
57      /***
58       * @param owner the owner
59       * @param simulator the simulator
60       * @param width the width of the track
61       */
62      public StraightTrackAnimation(final TrackInterface owner,
63              final SimulatorInterface simulator, final double width)
64      {
65          super((LocatableInterface) owner, simulator);
66          this.dx = ((Track) super.source).getdx();
67          this.dy = -((Track) super.source).getdy();
68          this.width = width;
69      }
70  
71      /***
72       * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2D#paint(java.awt.Graphics2D,
73       * java.awt.image.ImageObserver)
74       */
75      public void paint(final Graphics2D gr, final ImageObserver io)
76      {
77          try
78          {
79              gr.setColor(Color.BLUE);
80              gr.setStroke(new BasicStroke(0.1f));
81              gr.draw(new Line2D.Double(0, 0, this.dx, this.dy));
82              double alpha = Math.atan2(this.dy, this.dx);
83              double wx = this.width * Math.sin(alpha) / 2.0;
84              double wy = this.width * Math.cos(alpha) / 2.0;
85              gr.setColor(Color.GREEN);
86              gr.setStroke(new BasicStroke(0.1f));
87              gr.draw(new Line2D.Double(wx, -wy, this.dx + wx, this.dy - wy));
88              gr.draw(new Line2D.Double(-wx, wy, this.dx - wx, this.dy + wy));
89              if (true)
90              {
91                  double deltax = this.dx / 2;
92                  double deltay = this.dy / 2;
93                  gr.setColor(Color.BLACK);
94                  Font font = new Font("SansSerif", Font.PLAIN, 2)
95                          .deriveFont(1.0f);
96                  gr.setFont(font);
97                  gr.drawString(this.source.toString(), (float) (deltax - 3),
98                          (float) (deltay - 1));
99              }
100         } catch (Exception e)
101         {
102             e.printStackTrace();
103         }
104     }
105     
106     /***
107      * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface#contains(java.awt.geom.Point2D, java.awt.geom.Rectangle2D, java.awt.Dimension)
108      */
109     public boolean contains(Point2D pointWorldCoordinates, Rectangle2D extent,
110             Dimension screen)
111     {
112         //return super.contains(pointWorldCoordinates, extent, screen);
113         return false;
114     }
115 }