madkit.simulation.viewer

## Class SwingViewer

- java.lang.Object
- - madkit.kernel.AbstractAgent
  - - madkit.kernel.Watcher
    - - madkit.simulation.viewer.SwingViewer

- All Implemented Interfaces:
    Comparable<AbstractAgent>

      public abstract class SwingViewer
      extends Watcher

  A very basic simulation viewer agent. This class defines a panel for the simulation rendering and two modes of rendering: Synchronous and asynchronous. The synchronous mode ensures that each simulation frame is displayed. That means that the scheduler will wait the end of the rendering activity to proceed to the next activator, waiting for the swing thread to ends. this is not the case with the asynchronous mode so that the whole simulation process goes faster because some simulation states will not be displayed. An `observe` method is already defined and is intended to be called by scheduler agents to trigger the rendering. This class could be thus extended to reuse the rendering call mechanism which is defined in here.
  Since:
    MaDKit 5.0.0.17
  Version:
    1.1
  Author:
    Fabien Michel

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class madkit.kernel.AbstractAgent

      `AbstractAgent.ReturnCode, AbstractAgent.State`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `SwingViewer()` Creates a new agent with a default panel for rendering purposes

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `JMenu` | `getDisplayMenu()` Returns a menu which could be used in any viewer GUI.
    `JComponent` | `getDisplayPane()`
    `JFrame` | `getFrame()` By default, get the default frame provided by MaDKit in `setupFrame(AgentFrame)` and set using `setupFrame(AgentFrame)`.
    `Action` | `getSynchroPaintingAction()`
    `JToolBar` | `getToolBar()` Returns the viewer's toolbar.
    `boolean` | `isRendering()`
    `boolean` | `isSynchronousPainting()` Tells if the rendering should be done synchronously or asynchronously with respect to simulation steps.
    `protected void` | `observe()` Intended to be invoked by a scheduler's activator for triggering the rendering.
    `protected abstract void` | `render(Graphics g)` Override this method to do the rendering in the agent's panel.
    `void` | `setDisplayPane(JComponent displayPane)` Could be used to define a customized panel instead of the default pane
    `void` | `setRendering(boolean activated)` Enable or disable the rendering activity
    `void` | `setRenderingInterval(int interval)` Set the number of states between display updates.
    `void` | `setSynchronousPainting(boolean synchronousPainting)` Set the rendering mode to synchronous or asynchronous.
    `void` | `setupFrame(AgentFrame frame)` Provides a default implementation that assigns the default panel to the default frame

    - ### Methods inherited from class madkit.kernel.Watcher

      `activate, addProbe, allProbes, getProbes, removeAllProbes, removeProbe, toString`

    - ### Methods inherited from class madkit.kernel.AbstractAgent

      `broadcastMessage, broadcastMessageWithRole, bucketModeCreateGroup, bucketModeRequestRole, checkAgentAddress, compareTo, createGroup, createGroup, createGroup, createGroupIfAbsent, createGroupIfAbsent, createGroupIfAbsent, createGUIOnStartUp, destroyCommunity, destroyGroup, destroyRole, end, executeThisAgent, executeThisAgent, executeThisAgent, getAgentAddressIn, getAgentsWithRole, getAgentsWithRole, getAgentWithRole, getDistantAgentWithRole, getExistingCommunities, getExistingGroups, getExistingRoles, getKernelAddress, getLastReceivedMessage, getLastReceivedMessage, getLogger, getMadkitConfig, getMadkitProperty, getMadkitProperty, getMyGroups, getMyRoles, getName, getNetworkID, getNextEventDate, getOrganizationSnapShot, getReplyTo, getServerInfo, getSimpleNetworkID, getSimulationTime, getState, hasGUI, hashCode, hasRole, isAlive, isCommunity, isGroup, isKernelOnline, isMadkitPropertyTrue, isMessageBoxEmpty, isRole, killAgent, killAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgent, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchAgentBucket, launchNode, launchXmlAgents, leaveGroup, leaveRole, nextMessage, nextMessage, nextMessages, proceedEnumMessage, purgeMailbox, receiveMessage, reload, requestRole, requestRole, sendMessage, sendMessage, sendMessageWithRole, sendMessageWithRole, sendReply, sendReplyWithRole, setLogLevel, setMadkitProperty, setMadkitProperty, setName`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### SwingViewer

          public SwingViewer()

      Creates a new agent with a default panel for rendering purposes

  - ### Method Detail

    - #### isRendering

          public boolean isRendering()

      Returns:
        `true` if the rendering activity is activated.

    - #### setRendering

          public void setRendering(boolean activated)

      Enable or disable the rendering activity

    - #### getDisplayPane

          public JComponent getDisplayPane()

      Returns:
        the current panel which is used for display

    - #### setDisplayPane

          public void setDisplayPane(JComponent displayPane)

      Could be used to define a customized panel instead of the default pane
      Parameters:
        `displayPane` - the displayPane to set

    - #### observe

          protected void observe()

      Intended to be invoked by a scheduler's activator for triggering the rendering. This method activate the rendering either synchronously or asynchronously depending on `isSynchronousPainting()`.

    - #### render

          protected abstract void render(Graphics g)

      Override this method to do the rendering in the agent's panel. This method is automatically called when the `observe` method is triggered by a Scheduler
      Parameters:
        `g` -

    - #### isSynchronousPainting

          public boolean isSynchronousPainting()

      Tells if the rendering should be done synchronously or asynchronously with respect to simulation steps.
      Returns:
        the synchronousPainting

    - #### setSynchronousPainting

          public void setSynchronousPainting(boolean synchronousPainting)

      Set the rendering mode to synchronous or asynchronous. Synchronous painting is done for each time step and the simulation does not advance until all the rendering is done for a step: The simulation is slower but more smoothly rendered, making the visualization of the simulation dynamics more precise. In asynchronous mode, the rendering is done in parallel with the simulation steps and thus only display snapshot of the simulation's state:
      Parameters:
        `synchronousPainting` - the synchronousPainting mode to set

    - #### setupFrame

          public void setupFrame(AgentFrame frame)

      Provides a default implementation that assigns the default panel to the default frame
      Overrides:
        `setupFrame` in class `AbstractAgent`
      Parameters:
        `frame` - the default frame which has been created by MaDKit for this agent.
      See Also:
        `AbstractAgent.setupFrame(AgentFrame)`

    - #### getDisplayMenu

          public JMenu getDisplayMenu()

      Returns a menu which could be used in any viewer GUI.
      Returns:
        a menu controlling the viewer's options

    - #### getFrame

          public JFrame getFrame()

      By default, get the default frame provided by MaDKit in `setupFrame(AgentFrame)` and set using `setupFrame(AgentFrame)`. It can be any `JFrame` if `setupFrame(AgentFrame)` has been overridden.
      Returns:
        the working frame

    - #### getSynchroPaintingAction

          public Action getSynchroPaintingAction()

    - #### setRenderingInterval

          public void setRenderingInterval(int interval)

      Set the number of states between display updates. If set to 1, every simulation states will be displayed
      Parameters:
        `interval` - an int > 0

    - #### getToolBar

          public JToolBar getToolBar()

      Returns the viewer's toolbar.
      Returns:
        a toolBar controlling the viewer's actions

Fabien Michel, Olivier Gutknecht, Jacques Ferber - February 17 2020