View Javadoc
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   * Wrap a DSOL simulation model, or any (descendant of a) JPanel in a JFrame (wrap it in a window). The window will be
23   * maximized.
24   * <p>
25   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
26   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
27   * </p>
28   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
30   * @param <T> model type
31   */
32  public class OtsSwingApplication<T extends OtsModelInterface> extends AppearanceApplication
33  {
34      /** */
35      private static final long serialVersionUID = 20141216L;
36  
37      /** the model. */
38      private final T model;
39  
40      /** whether the application has been closed or not. */
41      @SuppressWarnings("checkstyle:visibilitymodifier")
42      protected boolean closed = false;
43  
44      /** Default GTU colorers. */
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       * Wrap an OtsModel in a JFrame. Uses a default GTU colorer.
51       * @param model the model that will be shown in the JFrame
52       * @param panel this should be the JPanel of the simulation
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       * Return the initial 'home' extent for the animation. The 'Home' button returns to this extent. Override this method when a
77       * smaller or larger part of the infra should be shown. In the default setting, all currently visible objects are shown.
78       * @return the initial and 'home' rectangle for the animation.
79       */
80      @SuppressWarnings("checkstyle:designforextension")
81      protected Rectangle2D makeAnimationRectangle()
82      {
83          return this.model.getNetwork().getExtent();
84      }
85  
86      /**
87       * @param exitOnClose set exitOnClose
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      * @return closed
103      */
104     public final boolean isClosed()
105     {
106         return this.closed;
107     }
108 
109     /**
110      * @return model
111      */
112     public final T getModel()
113     {
114         return this.model;
115     }
116 
117 }