GtuGeneratorPositionAnimation.java
package org.opentrafficsim.animation.road;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.image.ImageObserver;
import org.djutils.draw.point.Point2d;
import org.opentrafficsim.animation.DrawLevel;
import org.opentrafficsim.animation.OtsRenderable;
import org.opentrafficsim.animation.RenderableTextSource;
import org.opentrafficsim.animation.TextAlignment;
import org.opentrafficsim.animation.road.GtuGeneratorPositionAnimation.GtuGeneratorPositionData;
import org.opentrafficsim.base.geometry.OtsShape;
import nl.tudelft.simulation.naming.context.Contextualized;
/**
* Animates a GtuGeneratorPosition.
* <p>
* Copyright (c) 2022-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
* BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
* </p>
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class GtuGeneratorPositionAnimation extends OtsRenderable<GtuGeneratorPositionData>
{
/** Chevron path to draw. */
private static final Path2D.Float PATH;
static
{
PATH = new Path2D.Float();
addChevron(PATH, 0);
addChevron(PATH, 1);
addChevron(PATH, 2);
}
/**
* Add chevron to drawing path.
* @param path Path2D.Float; path.
* @param number number of the chevron.
*/
private static void addChevron(final Path2D.Float path, final int number)
{
float x = number * 1.5f;
path.moveTo(x, -1.0);
path.lineTo(x + 1.0, 0.0);
path.lineTo(x, 1.0);
path.lineTo(x + 0.75, 1.0);
path.lineTo(x + 1.75, 0.0);
path.lineTo(x + 0.75, -1.0);
path.lineTo(x, -1.0);
}
/** Queue text. */
private final Queue queue;
/**
* Constructor.
* @param source source.
* @param contextProvider simulator.
*/
public GtuGeneratorPositionAnimation(final GtuGeneratorPositionData source, final Contextualized contextProvider)
{
super(source, contextProvider);
this.queue = new Queue(source, contextProvider);
}
@Override
public synchronized void destroy(final Contextualized contextProvider)
{
super.destroy(contextProvider);
this.queue.destroy(contextProvider);
}
@Override
public void paint(final Graphics2D graphics, final ImageObserver observer)
{
graphics.setColor(Color.BLUE);
setRendering(graphics);
graphics.fill(PATH);
resetRendering(graphics);
}
/**
* Paints a queue counter with a GtuGeneratorPosition.
*/
public static class Queue extends RenderableTextSource<GtuGeneratorPositionData, Queue>
{
/**
* Constructor.
* @param source source.
* @param contextualized context provider
*/
public Queue(final GtuGeneratorPositionData source, final Contextualized contextualized)
{
super(source, () -> Integer.toString(source.getQueueCount()), 0.0f, 0.0f, TextAlignment.CENTER, Color.BLACK, 3.0f,
12.0f, 50f, contextualized, null, RenderableTextSource.RENDERWHEN10);
}
}
/**
* GtuGeneratorPositionData provides the information required to draw a GTU generator position.
*/
public interface GtuGeneratorPositionData extends OtsShape
{
/**
* Returns the queue count.
* @return queue count.
*/
int getQueueCount();
@Override
default double signedDistance(final Point2d point)
{
return getLocation().distance(point);
}
@Override
default double getZ()
{
return DrawLevel.OBJECT.getZ();
}
}
}