This demo illustrates how the JMSL Library can easily be used to build a Java application that incorporates live data. In this case, the application is of a strip chart display..
The Lines menu bar lets you select which lines are plotted on the chart.
The chart is a traditional set of line graphs of random data. When the menu item is selected or deselected, an internal class is used to set the paint attribute for that Data object:
jMenuItem
.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
data[index].setPaint(jMenuItem.getState());
}
});
The animation is accomplished by using a separate thread.
The thread is started in the main()
method:
Thread
thread = new Thread(strip);
thread.setDaemon(true);
thread.start();
This code then calls the run()
method of
Strip because it implements Runnable
, which updates the
chart. Note the try/catch block and the Thread.sleep()
call. Also
note that the paint()
method of Strip must be synchronized
.
public
void run() {
int n = y[0].length;
float t = 0;
while (true) {
synchronized (this) {
t += 1.0f;
label.setTitle("Current Time "+t);
for (int i = 0; i < y.length; i++) {
for (int k = n-1; k > 0; k--) {
y[i][k] = y[i][k-1];
}
y[i][0] += 0.10*Math.random() - 0.05;
if (Math.abs(y[i][0]) > 1) y[i][0] = 0.0;
}
}
repaint();
try {
Thread.sleep(50);
} catch (Exception e) {
}
}
}
Strip.java | This is the main class that extends JFrameChart and implements Runnable because of the animation thread. |
Two alternatives are available to run this demo:
1) Use the source code in your development environment as any other Java code. More information is available in the How To.
2) An executable jar file containing all of the demos
referenced in this guide is included in the jmsl/lib directory. On
Windows, you may double-click the file to run it if files with a ".jar"
extension are properly registered with javaw.exe. Alternatively,
for both Windows and UNIX environments, the jar file may be executed from the
command line using java -jar gallery.jar
.
As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.