View Javadoc
1   package org.opentrafficsim.swing.gui;
2   
3   import java.awt.Color;
4   import java.awt.Frame;
5   import java.awt.event.WindowAdapter;
6   import java.awt.event.WindowEvent;
7   import java.awt.geom.Rectangle2D;
8   import java.util.List;
9   
10  import javax.swing.JPanel;
11  import javax.swing.WindowConstants;
12  
13  import org.opentrafficsim.animation.gtu.colorer.AccelerationGtuColorer;
14  import org.opentrafficsim.animation.gtu.colorer.IdGtuColorer;
15  import org.opentrafficsim.animation.gtu.colorer.SpeedGtuColorer;
16  import org.opentrafficsim.core.dsol.OtsModelInterface;
17  import org.opentrafficsim.core.gtu.Gtu;
18  import org.opentrafficsim.draw.colorer.Colorer;
19  import org.opentrafficsim.draw.colorer.FixedColorer;
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<Colorer<? super Gtu>> DEFAULT_GTU_COLORERS = List.of(new FixedColorer<>(Color.BLUE, "Blue"),
46              new IdGtuColorer(), new SpeedGtuColorer(), new AccelerationGtuColorer());
47  
48      /**
49       * Wrap an OtsModel in a JFrame. Uses a default GTU colorer.
50       * @param model the model that will be shown in the JFrame
51       * @param panel this should be the JPanel of the simulation
52       */
53      public OtsSwingApplication(final T model, final JPanel panel)
54      {
55          super(panel);
56          this.model = model;
57          setTitle("OTS | The Open Traffic Simulator | " + model.getDescription());
58          pack();
59          setExtendedState(Frame.MAXIMIZED_BOTH);
60          setVisible(true);
61  
62          setExitOnClose(true);
63          addWindowListener(new WindowAdapter()
64          {
65              @Override
66              public void windowClosing(final WindowEvent windowEvent)
67              {
68                  OtsSwingApplication.this.closed = true;
69                  super.windowClosing(windowEvent);
70              }
71          });
72      }
73  
74      /**
75       * Return the initial 'home' extent for the animation. The 'Home' button returns to this extent. Override this method when a
76       * smaller or larger part of the infra should be shown. In the default setting, all currently visible objects are shown.
77       * @return the initial and 'home' rectangle for the animation.
78       */
79      @SuppressWarnings("checkstyle:designforextension")
80      protected Rectangle2D makeAnimationRectangle()
81      {
82          return this.model.getNetwork().getExtent();
83      }
84  
85      /**
86       * Set exit on close.
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 whether the application is closed.
103      * @return closed
104      */
105     public final boolean isClosed()
106     {
107         return this.closed;
108     }
109 
110     /**
111      * Return model.
112      * @return model
113      */
114     public final T getModel()
115     {
116         return this.model;
117     }
118 
119 }