Education Prize Logo
Science SPORE Prize
November 2011

NSF Logo
The Open Source Physics Project is supported by NSF DUE-0442581.

Simulations

History of Java to JavaScript in Open Source Physics

In order to make Open Source Physics material available on all computer platforms, the OSP project first adopted the Java programming language because it promised to let application developers write once, run anywhere (WORA), meaning that compiled Java code could be run on all platforms that had Java installed without the need for recompilation. Java was available for Microsoft, Apple and other vendors at no cost and was often distributed as part of the operating system. Eventually, the Sun (now Oracle) version of Java became the standard distribution and it appeared that educators had a stable platform independent language for software development. An important feature of Java was that programs could be embedded into html web pages as Java applets, thereby allowing educators to create and distribute interactive simulation-based curricular material using readily available browsers. Physlet Physics and Physlets Quantum Physics books used the Java applet platform to deliver over one thousand ready-to-use html pages for the teaching of introductory physics. In order to engage students in computational physics and computer modeling, the OSP team used its experience developing Physlets to create to create an object-oriented code library for a new (third) edition Introduction to Computer Simulation Methods book by Harvey Gould and Jan Tobochnik. Simulations were developed in the Eclipse Integrated Development Environment (Eclipse IDE) and then compiled into stand-alone java archive (jar) files that could be run on any computer that had Java installed.

The successful hosting of Physlets and Java-based OSP simulations on ComPADRE provided worldwide access to our materials for users on desktop and laptop computers. Unfortunately, Java applets were an easy target for hackers and eventually became outdated and clumsy, requiring frequent user-installed updates, security certificates, and use of specific browsers.  Support for applets diminished when Apple removed support for Java applets in 2012 from Safari and the Java plugin was removed from Firefox and Chrome browsers in 2018, thereby breaking web sites that utilized Java applets, including our original Physlets. Thus, it was believed that curricular material that had grown around Java applets had to be abandoned. (Java itself continues to develop and is widely used to develop stand-alone programs for servers and desktop computers. A version of Java powers Android and is used to develop native Android apps.)

The decline of Java Applets coincided with the rise of mobile devices and their use of JavaScript + HMTL5. The Open Source Physics team addressed this technological transition in two ways:

  1. Develop new version of the Easy Java Simulations authoring tool capable of creating using JavaScript simulations, and
  2. Convert existing simulations from Java to JavaScript.

The JavaScript + HMTL5 combination again enabled curriculum authors to write once, run anywhere. An added benefit of moving OSP curriculum development to the industry standard platform was that this technology is the basis for the modern web and for mobile apps, electronic books with embedded interactive simulations, and Progressive Web Apps – web pages which mimic standalone programs by installing themselves on desktop or mobile home screens.

The Easy Java/JavaScript Simulations Authoring Tool

Because students and in-service teachers found it difficult to combine programming and curriculum authoring in the Eclipse IDE, in 2002 Francisco Esquembre (University of Murcia, Spain) used the OSP code library to develop the Easy Java Simulations (EJS) programming and authoring tool.  EJS is a free open-source program developed in Java that makes use of OSP numerical analysis, 2D and 3D visualization, and graphical user input-output components. It was originally designed for interactive learning under the supervision of educators but is suitable for use by researchers to prototype applications and by authors to develop and distribute Java-based curricular materials. Authors could quickly create a user interface with buttons, tables, graphs and html narrative by dragging and dropping components from a pallet into a view. While some programming knowledge is assumed, EJS users are encouraged to focus on modeling rather than on programming. A button click was all that was required to create either a stand-along simulation (jar file) or a web page with an embedded Java applet.

Although EJS continues to be an excellent Java tool for developing Java simulations, it became less useful as a curriculum authoring tool when browsers dropped applet support. Fortunately, Felix Garcia (University of Murcia, Spain) joined Esquembre in 2014 to collaboratively develop a version of EJS that can produce JavaScript simulations that run on any platform, including mobile devices.  This new JavaScript version of EJS is supported by the OSP project and the ComPADRE OSP Collection now contains 120 EJS generated JavaScript simulations.

SwingJS Transpiler

A compiler takes the source code of a program written in one programming language as its input and produces the equivalent code in another programming language. Tools that translate between high level languages are often referred to as transpilers. For example, there exist Fortran to C++ and Python to Ruby transpilers. The Java to JavaScript (java2cript) transpiler that we used to convert Physlets and other OSP programs was started by Zhou Renjian and further developed and extended at St. Olaf College to convert the JMol molecular structure viewer to JavaScript. In the browser, a second software component, the SwingJS library, executes the transformed JavaScript code. The SwingJS library, essentially, reproduces the Java Virtual Machine in JavaScript.

The JavaScript simulations are created in parallel with standard Eclipse-based Java development, along with their Java counterparts. In the end, users have the original functional Java applet or stand-alone application, and they have its virtually identical JavaScript counterpart, with all the layout, events, and functionality of the original. One of the most powerful and important features of Java -- reflection (the loading of classes on the fly, only as needed) -- is fully preserved, providing the modularity necessary for complex web application development.

The OSP-ComPADRE team, in collaboration with Bob Hanson at St. Olaf College, used the java2script transpiler and the SwingJS library to converted the Physlet Physics and Physlet Quantum Physics books to JavaScript with only minor modifications.  We note that the Java to JavaScript has also been used by the St. Olaf team to convert some Falstad and PhET simulations as shown on the SwingJS website.

SwingJS_conversions.png
The St. Olaf SwingJS website catalogs simulations that have been converted from Java to JavaScript.

Performance

We do not believe that users will notice any significant performance differences in most educational software, such as Java and JavaScript Physlets. For example, the Diatomic Physlet models collisions between hard-disks and hard-disk-barbells thus simulating a mixture of monatomic and diatomic atoms. This simulation is used in Chapter 20 Exploration 4 of the Physlet Physics book to study the equipartition of energy. Students choose the number of particles of each type and the simulations displays the molecular motion together with a graph and a table showing the total energy, the translational energy of each particle type, and the rotational energy diatomic particles. Although the Diatomic Physlet JavaScript implementation is 30% slower than the Java implementation when running with 500 monatomic and 500 diatomic particles, this performance difference are not noticeable in the Physlet Physics book because the user input limits the number of particles to avoid close packing of the particles.

The OSP Network:
Open Source Physics - Tracker - EJS Modeling
Physlet Physics
Physlet Quantum Physics