1 package org.opentrafficsim.swing.gui;
2
3 import java.awt.Frame;
4 import java.awt.event.WindowAdapter;
5 import java.awt.event.WindowEvent;
6 import java.awt.geom.Rectangle2D;
7 import java.util.List;
8
9 import javax.swing.JPanel;
10 import javax.swing.WindowConstants;
11
12 import org.djunits.unit.SpeedUnit;
13 import org.djunits.value.vdouble.scalar.Acceleration;
14 import org.djunits.value.vdouble.scalar.Speed;
15 import org.opentrafficsim.animation.gtu.colorer.AccelerationGtuColorer;
16 import org.opentrafficsim.animation.gtu.colorer.GtuColorer;
17 import org.opentrafficsim.animation.gtu.colorer.IdGtuColorer;
18 import org.opentrafficsim.animation.gtu.colorer.SpeedGtuColorer;
19 import org.opentrafficsim.core.dsol.OtsModelInterface;
20
21
22
23
24
25
26
27
28
29
30
31
32 public class OtsSwingApplication<T extends OtsModelInterface> extends AppearanceApplication
33 {
34
35 private static final long serialVersionUID = 20141216L;
36
37
38 private final T model;
39
40
41 @SuppressWarnings("checkstyle:visibilitymodifier")
42 protected boolean closed = false;
43
44
45 public static final List<GtuColorer> DEFAULT_GTU_COLORERS =
46 List.of(new IdGtuColorer(), new SpeedGtuColorer(new Speed(150, SpeedUnit.KM_PER_HOUR)),
47 new AccelerationGtuColorer(Acceleration.instantiateSI(-6.0), Acceleration.instantiateSI(2)));
48
49
50
51
52
53
54 public OtsSwingApplication(final T model, final JPanel panel)
55 {
56 super(panel);
57 this.model = model;
58 setTitle("OTS | The Open Traffic Simulator | " + model.getDescription());
59 pack();
60 setExtendedState(Frame.MAXIMIZED_BOTH);
61 setVisible(true);
62
63 setExitOnClose(true);
64 addWindowListener(new WindowAdapter()
65 {
66 @Override
67 public void windowClosing(final WindowEvent windowEvent)
68 {
69 OtsSwingApplication.this.closed = true;
70 super.windowClosing(windowEvent);
71 }
72 });
73 }
74
75
76
77
78
79
80 @SuppressWarnings("checkstyle:designforextension")
81 protected Rectangle2D makeAnimationRectangle()
82 {
83 return this.model.getNetwork().getExtent();
84 }
85
86
87
88
89 public final void setExitOnClose(final boolean exitOnClose)
90 {
91 if (exitOnClose)
92 {
93 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
94 }
95 else
96 {
97 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
98 }
99 }
100
101
102
103
104 public final boolean isClosed()
105 {
106 return this.closed;
107 }
108
109
110
111
112 public final T getModel()
113 {
114 return this.model;
115 }
116
117 }