Package org.opensourcephysics.ejs
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
apply()
apply user interface changesvoid
apply a single change in the user interfacevoid
applyAll()
apply user interface changes.Returns the codebasegetModel()
getVariable
(String _name) 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.getView()
void
Initialize model using user interface changesboolean
isPaused()
Returns whether the simulation is running or notboolean
Returns whether the simulation is running or notvoid
pause()
Stops the simulationvoid
play()
Sets the simulation in play modeboolean
boolean
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 statevoid
run()
Implementation of the Runnable interfaceboolean
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 codebasevoid
setDelay
(int _aDelay) Sets the delay between two steps of the simulationvoid
setFPS
(int _fps) Sets the (approximate) number of frames per second for the simulationvoid
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
boolean
void
step()
stepvoid
update()
updatevoid
-
Field Details
-
MAXIMUM_FPS
public static final int MAXIMUM_FPS- See Also:
-
MINIMUM_FPS
public static final int MINIMUM_FPS- See Also:
-
-
Constructor Details
-
Simulation
public Simulation()
-
-
Method Details
-
getModel
-
setModel
-
getView
-
setView
-
setCodebase
Sets the codebase -
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 interfacejavajs.async.SwingJSUtils.StateMachine
-
run
public void run()Implementation of the Runnable interface -
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
apply a single change in the user interface -
update
public void update()update -
step
public void step()step -
updateAfterModelAction
public void updateAfterModelAction() -
getVariable
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
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
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
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
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
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
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
-
readState
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
-