Class Simulation

java.lang.Object
org.opensourcephysics.ejs.Simulation
All Implemented Interfaces:
Runnable, javajs.async.SwingJSUtils.StateMachine

public abstract class Simulation extends Object implements Runnable, javajs.async.SwingJSUtils.StateMachine
A base interface for a simulation
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
     
    static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    apply user interface changes
    void
    apply(String _variable)
    apply a single change in the user interface
    void
    apply user interface changes.
    Returns the codebase
     
    This method returns a String with the value of a public variable of the model.
    getVariable(String _name, String _sep)
    This method returns a String with the value of a public variable of the model.
     
    void
    Initialize model using user interface changes
    boolean
    Returns whether the simulation is running or not
    boolean
    Returns whether the simulation is running or not
    void
    Stops the simulation
    void
    Sets the simulation in play mode
    boolean
    readState(String _filename)
     
    boolean
    readState(String _filename, URL _codebase)
    Reads the state of the model either from a file on the disk, from memory or from a url location.
    void
    Resets the simulation to a complete initial state
    void
    run()
    Implementation of the Runnable interface
    boolean
    saveState(String _filename)
    Saves the state of the model either to a file on the disk or to memory.
    void
    setAutoplay(boolean _play)
    Sets whether the simulation should be set to play mode when it is reset.
    void
    setCodebase(URL _codebase)
    Sets the codebase
    void
    setDelay(int _aDelay)
    Sets the delay between two steps of the simulation
    void
    setFPS(int _fps)
    Sets the (approximate) number of frames per second for the simulation
    void
    setModel(Model _aModel)
     
    boolean
    setVariable(String _name, String _value)
    This method sets the value of a public variable of the model.
    boolean
    setVariable(String _variable, String _value, String _sep)
    This method sets the value of a public variable of the model.
    boolean
    setVariables(String _valueList)
    This method is used to set more than one variables of the model at once.
    boolean
    setVariables(String _valueList, String _sep, String _arraySep)
    This method is used to set more than one variables of the model at once.
    void
    setView(View _aView)
     
    boolean
     
    void
    step
    void
    update
    void
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Simulation

      public Simulation()
  • Method Details

    • getModel

      public Model getModel()
    • setModel

      public void setModel(Model _aModel)
    • getView

      public View getView()
    • setView

      public void setView(View _aView)
    • setCodebase

      public void setCodebase(URL _codebase)
      Sets the codebase
    • getCodebase

      public URL getCodebase()
      Returns the codebase
    • play

      public void play()
      Sets the simulation in play mode
    • pause

      public void pause()
      Stops the simulation
    • stateLoop

      public boolean stateLoop()
      Specified by:
      stateLoop in interface javajs.async.SwingJSUtils.StateMachine
    • run

      public void run()
      Implementation of the Runnable interface
      Specified by:
      run in interface Runnable
    • setFPS

      public void setFPS(int _fps)
      Sets the (approximate) number of frames per second for the simulation
      Parameters:
      _fps - the number of frames per second
    • setDelay

      public void setDelay(int _aDelay)
      Sets the delay between two steps of the simulation
      Parameters:
      _aDelay - the number of milliseconds for the delay
    • setAutoplay

      public void setAutoplay(boolean _play)
      Sets whether the simulation should be set to play mode when it is reset. Default is false.
      Parameters:
      _play - Whether it should play
    • isPlaying

      public boolean isPlaying()
      Returns whether the simulation is running or not
    • isPaused

      public boolean isPaused()
      Returns whether the simulation is running or not
    • reset

      public void reset()
      Resets the simulation to a complete initial state
    • initialize

      public void initialize()
      Initialize model using user interface changes
    • apply

      public void apply()
      apply user interface changes
    • applyAll

      public void applyAll()
      apply user interface changes. Yes, exactly the same as apply() (I need it somewhere else :-)
    • apply

      public void apply(String _variable)
      apply a single change in the user interface
    • update

      public void update()
      update
    • step

      public void step()
      step
    • updateAfterModelAction

      public void updateAfterModelAction()
    • getVariable

      public String getVariable(String _name)
      This method returns a String with the value of a public variable of the model. If the variable is an array, individual element values are separated by a comma. Only public variables of primitive or String type can be accessed.
      Parameters:
      _name - The name of a public variable of the model
      Returns:
      The value of the variable as a String.
    • getVariable

      public String getVariable(String _name, String _sep)
      This method returns a String with the value of a public variable of the model. If the variable is an array, individual element values are separated by the specified separator string. Only public variables of primitive or String type can be accessed.
      Parameters:
      _name - The name of a public variable of the model
      _sep - A separator string to use for array variables
      Returns:
      The value of the variable
    • setVariable

      public boolean setVariable(String _name, String _value)
      This method sets the value of a public variable of the model. If the variable is an array, individual element values must separated by a comma. In this case, if the number of values specifies differs with the length of the array (a warning may be issued and) either the extra values are ignored (if there are more) or the last elements of the array will be left unmodified (if there are less). If the values provided cannot be parsed to the variable type (an error message may be issued and) the method returns false. Only public variables of primitive or String type can be accessed.
      Parameters:
      _name - the name of a public variable of the model
      _value - the value to be given to the variable
      Returns:
      true if the process was completed sucessfully, false otherwise
    • setVariable

      public boolean setVariable(String _variable, String _value, String _sep)
      This method sets the value of a public variable of the model. If the variable is an array, individual element values must separated by the specified separator string. In this case, if the number of values specifies differs with the length of the array (a warning may be issued and) either the extra values are ignored (if there are more) or the last elements of the array will be left unmodified (if there are less). Only public variables of primitive or String type can be accessed. If the values provided cannot be parsed to the variable type (an error message may be issued and) the method returns false.
      Parameters:
      _variable - the name of a public variable of the model
      _value - the value to be given to the variable
      _sep - the separator string for arrays
      Returns:
      true if the process was completed sucessfully, false otherwise
    • setVariables

      public boolean setVariables(String _valueList)
      This method is used to set more than one variables of the model at once. Pairs of the type 'variable=value' must be separated by semicolons. Then they will be tokenized and sent to setVariable().
      Parameters:
      _valueList - the string containing the pairs 'variable=value'
      Returns:
      true if all the variables are correctly set by setVariable()
      See Also:
    • setVariables

      public boolean setVariables(String _valueList, String _sep, String _arraySep)
      This method is used to set more than one variables of the model at once. Pairs of the type 'variable=value' must be separated by the separator string _sep. Then they will be tokenized and sent to setVariable(), using _arraySep as separator string for values of array variables.
      Parameters:
      _valueList - the string containing the pairs 'variable=value'
      _sep - the separator string between pairs
      _arraySep - the separator string for values of array variables
      Returns:
      true if all the variables are correctly set by setVariable()
      See Also:
    • saveState

      public boolean saveState(String _filename)
      Saves the state of the model either to a file on the disk or to memory. If the name of the file starts with the prefix "ejs:", then the state of the model will be saved to memory, otherwise it will be dumped to disk. Security considerations apply when running the simulation as an applet.

      The state of the model is saved by writing to disk all its public fields which implement the java.io.Serializable interface. This includes primitives and arrays.

      Parameters:
      _filename - the name of a file (either in disk or in memory)
      Returns:
      true if the file was correctly saved
    • readState

      public boolean readState(String _filename)
    • readState

      public boolean readState(String _filename, URL _codebase)
      Reads the state of the model either from a file on the disk, from memory or from a url location. If the name of the file starts with the prefix "ejs:", then the state of the model will be read from a memory file that must have been created previously with the corresponding call to saveState(). If the name of the file starts with "url:" it will be considered a url location and the method will attempt to read the file from this url (either locally or through the network). This file must have been previously created with a call to saveState() with destination a disk file, and then this file must have been copied at the right url location. dumped to disk. If the name of the file does not start with any of those prefixes, then it will be considered to be a file. Security considerations apply when running the simulation as an applet.

      The state of the model is read by reading from disk all its public fields which implement the java.io.Serializable interface. This includes primitives and arrays.

      Parameters:
      _filename - the name of a file (either in disk , in memory or a url)
      Returns:
      true if the file was correctly read