--------------------------------------------------------------------
This single plain text file contains the files included with
the article "Contour plotting in Java" published in MacTech
magazine, vol. 13, no. 9 (September 1997):
-- ContourPlotApplet.html
-- ContourPlotApplet.java
-- ContourPlot.java
-- ContourPlotLayout.java
-- ParseMatrixException.java
-- Alternate Data
--------------------------------------------------------------------
File "ContourPlotApplet.html":
--------------------------------------------------------------------
Contour Plotting using Java
Contour Plotting using Java
Yes, this page uses Java
and thus requires a Java-enabled browser.
Instructions
- Enter the matrix of z values, in the following format (similar to Mathematica format):
- The values are floating point numbers separated by commas.
- Each row of values is enclosed in brace brackets.
e.g. {0.4, 1.2, 1.03}
- The rows themselves are separated by commas and enclosed in brace brackets.
- The number of rows must be at least 2 and at most 100.
- The rows need not be of the same length; the longest row must have at least 2 and at most 100 values.
- Click in the button "Draw" to trace the contour plot based on the data you have provided. The program will:
- parse the matrix of z values, filling short rows with zeroes if necessary to make the matrix rectangular;
- calculate ten values, one for each contour to be drawn, by linearly interpolating between the maximum and minimum z values in the matrix;
(One may, however, select logarithmic interpolation using the check box just to the left of the "Draw" button.
Log interpolation is better for data containing sharp peaks, but is possible only if all values in the matrix are positive.)
- display the results of steps 1 and 2 (including the 10 contour values, numbered [0] through [9]) in the area in the lower left of the applet's panel; and
- draw the contour plot in the right-hand portion of the applet's panel.
About this applet...
This applet is a work in progress and may contain bugs. It was developed by
David Rand on a Macintosh using Metrowerks CodeWarrior Java.
It has undergone preliminary testing on Macintosh and UNIX platforms. If you experience problems with this applet, please inform
the author by e-mail at rand@CRM.UMontreal.CA; be sure to specify your platform and browser.
Thanks in advance.
The plotting algorithm was taken from a Fortran program by Snyder [1].
The program was first translated into C, then reworked, and finally
translated into Java. Flanagan [2] was indispensable for the Java implementation.
References
- W. V. Snyder, "Algorithm 531, Contour plotting [J6]",
ACM Trans. Math. Softw. 4, 3 (Sept. 1978), 290-294.
- D. Flanagan, Java in a Nutshell, O'Reilly & Associates (1996).
--------------------------------------------------------------------
File "ContourPlotApplet.java":
--------------------------------------------------------------------
import java.awt.*;
import java.io.*;
//----------------------------------------------------------
// "ContourPlotApplet" is the main class, i.e. the applet
// which is a container for all the user-interface elements.
//----------------------------------------------------------
public class ContourPlotApplet extends java.applet.Applet {
// Below, constants, i.e. "final static" data members:
final static int NUMBER_COMPONENTS = 6;
final static int MIN_X_STEPS = 2,
MIN_Y_STEPS = 2,
MAX_X_STEPS = 100,
MAX_Y_STEPS = 100;
final static String EOL =
System.getProperty("line.separator");
final static String DEFAULT_Z =
"{{0.5,1.1,1.5,1,2.0,3,3,2,1,0.1}," + EOL +
" {1.0,1.5,3.0,5,6.0,2,1,1.2,1,4}," + EOL +
" {0.9,2.0,2.1,3,6.0,7,3,2,1,1.4}," + EOL +
" {1.0,1.5,3.0,4,6.0,5,2,1.5,1,2}," + EOL +
" {0.8,2.0,3.0,3,4.0,4,3,2.4,2,3}," + EOL +
" {0.6,1.1,1.5,1,4.0,3.5,3,2,3,4}," + EOL +
" {1.0,1.5,3.0,5,6.0,2,1,1.2,2.7,4}," + EOL +
" {0.8,2.0,3.0,3,5.5,6,3,2,1,1.4}," + EOL +
" {1.0,1.5,3.0,4,6.0,5,2,1,0.5,0.2}}";
// Below, the six user-interface components:
ContourPlot thePlot = new ContourPlot(MIN_X_STEPS,
MIN_Y_STEPS);
Label zPrompt = new Label("", Label.LEFT);
TextArea zField = new TextArea(DEFAULT_Z,30,6);
Checkbox interBox = new Checkbox();
Button drawBtn = new Button();
TextArea results = new TextArea();
// Below, class data members read from the