1 package org.opentrafficsim.road.network.animation;
2
3 import java.awt.Color;
4 import java.awt.Font;
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 org.djunits.unit.SpeedUnit;
15 import org.opentrafficsim.road.network.lane.object.SpeedSign;
16
17 import nl.tudelft.simulation.dsol.animation.D2.Renderable2D;
18 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
19
20
21
22
23
24
25
26
27
28
29
30
31 public class SpeedSignAnimation extends Renderable2D<SpeedSign> implements Serializable
32 {
33
34
35 private static final long serialVersionUID = 20170420L;
36
37
38 private static final double RADIUS = 1.6;
39
40
41 private static final double EDGE = 1.3;
42
43
44
45
46
47
48
49 public SpeedSignAnimation(final SpeedSign source, final SimulatorInterface.TimeDoubleUnit simulator)
50 throws NamingException, RemoteException
51 {
52 super(source, simulator);
53 setRotate(false);
54 }
55
56
57 @Override
58 public final void paint(final Graphics2D g, final ImageObserver arg1) throws RemoteException
59 {
60 Ellipse2D ellipse = new Ellipse2D.Double(-RADIUS, -RADIUS, 2 * RADIUS, 2 * RADIUS);
61 g.setColor(Color.RED);
62 g.fill(ellipse);
63 ellipse = new Ellipse2D.Double(-EDGE, -EDGE, 2 * EDGE, 2 * EDGE);
64 g.setColor(Color.WHITE);
65 g.fill(ellipse);
66 g.setColor(Color.BLACK);
67 int speed = (int) getSource().getSpeed().getInUnit(SpeedUnit.KM_PER_HOUR);
68 if (speed < 100)
69 {
70 g.setFont(new Font("Arial", 0, -1).deriveFont(2.0f));
71 }
72 else
73 {
74 g.setFont(new Font("Arial narrow", 0, -1).deriveFont(1.85f));
75 }
76 String str = Integer.toString(speed);
77 Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(str, g);
78 g.drawString(str, (float) -stringBounds.getCenterX(), (float) -stringBounds.getCenterY());
79 }
80
81
82 @Override
83 public final String toString()
84 {
85 return "SpeedSignAnimation";
86 }
87
88 }