Java Reference
In-Depth Information
JFrame position - frame = new JFrame ( " Position Histogram " );
position - frame.getContentPane ().add (fPositionHPan);
Let's add a Quit button to demonstrate the use of the fifth element of the input
data array to control the server-side simulation:
fReallyQuit = false;
fQuitButton = new JButton ( " Quit " );
fQuitButton.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent ev) {
if (fReallyQuit)
System.exit (0);
fIndata[5] = 1.f;
fReallyQuit = true;
fQuitButton.setText ("One more time to exit");
}
});
position - frame.getContentPane().add (fQuitButton,
BorderLayout.SOUTH);
When fReallyQuit is false, as it will be the first time the Quit button is
clicked, then we set the value of fIndata[5] to be 1, the special sentinel value
that the server uses to halt the simulation. Upon the next poll for new data,
fIndata is sent to the server, and the server sees the 1 in the fifth position and
halts the simulation. We also set fReallyQuit to true and change the text of the
Quit button. The second time the button is clicked causes the client application
to exit.
During polling, we load the histogram with new data and cause it to be
repainted:
outdata = fServer.retrieveData (fIndata);
time = outdata[0];
xposition = outdata[1];
velocity = outdata[2];
acceleration = outdata[3];
kinetic = outdata[4];
potential = outdata[5];
total - energy = kinetic + potential;
fPositionHist.add (xposition);
fPositionHPan.repaint ();
We can just as easily make histograms of the other quantities as well. They all look
similar. Using A
we obtain the following
position histogram when using 21 bins (see Figure 20.2). A better approximation
is obtained using
= ω =
K
=
1,
T
=
0.1 and T max =
2
π
T
=
0.001 and 51 bins (see Figure 20.3).
Search WWH ::




Custom Search