View Javadoc
1   package nl.tudelft.simulation.dsol.swing.animation.d2;
2   
3   import java.rmi.RemoteException;
4   
5   import org.djutils.draw.bounds.Bounds2d;
6   import org.djutils.event.Event;
7   import org.djutils.exceptions.Throw;
8   
9   import nl.tudelft.simulation.dsol.experiment.Replication;
10  import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
11  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12  import nl.tudelft.simulation.language.DsolException;
13  import nl.tudelft.simulation.naming.context.ContextInterface;
14  import nl.tudelft.simulation.naming.context.util.ContextUtil;
15  
16  /**
17   * The AnimationPanel to display animated (Locatable) objects as an extension of the VisualizationPanel. The difference is that
18   * the AnimationPanel is Simulator and Replication aware. When a new replication starts, a context for that replication is
19   * created, and Renderable objects for that replication are stored in a subcontext that is specific for that replication. This
20   * means that even when multiple replications run in parallel, animations can be stored internally and the user could
21   * theoretically shift between the different animations (or show multiple animations at once).
22   * <p>
23   * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
24   * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
25   * project is distributed under a three-clause BSD-style license, which can be found at
26   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
27   * </p>
28   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
29   * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
30   */
31  public class AnimationPanel extends VisualizationPanel
32  {
33      /** */
34      private static final long serialVersionUID = 1L;
35  
36      /** the simulator. */
37      private SimulatorInterface<?> simulator;
38  
39      /**
40       * constructs a new AnimationPanel.
41       * @param homeExtent Bounds2d; the home (initial) extent of the panel
42       * @param simulator SimulatorInterface&lt;?, ?, ?&gt;; the simulator of which we want to know the events for animation
43       * @throws RemoteException on network error for one of the listeners
44       * @throws DsolException when the simulator is not implementing the AnimatorInterface
45       */
46      public AnimationPanel(final Bounds2d homeExtent, final SimulatorInterface<?> simulator)
47              throws RemoteException, DsolException
48      {
49          super(homeExtent, simulator);
50          Throw.when(!(simulator instanceof AnimatorInterface), DsolException.class,
51                  "Simulator must implement the AnimatorInterface");
52          this.simulator = simulator;
53          simulator.addListener(this, Replication.START_REPLICATION_EVENT);
54      }
55  
56      @Override
57      public void notify(final Event event) throws RemoteException
58      {
59          super.notify(event);
60  
61          if (event.getType().equals(Replication.START_REPLICATION_EVENT))
62          {
63              synchronized (this.elementList)
64              {
65                  this.elements.clear();
66                  try
67                  {
68                      if (this.context != null)
69                      {
70                          this.context.removeListener(this, ContextInterface.OBJECT_ADDED_EVENT);
71                          this.context.removeListener(this, ContextInterface.OBJECT_REMOVED_EVENT);
72                      }
73                      this.context =
74                              ContextUtil.lookupOrCreateSubContext(this.simulator.getReplication().getContext(), "animation/2D");
75                      subscribeToContext();
76                  }
77                  catch (Exception exception)
78                  {
79                      this.simulator.getLogger().always().warn(exception, "notify");
80                  }
81              }
82          }
83      }
84  
85  }