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-2024 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/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
25 * project is distributed under a three-clause BSD-style license, which can be found at
26 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
27 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
28 * </p>
29 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
30 * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>
31 */
32 public class AnimationPanel extends VisualizationPanel
33 {
34 /** */
35 private static final long serialVersionUID = 1L;
36
37 /** the simulator. */
38 private SimulatorInterface<?> simulator;
39
40 /**
41 * constructs a new AnimationPanel.
42 * @param homeExtent Bounds2d; the home (initial) extent of the panel
43 * @param simulator SimulatorInterface<?, ?, ?>; the simulator of which we want to know the events for animation
44 * @throws RemoteException on network error for one of the listeners
45 * @throws DsolException when the simulator is not implementing the AnimatorInterface
46 */
47 public AnimationPanel(final Bounds2d homeExtent, final SimulatorInterface<?> simulator)
48 throws RemoteException, DsolException
49 {
50 super(homeExtent, simulator);
51 Throw.when(!(simulator instanceof AnimatorInterface), DsolException.class,
52 "Simulator must implement the AnimatorInterface");
53 this.simulator = simulator;
54 simulator.addListener(this, Replication.START_REPLICATION_EVENT);
55 }
56
57 /** {@inheritDoc} */
58 @Override
59 public void notify(final Event event) throws RemoteException
60 {
61 super.notify(event);
62
63 if (event.getType().equals(Replication.START_REPLICATION_EVENT))
64 {
65 synchronized (this.elementList)
66 {
67 this.elements.clear();
68 try
69 {
70 if (this.context != null)
71 {
72 this.context.removeListener(this, ContextInterface.OBJECT_ADDED_EVENT);
73 this.context.removeListener(this, ContextInterface.OBJECT_REMOVED_EVENT);
74 }
75 this.context =
76 ContextUtil.lookupOrCreateSubContext(this.simulator.getReplication().getContext(), "animation/2D");
77 subscribeToContext();
78 }
79 catch (Exception exception)
80 {
81 this.simulator.getLogger().always().warn(exception, "notify");
82 }
83 }
84 }
85 }
86
87 }