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.real.StopSignInterface;
27  
28  /***
29   * This class is used for the animation of a simple red/green traffic light.
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 TrafficLightAnimation extends Renderable2D
47  {
48      /*** the deltax of the place of the traffic light */
49      private double deltax;
50  
51      /*** the deltay of the place of the traffic light */
52      private double deltay;
53  
54      /***
55       * @param owner
56       * @param simulator
57       */
58      public TrafficLightAnimation(final LocatableInterface owner,
59              final SimulatorInterface simulator)
60      {
61          this(owner, simulator, 0.0, 0.0);
62      }
63  
64      /***
65       * @param owner
66       * @param simulator
67       * @param deltax
68       * @param deltay
69       */
70      public TrafficLightAnimation(final LocatableInterface owner,
71              final SimulatorInterface simulator, final double deltax,
72              final double deltay)
73      {
74          super(owner, simulator);
75          this.deltax = deltax;
76          this.deltay = -deltay;
77      }
78  
79      /***
80       * @see nl.tudelft.simulation.dsol.animation.D2.Renderable2D#paint(java.awt.Graphics2D,
81       * java.awt.image.ImageObserver)
82       */
83      public void paint(Graphics2D graphics, ImageObserver imo)
84              throws RemoteException
85      {
86          StopSignInterface trafficLight = (StopSignInterface) super.source;
87          graphics.setColor(Color.BLACK);
88          graphics.setStroke(new BasicStroke(0.05f));
89          graphics.draw(new Line2D.Double(0, 0, this.deltax, this.deltay));
90          graphics.fill(new Rectangle2D.Double(this.deltax - 2, this.deltay - 8,
91                  4, 8));
92          if (trafficLight.getStatus() == StopSignInterface.CONTINUE)
93          {
94              graphics.setColor(Color.GREEN);
95              graphics.fill(new Ellipse2D.Double(this.deltax - 1.5,
96                      this.deltay - 3.5, 3, 3));
97          } else
98          {
99              graphics.setColor(Color.RED);
100             graphics.fill(new Ellipse2D.Double(this.deltax - 1.5,
101                     this.deltay - 7.5, 3, 3));
102         }
103         if (true)
104         {
105             graphics.setColor(Color.BLACK);
106             Font smallFont = new Font("SansSerif", Font.PLAIN, 2);
107             graphics.setFont(smallFont);
108             graphics.drawString(this.source.toString(),
109                     (float) (this.deltax + 4), (float) (this.deltay));
110         }
111     }
112 }