View Javadoc
1   package org.opentrafficsim.draw.road;
2   
3   import java.awt.Color;
4   import java.awt.Graphics2D;
5   import java.awt.geom.Path2D;
6   import java.awt.image.ImageObserver;
7   import java.rmi.RemoteException;
8   
9   import javax.naming.NamingException;
10  
11  import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
12  import org.opentrafficsim.core.geometry.DirectedPoint;
13  import org.opentrafficsim.core.gtu.GtuGenerator.GtuGeneratorPosition;
14  import org.opentrafficsim.draw.core.TextAlignment;
15  import org.opentrafficsim.draw.core.TextAnimation;
16  
17  import nl.tudelft.simulation.dsol.animation.Locatable;
18  import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
19  import nl.tudelft.simulation.language.d2.Angle;
20  
21  /**
22   * Animates a GtuGeneratorPosition.
23   * <p>
24   * Copyright (c) 2022-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
29   * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
30   */
31  public class GtuGeneratorPositionAnimation extends Renderable2D<GtuGeneratorPosition>
32  {
33  
34      /** */
35      private static final long serialVersionUID = 20230204L;
36      
37      /** Chevron path to draw. */
38      private static final Path2D.Float PATH;
39  
40      static
41      {
42          PATH = new Path2D.Float();
43          addChevron(PATH, 0);
44          addChevron(PATH, 1);
45          addChevron(PATH, 2);
46      }
47  
48      /**
49       * Add chevron to drawing path.
50       * @param path Path2D.Float; path.
51       * @param number int; number of the chevron.
52       */
53      private static void addChevron(final Path2D.Float path, final int number)
54      {
55          float x = number * 1.5f;
56          path.moveTo(x, -1.0);
57          path.lineTo(x + 1.0, 0.0);
58          path.lineTo(x, 1.0);
59          path.lineTo(x + 0.75, 1.0);
60          path.lineTo(x + 1.75, 0.0);
61          path.lineTo(x + 0.75, -1.0);
62          path.lineTo(x, -1.0);
63      }
64  
65      /**
66       * Constructor.
67       * @param source GtuGeneratorPosition; source.
68       * @param contextProvider OtsSimulatorInterface; simulator.
69       * @throws NamingException when animation context cannot be created or retrieved
70       * @throws RemoteException when remote context cannot be found
71       */
72      public GtuGeneratorPositionAnimation(final GtuGeneratorPosition source, final OtsSimulatorInterface contextProvider)
73              throws RemoteException, NamingException
74      {
75          super(source, contextProvider);
76          new Queue(source, contextProvider);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public void paint(final Graphics2D graphics, final ImageObserver observer)
82      {
83          graphics.setColor(Color.BLUE);
84          graphics.fill(PATH);
85      }
86  
87      /**
88       * Paints a queue counter with a GtuGeneratorPosition.
89       * <p>
90       * Copyright (c) 2022-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
91       * <br>
92       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
93       * </p>
94       * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
95       * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
96       * @author <a href="https://dittlab.tudelft.nl">Wouter Schakel</a>
97       */
98      public class Queue extends TextAnimation
99      {
100         /** */
101         private static final long serialVersionUID = 20230204L;
102 
103         /**
104          * Constructor.
105          * @param source Locatable; source.
106          * @param simulator OtsSimulatorInterface; simulator.
107          * @throws NamingException when animation context cannot be created or retrieved
108          * @throws RemoteException when remote context cannot be found
109          */
110         public Queue(final Locatable source, final OtsSimulatorInterface simulator) throws RemoteException, NamingException
111         {
112             super(source, "", 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, 3.0f, 12.0f, 50f, simulator, null,
113                     TextAnimation.RENDERALWAYS);
114         }
115         
116         /** {@inheritDoc} */
117         @Override
118         @SuppressWarnings("checkstyle:designforextension")
119         public DirectedPoint getLocation()
120         {
121             // draw always on top, and not upside down.
122             DirectedPoint p = super.getLocation();
123             double a = Angle.normalizePi(p.getRotZ());
124             if (a > Math.PI / 2.0 || a < -0.99 * Math.PI / 2.0)
125             {
126                 a += Math.PI;
127             }
128             return new DirectedPoint(p.x, p.y, Double.MAX_VALUE, 0.0, 0.0, a);
129         }
130 
131         /** {@inheritDoc} */
132         @Override
133         public void paint(final Graphics2D graphics, final ImageObserver observer)
134         {
135             setText(Integer.toString(((GtuGeneratorPosition) getSource()).getQueueCount()));
136             super.paint(graphics, observer);
137         }
138     }
139 
140 }