Source code for the Vortex Applet.

The source code is presented here along with a brief description of what each class does.

This code is in the public domain.

PlottingContext.java

A utility class for mapping real-number coordinates onto screen-based, pixel based coordinates.

Vortex.java
VortexSystem.java
VortexSystem_RK.java

It is in these classes that the solution to the system of ODEs is actually computed. The Vortex class represents a vortex which has an x-coordinate, a y-coordinate, and a vorticity. In the current version of the Vortex Applet, every vortex has vorticity one. However, the numerical scheme works with vorticities equal to any real number, and future versions of the Vortex Applet may allow the user to create such vortices.

The VortexSystem class manages the basic data structures which represent a vortex system, which is represented as two Vector objects which hold elements of type Vortex. After each iteration, the references to these objects are swapped. The actual numerical method (a Runge-Kutta scheme) is implemented in VortexSystem_RK, which is a subclass of VortexSystem. One could implement a different numerical method by defining a different subclass of VortexSystem.

RunLock.java

A lightweight class used to implement the synchronization strategy.

Sprite.java
SpriteCanvas.java

General classes for implementing the draggable sprite-based interface. A good introduction to the techniques used can be found in that fine book, The Black Art of Java Game Programming.

VortexSprite.java

A vortex sprite is both a vortex and a sprite (a graphical object that can be dragged by the user). Each vortex sprite contains a reference to the plotting context used in the vortex canvas, which defines the mapping between the mathematical (x,y)-plane and pixel-based screen coordinates.

VortexCanvas.java
VortexApplet.java

The VortexApplet class defines the main line of code. It creates the vortex canvas and GUI controls, and contains a run() method which executes the main animation loop. The vortex canvas is the place where the display and user manipulation of the vortices happen, and is an object of class VortexCanvas, itself a subclass of SpriteCanvas.

The synchronization strategy

While the applet is running, there are three threads which can run simultaneously:

The applet uses two different lock mechnanisms to ensure that these threads do not interfere with each other.

First, there is the lock associated with the vortex canvas itself. The bodies of the nextFrame() and paint(Graphics) methods of the VortexCanvas class are enclosed in synchronized(this) blocks to ensure that, for example, the positions of the vortices are not altered while a frame is being painted.

Second, the vortex canvas has a field runLock, which is an object of class RunLock that contains a boolean field named running. The animation thread holds the lock on runLock while the positions of the vortices for a new frame are being computed and the frame is being drawn. Between frames, it checks to see if the value of runLock.running is true; if not, it exits the loop. When a mouse down event is handled by the vortex canvas, the event handler sets runLock.running to equal false, then blocks until it acquires the lock on runLock. This ensures that the last frame is completely drawn and the animation loop completely halted before a mouse down event is processed.


Return to Vortex Applet page