View Javadoc
1   package org.opentrafficsim.animation.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.animation.DrawLevel;
10  import org.opentrafficsim.animation.OtsRenderable;
11  import org.opentrafficsim.animation.RenderableTextSource;
12  import org.opentrafficsim.animation.TextAlignment;
13  import org.opentrafficsim.animation.road.GtuGeneratorPositionAnimation.GtuGeneratorPositionData;
14  import org.opentrafficsim.base.geometry.OtsShape;
15  
16  import nl.tudelft.simulation.naming.context.Contextualized;
17  
18  /**
19   * Animates a GtuGeneratorPosition.
20   * <p>
21   * Copyright (c) 2022-2026 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 Alexander Verbraeck
25   * @author Peter Knoppers
26   * @author Wouter Schakel
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      /** Queue text. */
60      private final Queue queue;
61  
62      /**
63       * Constructor.
64       * @param source source.
65       * @param contextProvider simulator.
66       */
67      public GtuGeneratorPositionAnimation(final GtuGeneratorPositionData source, final Contextualized contextProvider)
68      {
69          super(source, contextProvider);
70          this.queue = new Queue(source, contextProvider);
71      }
72  
73      @Override
74      public synchronized void destroy(final Contextualized contextProvider)
75      {
76          super.destroy(contextProvider);
77          this.queue.destroy(contextProvider);
78      }
79  
80      @Override
81      public void paint(final Graphics2D graphics, final ImageObserver observer)
82      {
83          graphics.setColor(Color.BLUE);
84          setRendering(graphics);
85          graphics.fill(PATH);
86          resetRendering(graphics);
87      }
88  
89      /**
90       * Paints a queue counter with a GtuGeneratorPosition.
91       */
92      public static class Queue extends RenderableTextSource<GtuGeneratorPositionData, Queue>
93      {
94          /**
95           * Constructor.
96           * @param source source.
97           * @param contextualized context provider
98           */
99          public Queue(final GtuGeneratorPositionData source, final Contextualized contextualized)
100         {
101             super(source, () -> Integer.toString(source.getQueueCount()), 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, 3.0f,
102                     12.0f, 50f, contextualized, null, RenderableTextSource.RENDERWHEN10);
103         }
104     }
105 
106     /**
107      * GtuGeneratorPositionData provides the information required to draw a GTU generator position.
108      */
109     public interface GtuGeneratorPositionData extends OtsShape
110     {
111         /**
112          * Returns the queue count.
113          * @return queue count.
114          */
115         int getQueueCount();
116 
117         @Override
118         default double signedDistance(final Point2d point)
119         {
120             return getLocation().distance(point);
121         }
122 
123         @Override
124         default double getZ()
125         {
126             return DrawLevel.OBJECT.getZ();
127         }
128     }
129 
130 }