Our gravitational acceleration experiment simulation
will follow at Model-View-Controls (MVC) design pattern.
(A "design pattern" refers to a common generic type of
structure for a program or program function.) Here the Model
refers to the physics calculations that will generate the simulation
behavior. The View refers to the display of the simulation
graphics, the simulated detector results, and the buttons and parameter
entry fields. The Controls refer to the code that provides
the response to the buttons and entry fields, i.e. actually runs
the program. (The Swing GUI system follows a MVC design.)
We will build an applet (which will include a main()
method so it can also run as an application) to hold the elements
of the simulation. It will also provide the control code for the
program. We could have spun the controls into an separate class
but for convenience here we will include it in the top level applet
class.
- Model:
- DropModel
instance generates the simulated data - position and velocity
- of the falling mass
- View:
- DropTestApplet
provides the JApplet
subclass that holds the following graphic components:
- DropPanel
- this subclass of PlotPanel
will display the animation of the falling mass.
- HistPanel
- displays the results of the experiment.
- JButton
components to start to dropping of the object, reset the
histogram and other variables, and to exit the program
- JTextField
components to allow user input of the number of drops
to do at one time and the animation rate factor.
- Controls
- DropTestApplet
controls the program:
- Builds the graphical interface, creates an instance
of DropModel,
initializes the setup.
- It implements ActionListener
interface and receives the action events from the buttons.
- It creates timer objects that provided the periodic
signals to do the incremental drop calculations with the
model and then display the animation frame for the ball
drop.
Figure Phy.9.1 shows the user interface for the simulation.
Consists of 3 parts:
the controls, the display of the drop tower with falling ball, and
the histogram.
The user interface will show a ball dropping after the user hits
the "Drop" button. The histogram will record the time
it takes to pass between two points along the vertical track. The
following graphic shows the process the program follows after the
user clicks on the drop button.
Figure Phy.9.2 When the drop button is clicked on the
DropTest_JApplet11
progam creates a Timer
instance that fires every dt=20ms.
The run()
method in the TimerTask
subclass first invokes the step()
method in DropModel
to obtain the new y position for the ball. Next the DropPanel
updates the position of the ball and redraws itself. The cycle
repeats until the ball crosses a given finish line at the bottom.
The cycle then repeats for a the next drop.
As one starts to code a physics simulation, various details start
to emerge that were not necessarily apparent at the start (See also
the Chapter 1: Physics
: Learn by Coding discussion.). For example,:
- Scaling, e.g. how many cm per pixel?
- Time scale, e.g. will the animation frame rate reflect a true
physical time or can it be speeded up or slowed down? Does the
speed up affect the calculations or does it only affect the animation
display?
- Care must be taken in dealing with units, e.g. floating point
values are required for many of the calculations of velocity,
position and these may need transforming to integer pixel values.
Are round-off errors significant?
Note that one of the primary benefits of a simulation is that an
animation can show if the performance is realistic, thus giving
a check on the calculations. In fact, that may be the reason for
creating the simulation in the first place, especially if you are
working on a theoretical description of a phenomena. A particular
advantage of using Java for simulations is that, as we have seen,
it has strong graphics and animation capabilities.
Issues with regard to the program's user interface involve:
- We tried to follow a flexible, modular design that will allow
addition of a detector section and other enhancements at later
stages.
- Controls:
- The drop button initiates the drop and then changes to a
stop button to allow the user the option of ending the drop(s)
sequence.
- Provides entry fields to set the number of drops and a speed
up factor for the animation.
- After using the program you might want to modify it to allow
for more options:
- You must decide how many options to allow and how many
settings to hard code?
- You must decide whether to use menus, lists, or other UI components
to allow the user to modify settings?
Most recent update: Oct. 25, 2005
|