Dr.John.B.Matthews

KineticModel

Java Web Start: Launch the KineticModel by clicking here.
Download jar: Get the KineticModel jar file by clicking here.

This program models elastic collisions among spherical particles and the walls of a container. KineticModel uses the conservation of momentum [p = m·v] and kinetic energy [KE = ½·m·v2] to determine the velocity of particles after a collision. To simplify calculations, position and velocity are stored in 2-dimensional vectors as described below.


Design

The design of KineticModel is an example of the Model-View-Controller pattern. The main class, KineticModel, creates an instance of the model, which is an Ensemble of Particle. It then creates the main view of that model, a DisplayPanel. A separate ControlPanel allows setting the simulation's parameters. The Vector class stores 2-dimensional vector data and provides elementary vector operations. Finally, a Histogram gives a continuously updated view of the Particles' velocities.

Implementation

This implementation requires Java 1.5 or later. The animation loop is driven by a javax.swing.Timer instance in the controller. After an initial delay, the Timer fires at a nominal rate of 25 Hz, invoking the controller's actionPerformed() method. The latter repaints the main view, a histogram of particle velocities, and several informational labels. Three animation techniques are used: texture-paint, erase-draw, and label-icon.

In texture-paint animation, an instance of TexturePaint is created with a small BufferedImage. At each iteration, the image's raw pixels are randomly re-colored, and the TexturePaint is used to frame the view. Because the underlying image is small and used repeatedly for filling, the animation overhead is small.

The erase-draw method simply erases the entire panel to black, moves each particle to it's next position and re-draws the particle. The Particle may be filled with Color or drawn from a GradientImage The model's iterate() method handles collisions. Because the JPanel itself is double-buffered, the animation remains smooth.

Finally, the velocity histogram is repainted. The Histogram class simply extends JLabel and implements the icon interface, becoming its own icon.

References

Revision history

1.2.0 15-Oct-2008 Use RadialGradientPaint.

1.1.0 01-Sep-2008 Initial release.

1.0.67 15-Jul-2008 Initial Java version.

KineticModel is an open-source project, distributed under the terms of the GPL, version 3.

Copyright: ©2008, John B. Matthews.

Last updated 17-Oct-2008

Subpages (1): Source code

Attachments (2)

  • KineticModel.jar - on Nov 20, 2009 3:27 PM by John Matthews (version 3 / earlier versions)
    48k Download
  • KineticModel.jnlp - on Jun 28, 2009 6:45 AM by John Matthews (version 1)
    1k Download