View Javadoc
1   package org.opentrafficsim.draw.road;
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.draw.core.ClonableRenderable2DInterface;
14  import org.opentrafficsim.draw.core.PaintLine;
15  import org.opentrafficsim.draw.core.PaintPolygons;
16  import org.opentrafficsim.draw.core.TextAlignment;
17  import org.opentrafficsim.draw.core.TextAnimation;
18  import org.opentrafficsim.road.network.lane.Lane;
19  
20  import nl.tudelft.simulation.dsol.animation.Locatable;
21  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
22  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
23  import nl.tudelft.simulation.language.d2.Angle;
24  import nl.tudelft.simulation.language.d3.DirectedPoint;
25  
26  /**
27   * <p>
28   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
29   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
30   * <p>
31   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
32   * initial version Oct 17, 2014 <br>
33   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
34   */
35  public class LaneAnimation extends Renderable2D<Lane> implements ClonableRenderable2DInterface<Lane>, Serializable
36  {
37      /** */
38      private static final long serialVersionUID = 20141017L;
39  
40      /** Color of the lane. */
41      private final Color color;
42  
43      /** Whether to draw the center line or not. */
44      private final boolean drawCenterLine;
45  
46      /** the Text object to destroy when the animation is destroyed. */
47      private final Text text;
48  
49      /**
50       * Animate a Lane.
51       * @param lane Lane; the lane
52       * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
53       * @param color Color; Color of the lane.
54       * @param drawCenterLine boolean; whether to draw the center line or not
55       * @throws NamingException in case of registration failure of the animation
56       * @throws RemoteException on communication failure
57       */
58      public LaneAnimation(final Lane lane, final SimulatorInterface.TimeDoubleUnit 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          this.text = new Text(lane, lane.getParentLink().getId() + "." + lane.getId(), 0.0f, 0.0f, TextAlignment.CENTER,
65                  Color.BLACK, simulator);
66      }
67  
68      /**
69       * @return text.
70       */
71      public final Text getText()
72      {
73          return this.text;
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public final void paint(final Graphics2D graphics, final ImageObserver observer)
79      {
80          Lane lane = getSource();
81          if (this.color != null)
82          {
83              PaintPolygons.paintMultiPolygon(graphics, this.color, lane.getLocation(), lane.getContour(), true);
84          }
85  
86          if (this.drawCenterLine)
87          {
88              PaintLine.paintLine(graphics, Color.RED, 0.25, lane.getLocation(), lane.getCenterLine());
89              Shape startCircle = new Ellipse2D.Double(lane.getCenterLine().getFirst().x - lane.getLocation().x - 0.25,
90                      -lane.getCenterLine().getFirst().y + lane.getLocation().y - 0.25, 0.5, 0.5);
91              Shape endCircle = new Ellipse2D.Double(lane.getCenterLine().getLast().x - lane.getLocation().x - 0.25,
92                      -lane.getCenterLine().getLast().y + lane.getLocation().y - 0.25, 0.5, 0.5);
93              graphics.setColor(Color.BLUE);
94              graphics.fill(startCircle);
95              graphics.setColor(Color.RED);
96              graphics.fill(endCircle);
97          }
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public final void destroy() throws NamingException
103     {
104         super.destroy();
105         this.text.destroy();
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     @SuppressWarnings("checkstyle:designforextension")
111     public ClonableRenderable2DInterface<Lane> clone(final Lane newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
112             throws NamingException, RemoteException
113     {
114         // the constructor also constructs the corresponding Text object
115         return new LaneAnimation(newSource, newSimulator, this.color, this.drawCenterLine);
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public final String toString()
121     {
122         return "LaneAnimation [lane = " + getSource().toString() + ", color=" + this.color + ", drawCenterLine="
123                 + this.drawCenterLine + "]";
124     }
125 
126     /**
127      * Text animation for the Node. Separate class to be able to turn it on and off...
128      * <p>
129      * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
130      * <br>
131      * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
132      * </p>
133      * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
134      * initial version Dec 11, 2016 <br>
135      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
136      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
137      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
138      */
139     public class Text extends TextAnimation
140     {
141         /** */
142         private static final long serialVersionUID = 20161211L;
143 
144         /**
145          * @param source Locatable; the object for which the text is displayed
146          * @param text String; the text to display
147          * @param dx float; the horizontal movement of the text, in meters
148          * @param dy float; the vertical movement of the text, in meters
149          * @param textPlacement TextAlignment; where to place the text
150          * @param color Color; the color of the text
151          * @param simulator SimulatorInterface.TimeDoubleUnit; the simulator
152          * @throws NamingException when animation context cannot be created or retrieved
153          * @throws RemoteException - when remote context cannot be found
154          */
155         public Text(final Locatable source, final String text, final float dx, final float dy,
156                 final TextAlignment textPlacement, final Color color, final SimulatorInterface.TimeDoubleUnit simulator)
157                 throws RemoteException, NamingException
158         {
159             super(source, text, dx, dy, textPlacement, color, simulator);
160         }
161 
162         /** {@inheritDoc} */
163         @Override
164         @SuppressWarnings("checkstyle:designforextension")
165         public DirectedPoint getLocation() throws RemoteException
166         {
167             // draw always on top.
168             DirectedPoint p = ((Lane) getSource()).getCenterLine().getLocationFractionExtended(0.5);
169             double a = Angle.normalizePi(p.getRotZ());
170             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
171             {
172                 a += Math.PI;
173             }
174             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
175         }
176 
177         /** {@inheritDoc} */
178         @Override
179         @SuppressWarnings("checkstyle:designforextension")
180         public TextAnimation clone(final Locatable newSource, final SimulatorInterface.TimeDoubleUnit newSimulator)
181                 throws RemoteException, NamingException
182         {
183             return new Text(newSource, getText(), getDx(), getDy(), getTextAlignment(), getColor(), newSimulator);
184         }
185 
186         /** {@inheritDoc} */
187         @Override
188         public final String toString()
189         {
190             return "Text []";
191         }
192 
193     }
194 
195 }