1 package org.opentrafficsim.draw.road;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.geom.Path2D;
7 import java.awt.image.ImageObserver;
8 import java.rmi.RemoteException;
9 import java.util.Set;
10 import java.util.function.Supplier;
11
12 import javax.naming.NamingException;
13
14 import org.djutils.base.Identifiable;
15 import org.djutils.draw.line.PolyLine2d;
16 import org.djutils.draw.point.OrientedPoint2d;
17 import org.opentrafficsim.base.geometry.OtsRenderable;
18 import org.opentrafficsim.draw.DrawLevel;
19 import org.opentrafficsim.draw.PaintPolygons;
20 import org.opentrafficsim.draw.TextAlignment;
21 import org.opentrafficsim.draw.TextAnimation;
22 import org.opentrafficsim.draw.road.TrafficLightDetectorAnimation.TrafficLightDetectorData;
23
24 import nl.tudelft.simulation.naming.context.Contextualized;
25
26 /**
27 * Traffic light detector animation.
28 * <p>
29 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.<br>
30 * All rights reserved. <br>
31 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
32 * </p>
33 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
34 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
35 */
36 public class TrafficLightDetectorAnimation extends OtsRenderable<TrafficLightDetectorData>
37 {
38 /** */
39 private static final long serialVersionUID = 20150130L;
40
41 /** The traffic light detector. */
42 private final TrafficLightDetectorData detector;
43
44 /** Path of the detector. */
45 private final Set<Path2D.Float> paths;
46
47 /** the Text object to destroy when the animation is destroyed. */
48 private final Text text;
49
50 /**
51 * Construct a TrafficLightDetectorAnimation.
52 * @param detector TrafficLightSensor; the traffic light detector that will be animated
53 * @param contextualized Contextualized; context provider
54 * @throws NamingException in case of registration failure of the animation
55 * @throws RemoteException in case of remote registration failure of the animation
56 */
57 public TrafficLightDetectorAnimation(final TrafficLightDetectorData detector, final Contextualized contextualized)
58 throws NamingException, RemoteException
59 {
60 super(detector, contextualized);
61 this.detector = detector;
62 this.paths = PaintPolygons.getPaths(this.detector.getGeometry().getPointList());
63 this.text = new Text(detector, detector::getId, 0.0f, 0.5f + 0.2f, TextAlignment.CENTER, Color.BLACK, contextualized);
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 public final void paint(final Graphics2D graphics, final ImageObserver observer)
69 {
70 graphics.setStroke(new BasicStroke(0.2f));
71 setRendering(graphics);
72 PaintPolygons.paintPaths(graphics, this.detector.getOccupancy() ? Color.BLUE : Color.BLACK, this.paths, false);
73 resetRendering(graphics);
74 }
75
76 /** {@inheritDoc} */
77 @Override
78 public void destroy(final Contextualized contextProvider)
79 {
80 super.destroy(contextProvider);
81 this.text.destroy(contextProvider);
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public final String toString()
87 {
88 return "TrafficLightDetectorAnimation [getSource()=" + this.getSource() + "]";
89 }
90
91 /**
92 * Text animation for the Detector. Separate class to be able to turn it on and off...
93 * <p>
94 * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
95 * <br>
96 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
97 * </p>
98 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
99 * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
100 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
101 */
102 public class Text extends TextAnimation<TrafficLightDetectorData, Text> implements DetectorData.Text
103 {
104 /** */
105 private static final long serialVersionUID = 20161211L;
106
107 /**
108 * @param source TrafficLightDetectorData; the object for which the text is displayed
109 * @param text Supplier<String>; the text to display
110 * @param dx float; the horizontal movement of the text, in meters
111 * @param dy float; the vertical movement of the text, in meters
112 * @param textPlacement TextAlignment; where to place the text
113 * @param color Color; the color of the text
114 * @param contextualized Contextualized; context provider
115 * @throws NamingException when animation context cannot be created or retrieved
116 * @throws RemoteException - when remote context cannot be found
117 */
118 public Text(final TrafficLightDetectorData source, final Supplier<String> text, final float dx, final float dy,
119 final TextAlignment textPlacement, final Color color, final Contextualized contextualized)
120 throws RemoteException, NamingException
121 {
122 super(source, text, dx, dy, textPlacement, color, contextualized, TextAnimation.RENDERWHEN10);
123 }
124
125 /** {@inheritDoc} */
126 @Override
127 public final String toString()
128 {
129 return "Text []";
130 }
131 }
132
133 /**
134 * TrafficLightDetectorData provides the information required to draw a traffic light detector.
135 * <p>
136 * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
137 * <br>
138 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
139 * </p>
140 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
141 */
142 public interface TrafficLightDetectorData extends DetectorData, Identifiable
143 {
144 /**
145 * Returns the geometry.
146 * @return PolyLine2d; geometry.
147 */
148 PolyLine2d getGeometry();
149
150 /**
151 * Returns whether the detector is occupied.
152 * @return boolean; whether the detector is occupied.
153 */
154 boolean getOccupancy();
155
156 /** {@inheritDoc} */
157 @Override
158 OrientedPoint2d getLocation();
159
160 /** {@inheritDoc} */
161 @Override
162 default double getZ()
163 {
164 return DrawLevel.OBJECT.getZ();
165 }
166 }
167
168 }