11.2 A short introduction to EJS (Easy Java Simulation)


You reach the up-to-date description of the EJS program via the homepage given on the right. Here we will give a very short overview as suggestion to have a close look at this program. When calling the EJS program on the lower boundary of the screen the EJS console as shown in the following figure appears.

In the first line of the main window one enters the directory where the Java-jre(java runtime environemt) is located, if the program does not find it by itself. In the second line an arbitrary directory can be defined as workspace for EJS; the program then automatically creates two directories in this directory:

The program stores new or changed files automatically in these directories, unless other paths for saving are specified in the individual case. The two directories can contain a hierarchy of further directories. Files that have been automatically copied can later be copied of moved to other places.

Figure 11.1: EJS console

With Launch Easy Java Simulation the editing window in Fig.11.2 is created. The console can be configured in such a way, that this step happens automatically whenever it is called.

Figure 11.2: The picture shows a page for the definition of parameters (variables). The simulation calls a working EJS console with editing window. It either contains no data, or the those from the last simulation. You can browse through the individual pages and change entries. You may study the many possibilities in the main window View. Please be careful when saving in order not to overwrite a file that already exists; thus you must a name that does not exist yet!

The Figure above shows it visual interface. the main menu in the top line contains three sections, each of which can consist of several pages.

For many simulations only a few pages are active. Simple function plotters for example do not require any special code and can be realized using view alone.

In the following line of the menu model in Fig.11.2 the sub-menu Variables is highlighted. The page Hilfsgroessen for the simulaion Doppelpendel Antrieb is shown.

The individual pages of Model have the following meaning:


Global variables, that do not appear only locally in individual code methods. The are designated as decimal number (double), integers (int), symbolic text (string), logical variable (boolean) and are also classified according to their dimensions ( for example number of computation loops with indices i,j,j:[i] or [i][j][k] …). It is important to note, that the decimal point appears as point, as is the standard in the U.S. and not as a comma as in German.


Here is entered, what applies when starting, for example special values for variables, equations involving the variables of calls to methods listed on other pages, that have to take place at start. This also includes logical equations, that select from different possibilities. Help is provided via the context menu, that can be called with the right mouse button. The following example puts the two initial velocities va of the pendulum bobs and the time t at the start of the simulation to zero.

t = 0.0; va1 = 0.0; va2 = 0.0;


Controls the succession of events, for example for an animation. It is particularly important, that differential equations can be entered here, that are then automatically solved via a choice of different methods . Typical example:

dy1 dt = va1to be solved with Runge-Kutta 4

Fixed Relations:

Here relationships between the variables can be entered, that are always valid and provide input to the calculation. The following example from the simulation connects variables with trigonometric functions of other variables. (Please note, that the function name must start with Math. in Java code).






Here special methods are formulated in Java code, that are called for example from the initialization page or by the control elements. The following example is used by button of the visualization. It deletes all lines and resets the variables to their initial values.

public void clear(){_resetView();_initialize();}

Here the methods _resetView() and _initialize() can be selected from a large number of prepared subroutines and one does not have to program these.

We now describe the function of the icons, that are on the right hand side. The shorthand *. stands , as usual for files, for an arbitrary filename before the file type like xml or jar.


On the right there are three menus below each other with a few pages each. They contain many icons, that can be put together with drag and drop on the visualization tree.

The top menu, which is called Interface, includes Containers as superior parents of the Java hierarchy, and pre-assembled control elements as children to be contained inside of it.

The second menu 2D Drawables contains icons, that can be inserted for two-dimensional visualizations. the third 3D Drawables icons for three-dimensional visualizations. In addition to icons, that symbolize a single element, there are some, that represent whole families of elements, for example arrows, points or curves.

Figure 11.3: View page of EJS. On the right are the selection menus of the icons, that can be pulled with drag and drop into the visualization tree on the left

Every icon used on the tree shows after double clicking a large menu for formatting and for connecting to variables and methods. Fig.11.4 shows this for the relatively simple icon P1, that represents the blob of the main pendulum in the double pendulum simulation. The coordinates x1 and y1 are connected to the element Pos. In the element Size the same dimensions are fixed in both directions. Draggable True means, that the point can be pulled with the mouse, which automatically gives a new value to the variable. On Drag Pendel() calls the proceudre Pendel() when the mouse is pulled. (Deleting of previous traces and restarting the calculations). Many open positions can be used for further formatting.

The exact definition of individual elements appears, if one holds the mouse pointer on the designation on the left. If one points at the label Visible, the message The visibility of the element (boolean) appears. If one double clicks on the first icon at the right of an element either the existing choices ( for visible true, false are shown, or assistance for entering information is given. The second icon contains a list of permitted quantities or methods, one of which can be selected with a click.

Figure 11.4: Window for fixing the properties of a visualization element (here of a point P1).

The next figure 11.5 finally shows the appearance of the main window in the active simulation. You can certainly identify elements of the visualization tree, especially on the left hand side (L1 and L2 are the pendulum bars, P1 and P2 are the pendulum bobs. Pfad_P2, the red orbit of the secondary pendulum, and so on ).

Figure 11.5: Visualization window for the simulation of the double pendulum. The left part of the window shows the two masses (primary pendulum as small blue circle, secondary pendulum as large yellow circle) of the double pendulum, which is fixed rotatable in the green point. The plane orbit of the yellow pendulum mass is drawn on red; it is very irregular. The right part of the window shows the phase-space of the primary pendulum.

current EJS

You actually do not need to know more to start with these simulations, that have been developed for this book ,and to use and change them. The same applies to other simulations that have been created with EJS. The EJS console require for this purpose is contained in the data carrier for this book. You can download a possibly newer version from the EJS homepage.

Start with something simple, for example with the calculation of the geometric series. Pressing the Ctrl-key and clicking on the following interactive picture opens the working EJS simulation as independent *.jar file.

Figure 11.6: Geometric sequence and series as a simple practice example. After starting the EJS window is opened via clicking on the simulation window with the right mouse button.

Now click with the right mouse button on the simulation and choose OPEN EJS Model in the context menu, that appears. A menu will pop up , that shows how a *.xml file is extracted and stored. The standard storage location is the directory source in the EJS workspace; you may also choose a different one.

After confirming your input, the EJS console appears with the editing window as in Fig.11.3. The previously active simulation vanishes into the background, and passive EJS windows appears. You may see the configuration of the elements, but the simulation cannot run in these windows.

Now save under another name ( the *.xml file is saved). Pressing the icon in the shape of a green triangle creates an active simulation under a new name.

Now change individual elements in the pages of the editing console and then save the *.xml file under a new name. If you close the old version and click on the green triangle, your version becomes active, or you get an error message with solution hints, if you have build a bug into the program. Initially only go into View and change the colours or the thickness of lines. This way nothing can really break.

With some more insight you can also change the simple formulas in Custom and Fixed Relations and thus calculate another series then the geometric one. Frequent saving under different names allows the finding of working versions when encountering errors.

The help function, that can be called with the link on the margin or directly from the console contains details on all individual elements. At this point you also find extensive documentation for example for a introductory course. help-doc