1 package org.opentrafficsim.road.network.animation;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.geom.AffineTransform;
7 import java.awt.geom.Rectangle2D;
8 import java.awt.image.ImageObserver;
9 import java.io.Serializable;
10 import java.rmi.RemoteException;
11
12 import javax.naming.NamingException;
13
14 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
15 import org.opentrafficsim.core.network.animation.PaintPolygons;
16 import org.opentrafficsim.road.network.lane.conflict.Conflict;
17 import org.opentrafficsim.road.network.lane.conflict.ConflictType;
18
19 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
20
21
22
23
24
25
26
27
28
29
30
31 public class ConflictAnimation extends Renderable2D implements Serializable
32 {
33
34
35
36 private static final long serialVersionUID = 20161207L;
37
38
39 private final double halfWidth;
40
41
42
43
44
45
46
47 public ConflictAnimation(final Conflict source, final OTSSimulatorInterface simulator)
48 throws NamingException, RemoteException
49 {
50 super(source, simulator);
51 this.halfWidth = 0.45 * source.getLane().getWidth(source.getLongitudinalPosition()).getSI();
52 }
53
54
55 @Override
56 public final void paint(final Graphics2D graphics, final ImageObserver observer) throws RemoteException
57 {
58 Conflict conflict = (Conflict) this.getSource();
59 Color fillColor;
60 switch (conflict.getConflictRule())
61 {
62 case SPLIT:
63 fillColor = Color.blue;
64 break;
65
66 case PRIORITY:
67 fillColor = Color.green;
68 break;
69
70 case GIVE_WAY:
71 fillColor = Color.orange;
72 break;
73
74 default:
75
76 fillColor = Color.red;
77 break;
78 }
79
80 graphics.setColor(fillColor);
81 Rectangle2D rectangle = new Rectangle2D.Double(-0.25, -this.halfWidth, 0.5, 2 * this.halfWidth);
82 graphics.fill(rectangle);
83
84 BasicStroke stroke;
85 float factor = conflict.isPermitted() ? .5f : 1f;
86 if (conflict.getConflictType().equals(ConflictType.CROSSING))
87 {
88 stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
89 new float[] { factor * 1.0f, factor * 2.0f }, 0.0f);
90 }
91 else
92 {
93 stroke = new BasicStroke(.1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1.0f,
94 new float[] { factor * 1.0f, factor * 0.95f, factor * 0.1f, factor * 0.95f }, 0.0f);
95 }
96 graphics.setStroke(stroke);
97 AffineTransform saveAT = graphics.getTransform();
98 double angle = -getSource().getLocation().getRotZ();
99 if (isRotate() && angle != 0.0)
100 {
101 graphics.rotate(-angle);
102 }
103 if (conflict.getGeometry() != null)
104 {
105 PaintPolygons.paintMultiPolygon(graphics, fillColor, conflict.getLocation(), conflict.getGeometry(), false);
106 }
107 graphics.setTransform(saveAT);
108
109 }
110
111
112 @Override
113 public final String toString()
114 {
115 return "ConflictAnimation [getSource()=" + getSource() + "]";
116 }
117
118 }