1 package org.opentrafficsim.road.gtu.animation;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Graphics2D;
6 import java.awt.geom.Ellipse2D;
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 nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
15
16 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
17 import org.opentrafficsim.core.gtu.animation.GTUColorer;
18 import org.opentrafficsim.core.gtu.animation.IDGTUColorer;
19 import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class DefaultCarAnimation extends Renderable2D implements Serializable
33 {
34
35 private static final long serialVersionUID = 20150000L;
36
37
38 private GTUColorer gtuColorer;
39
40
41
42
43
44
45
46
47 public DefaultCarAnimation(final LaneBasedIndividualGTU source, final OTSSimulatorInterface simulator)
48 throws NamingException, RemoteException
49 {
50 this(source, simulator, null);
51 }
52
53
54
55
56
57
58
59
60
61 public DefaultCarAnimation(final LaneBasedIndividualGTU source, final OTSSimulatorInterface simulator,
62 final GTUColorer gtuColorer) throws NamingException, RemoteException
63 {
64 super(source, simulator);
65 if (null == gtuColorer)
66 {
67 this.gtuColorer = new IDGTUColorer();
68 }
69 else
70 {
71 this.gtuColorer = gtuColorer;
72 }
73 }
74
75
76
77
78
79 public final void setGTUColorer(final GTUColorer newGTUColorer)
80 {
81 this.gtuColorer = newGTUColorer;
82 }
83
84
85 @Override
86 public final void paint(final Graphics2D graphics, final ImageObserver observer)
87 {
88 final LaneBasedIndividualGTU car = (LaneBasedIndividualGTU) getSource();
89
90 if (car.isDestroyed())
91 {
92 try
93 {
94
95 return;
96 }
97 catch (Exception e)
98 {
99 System.err.println("Problem with GTU.destroy(): " + car.toString());
100
101 return;
102 }
103 }
104
105 final double length = car.getLength().getSI();
106 final double l2 = length / 2;
107 final double width = car.getWidth().getSI();
108 final double w2 = width / 2;
109 final double w4 = width / 4;
110 graphics.setColor(this.gtuColorer.getColor(car));
111 BasicStroke saveStroke = (BasicStroke) graphics.getStroke();
112 graphics.setStroke(new BasicStroke(0));
113 Rectangle2D rectangle = new Rectangle2D.Double(-l2, -w2, length, width);
114 graphics.draw(rectangle);
115 graphics.fill(rectangle);
116
117 graphics.setColor(Color.WHITE);
118 Ellipse2D.Double frontIndicator = new Ellipse2D.Double(l2 - w2 - w4, -w4, w2, w2);
119 graphics.draw(frontIndicator);
120 graphics.fill(frontIndicator);
121
122 graphics.setColor(Color.YELLOW);
123 if (car.getTurnIndicatorStatus() != null && car.getTurnIndicatorStatus().isLeftOrBoth())
124 {
125 Rectangle2D.Double leftIndicator = new Rectangle2D.Double(l2 - w4, -w2, w4, w4);
126 graphics.fill(leftIndicator);
127 }
128
129 if (car.getTurnIndicatorStatus() != null && car.getTurnIndicatorStatus().isRightOrBoth())
130 {
131 Rectangle2D.Double rightIndicator = new Rectangle2D.Double(l2 - w4, w2 - w4, w4, w4);
132 graphics.fill(rightIndicator);
133 }
134
135 graphics.setColor(Color.RED);
136 if (car.getAcceleration().si < -0.5)
137 {
138 Rectangle2D.Double leftBrake = new Rectangle2D.Double(-l2, w2 - w4, w4, w4);
139 Rectangle2D.Double rightBrake = new Rectangle2D.Double(-l2, -w2, w4, w4);
140 graphics.setColor(Color.RED);
141 graphics.fill(leftBrake);
142 graphics.fill(rightBrake);
143 }
144 graphics.setStroke(saveStroke);
145 }
146
147
148 @Override
149 public final String toString()
150 {
151 return this.getSource().toString();
152 }
153
154 }