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   
8   import org.djutils.draw.point.Point2d;
9   import org.opentrafficsim.base.geometry.OtsShape;
10  import org.opentrafficsim.draw.DrawLevel;
11  import org.opentrafficsim.draw.OtsRenderable;
12  import org.opentrafficsim.draw.RenderableTextSource;
13  import org.opentrafficsim.draw.TextAlignment;
14  import org.opentrafficsim.draw.road.GtuGeneratorPositionAnimation.GtuGeneratorPositionData;
15  
16  import nl.tudelft.simulation.naming.context.Contextualized;
17  
18  /**
19   * Animates a GtuGeneratorPosition.
20   * <p>
21   * Copyright (c) 2022-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
23   * </p>
24   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
26   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
27   */
28  public class GtuGeneratorPositionAnimation extends OtsRenderable<GtuGeneratorPositionData>
29  {
30  
31      /** Chevron path to draw. */
32      private static final Path2D.Float PATH;
33  
34      static
35      {
36          PATH = new Path2D.Float();
37          addChevron(PATH, 0);
38          addChevron(PATH, 1);
39          addChevron(PATH, 2);
40      }
41  
42      /**
43       * Add chevron to drawing path.
44       * @param path Path2D.Float; path.
45       * @param number number of the chevron.
46       */
47      private static void addChevron(final Path2D.Float path, final int number)
48      {
49          float x = number * 1.5f;
50          path.moveTo(x, -1.0);
51          path.lineTo(x + 1.0, 0.0);
52          path.lineTo(x, 1.0);
53          path.lineTo(x + 0.75, 1.0);
54          path.lineTo(x + 1.75, 0.0);
55          path.lineTo(x + 0.75, -1.0);
56          path.lineTo(x, -1.0);
57      }
58  
59      /**
60       * Constructor.
61       * @param source source.
62       * @param contextProvider simulator.
63       */
64      public GtuGeneratorPositionAnimation(final GtuGeneratorPositionData source, final Contextualized contextProvider)
65      {
66          super(source, contextProvider);
67          new Queue(source, contextProvider);
68      }
69  
70      @Override
71      public void paint(final Graphics2D graphics, final ImageObserver observer)
72      {
73          graphics.setColor(Color.BLUE);
74          setRendering(graphics);
75          graphics.fill(PATH);
76          resetRendering(graphics);
77      }
78  
79      /**
80       * Paints a queue counter with a GtuGeneratorPosition.
81       * <p>
82       * Copyright (c) 2022-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
83       * <br>
84       * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
85       * </p>
86       * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
87       * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
88       * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
89       */
90      public static class Queue extends RenderableTextSource<GtuGeneratorPositionData, Queue>
91      {
92          /**
93           * Constructor.
94           * @param source source.
95           * @param contextualized context provider
96           */
97          public Queue(final GtuGeneratorPositionData source, final Contextualized contextualized)
98          {
99              super(source, () -> Integer.toString(source.getQueueCount()), 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, 3.0f,
100                     12.0f, 50f, contextualized, null, RenderableTextSource.RENDERWHEN10);
101         }
102     }
103 
104     /**
105      * GtuGeneratorPositionData provides the information required to draw a GTU generator position.
106      * <p>
107      * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
108      * <br>
109      * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
110      * </p>
111      * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
112      */
113     public interface GtuGeneratorPositionData extends OtsShape
114     {
115         /**
116          * Returns the queue count.
117          * @return queue count.
118          */
119         int getQueueCount();
120 
121         @Override
122         default double signedDistance(final Point2d point)
123         {
124             return getLocation().distance(point);
125         }
126 
127         @Override
128         default double getZ()
129         {
130             return DrawLevel.OBJECT.getZ();
131         }
132     }
133 
134 }