org.opensourcephysics.ejs
Class Simulation

java.lang.Object
  extended by org.opensourcephysics.ejs.Simulation
All Implemented Interfaces:
java.lang.Runnable

public abstract class Simulation
extends java.lang.Object
implements java.lang.Runnable

A base interface for a simulation


Field Summary
static int MAXIMUM_FPS
           
static int MINIMUM_FPS
           
 
Constructor Summary
Simulation()
           
 
Method Summary
 void apply()
          apply user interface changes
 void apply(java.lang.String _variable)
          apply a single change in the user interface
 void applyAll()
          apply user interface changes.
 java.net.URL getCodebase()
          Returns the codebase
 Model getModel()
           
 java.lang.String getVariable(java.lang.String _name)
          This method returns a String with the value of a public variable of the model.
 java.lang.String getVariable(java.lang.String _name, java.lang.String _sep)
          This method returns a String with the value of a public variable of the model.
 View getView()
           
 void initialize()
          Initialize model using user interface changes
 boolean isPaused()
          Returns whether the simulation is running or not
 boolean isPlaying()
          Returns whether the simulation is running or not
 void pause()
          Stops the simulation
 void play()
          Sets the simulation in play mode
 boolean readState(java.lang.String _filename)
           
 boolean readState(java.lang.String _filename, java.net.URL _codebase)
          Reads the state of the model either from a file on the disk, from memory or from a url location.
 void reset()
          Resets the simulation to a complete initial state
 void run()
          Implementation of the Runnable interface
 boolean saveState(java.lang.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(java.net.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(java.lang.String _name, java.lang.String _value)
          This method sets the value of a public variable of the model.
 boolean setVariable(java.lang.String _variable, java.lang.String _value, java.lang.String _sep)
          This method sets the value of a public variable of the model.
 boolean setVariables(java.lang.String _valueList)
          This method is used to set more than one variables of the model at once.
 boolean setVariables(java.lang.String _valueList, java.lang.String _sep, java.lang.String _arraySep)
          This method is used to set more than one variables of the model at once.
 void setView(View _aView)
           
 void step()
          step
 void update()
          update
 void updateAfterModelAction()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXIMUM_FPS

public static final int MAXIMUM_FPS
See Also:
Constant Field Values

MINIMUM_FPS

public static final int MINIMUM_FPS
See Also:
Constant Field Values
Constructor Detail

Simulation

public Simulation()
Method Detail

getModel

public Model getModel()

setModel

public void setModel(Model _aModel)

getView

public View getView()

setView

public void setView(View _aView)

setCodebase

public void setCodebase(java.net.URL _codebase)
Sets the codebase


getCodebase

public java.net.URL getCodebase()
Returns the codebase


play

public void play()
Sets the simulation in play mode


pause

public void pause()
Stops the simulation


run

public void run()
Implementation of the Runnable interface

Specified by:
run in interface java.lang.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(java.lang.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 java.lang.String getVariable(java.lang.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 java.lang.String getVariable(java.lang.String _name,
                                    java.lang.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(java.lang.String _name,
                           java.lang.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(java.lang.String _variable,
                           java.lang.String _value,
                           java.lang.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(java.lang.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:
setVariable(String,String)

setVariables

public boolean setVariables(java.lang.String _valueList,
                            java.lang.String _sep,
                            java.lang.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:
setVariable(String,String)

saveState

public boolean saveState(java.lang.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(java.lang.String _filename)

readState

public boolean readState(java.lang.String _filename,
                         java.net.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