View Javadoc

1   /*
2    * Created on Dec 18, 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.Font;
16  import java.awt.Graphics2D;
17  import java.awt.geom.Ellipse2D;
18  import java.awt.geom.Line2D;
19  import java.awt.geom.Rectangle2D;
20  import java.awt.image.ImageObserver;
21  import java.rmi.RemoteException;
22  
23  import nl.tudelft.simulation.dsol.animation.LocatableInterface;
24  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
25  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
26  import nl.tudelft.simulation.traffic.controlpoint.blocks.BlockTrafficLight;
27  
28  /***
29   * This class is used for the animation of a green / red / yellow + green block
30   * traffic light.
31   * <p>
32   * (c) copyright 2004 <a href="http://www.simulation.tudelft.nl">Delft
33   * University of Technology </a>, the Netherlands.
34   * <p>
35   * See for project information <a href="http://www.simulation.tudelft.nl">
36   * www.simulation.tudelft.nl </a> <br>
37   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General
38   * Public License (GPL) </a>, no warranty <br>
39   * 
40   * @version 1.0 June 1, 2004 <br>
41   * @author <a
42   * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
43   * Verbraeck </a> <br>
44   * <b>Original version: </b> H.W.G. Phaff & J.H. Kwakkel
45   * @see nl.tudelft.simulation.traffic.track.TrackInterface
46   */
47  public class BlockTrafficLightAnimation extends Renderable2D
48  {
49      /*** the deltax of the place of the traffic light */
50      private double deltax;
51  
52      /*** the deltay of the place of the traffic light */
53      private double deltay;
54  
55      /***
56       * @param owner
57       * @param simulator
58       */
59      public BlockTrafficLightAnimation(final LocatableInterface owner,
60              final SimulatorInterface simulator)
61      {
62          this(owner, simulator, 0.0, 0.0);
63      }
64  
65      /***
66       * @param owner
67       * @param simulator
68       * @param deltax
69       * @param deltay
70       */
71      public BlockTrafficLightAnimation(final LocatableInterface owner,
72              final SimulatorInterface simulator, final double deltax,
73              final double deltay)
74      {
75          super(owner, simulator);
76          this.deltax = deltax;
77          this.deltay = -deltay;
78      }
79  
80      /***
81       * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2D#paint(java.awt.Graphics2D,
82       * java.awt.image.ImageObserver)
83       */
84      public void paint(Graphics2D graphics, ImageObserver imo)
85              throws RemoteException
86      {
87          BlockTrafficLight trafficLight = (BlockTrafficLight) super.source;
88          graphics.setColor(Color.BLACK);
89          graphics.setStroke(new BasicStroke(0.05f));
90          graphics.draw(new Line2D.Double(0, 0, this.deltax, this.deltay));
91          graphics.fill(new Rectangle2D.Double(this.deltax - 2, this.deltay - 12,
92                  4, 12));
93          String state = trafficLight.getCurrentState();
94          if (state == BlockTrafficLight.STATE_GREEN)
95          {
96              graphics.setColor(Color.GREEN);
97              graphics.fill(new Ellipse2D.Double(this.deltax - 1.5,
98                      this.deltay - 3.5, 3, 3));
99          } else if (state == BlockTrafficLight.STATE_RED)
100         {
101             graphics.setColor(Color.RED);
102             graphics.fill(new Ellipse2D.Double(this.deltax - 1.5,
103                     this.deltay - 7.5, 3, 3));
104         } else if (state == BlockTrafficLight.STATE_YELLOW)
105         {
106             graphics.setColor(Color.GREEN);
107             graphics.fill(new Ellipse2D.Double(this.deltax - 1.5,
108                     this.deltay - 3.5, 3, 3));
109             graphics.setColor(Color.YELLOW);
110             graphics.fill(new Ellipse2D.Double(this.deltax - 1.5,
111                     this.deltay - 11.5, 3, 3));
112         }
113         if (true)
114         {
115             graphics.setColor(Color.BLACK);
116             Font smallFont = new Font("SansSerif", Font.PLAIN, 2);
117             graphics.setFont(smallFont);
118             graphics.drawString(this.source.toString(),
119                     (float) (this.deltax + 4), (float) (this.deltay));
120         }
121     }
122 }