Mathlets Source Code
JavaTM Applets for Math Explorations
Written by Tom Leathrum
The source code and other materials provided here are subject to
copyright restrictions, and are provided here
as a courtesy for other Java developers.
If you wish to copy this material, in whole or in part, please
contact the author.
The previously advertized big upgrade is complete, so this page has
been modified to reflect the results of the upgrade process.
All of the Mathlets applets have been written in Java2 and compiled using
the Java2 SDK v1.4.2 on Linux, as available directly from
Sun Microsystems,
along with (in some cases) JavaCC v3.2, available now through Sun's Developer Connection.
The edu.jsu.leathrum.mathlets.shared
package contains the following classes (listed here in alphabetical order):
- AButtonSet implements FieldHandler, ActionListener
-- abstract class,
provides "name=value" label
and two RepeatingButton's
("+" and "-"), subclass must implement button actions
- interface ActionWrapper
requires method doThis() -- general way of storing a parameterless method
- AGraphPanel -- abstract class,
panel which includes an AGraphCanvas and buttons linked
to the graph manipulation methods in AGraphCanvas, subclass
must initialize (calling method initpanel()) with instantiated subclass of AGraphCanvas
inner classes:
- AGraphCanvas -- abstract class,
this is the basic graphing canvas, includes methods for graph
manipulations such as zooming and mouse tracking,
subclass must implement abstract drawFigure() method to do the actual drawing,
may also override buildFigure() and drawFirst() methods
- CoordsConverter -- converts
between graphing coordinates
and pixel drawing coordinates
- WindowPanel -- panel for pop-up
window for graph axis
bounds settings using several DoubleInputSet's and buttons
- AGraphPanel3d -- abstract class,
3-D version of AGraphPanel, fewer buttons since no zooming
or mouse tracking available
inner classes:
- AGraphCanvas3d -- abstract
class, the basic 3-D graphing canvas, the 3-D
version of AGraphCanvas, more sophisticated axis and coordinate
transformations, but zooming and mouse tracking methods not available
- CoordsConverter3d -- 3-D
version of CoordsConverter, includes 3-D to 2-D projection code
- WindowPanel -- 3-D version of
WindowPanel,
more axis bounds to set
- ViewPanel -- panel for pop-up
window for view angle and
graph grid settings using several DoubleInputSet's and buttons
- ViewPtPanel -- panel for pop-up window for view point settings using several
DoubleInputSet's and buttons, point translated to angles for
ViewPanel3d
- AGraphPanel3dFacets
extends AGraphPanel3d -- abstract class,
with inner class AGraphCanvas3dFacets extending AGraphCanvas3d,
includes code to
implement faceted surface representation of 3-D graphs
inner classes:
- Facets -- dynamic array
of DoubleTriad's, represents one "facet", painted as opaque polygon,
includes drawing code using CoordsConverter3d for projection
- GraphModelFacets -- dynamic array of Facet's (entire surface),
also includes sorting code for implementing
front-back "hiding" of surface
- AGraphPanel3dWires
extends AGraphCanvas3d-- abstract class,
with inner class AGraphCanvas3dWires extending AGraphCanvas3d,
includes code to implement wireframe representation of 3-D graphs
inner classes
- Path -- dynamic array
of DoubleTriad's, represents one "wire", painted as sequence of
line segments, includes drawing code using CoordsConverter3d for projection
- GraphModelWires -- dynamic array of Path's (entire wireframe)
- AMathlet
extends JApplet -- the main class that all applets in the Mathlets set extend, implements
handling of return-key events in input fields, major button events, JavaScript interface, HTML parameters
abstract methods:
- localinit() for applet-specific initialization, including setting label
contents based on Locale data
- draw() for applet-specific graphing procedures
other important methods:
- localclear() empty by default, can be overridden with applet-specific
procedures for clearing applet fields and graphs)
- setHandler() registers a FieldHandler with an associated parameter
name, for JavaScript interface and HTML parameter handling
- registerInputSet() registers an
InputSet with a local KeyHandlerSequence
- registerGraphButton() registers a button with a local ButtonList, associates a call to
the draw() method with the button
- registerClearButton() registers a button with a local ButtonList, associates a call to
the private clear() method with the button, calls setToDefault() on all fields registered with
setHandler(), then calls localclear() and draw()
- getNumberFormat() returns DecimalFormat associated with the Locale
- getProperty() gets data from properties files based on Locale
- AParserInputSet
extends InputSet -- abstract class, interprets text
in input field as expression for Parser,
includes code for evaluating stack returned by Parser,
subclass must implement setVars() and getVar() methods to establish how to handle variables
- ASimpleGraphCanvas --
abstract class, simplified graphing canvas without all of the
manipulation methods, subclass must implement drawFigure() method for actual figure to
be drawn
- ButtonList,
inner class Listener implements ActionListener --
method registerButton(AbstractButton, ActionWrapper) associates button with action, so that
clicking the button causes a call to the ActionWrapper's doThis() method
- DblButtonSet
extends AButtonSet -- button set designed for handling double values
- DoubleInputSet
extends InputSet -- input set designed for handling double values
- DoubleTriad -- simple data type
with three double's
- interface FieldHandler
requires methods setDefault() setToDefault() setField() getField() -- this is the interface by
which InputSet's are enabled to handle the JavaScript interface
(see InputSet for typical basic implementation, AMathlet for the actual methods
of the JavaScript interface)
- FixedWidthLabel
extends MLabel, implements label with fixed width,
to make layout easier with dynamic labels
- FormattedFixedWidthLabel
extends FormattedLabel, implements formatted label with fixed width,
to make layout easier with dynamic labels
- FormattedLabel
extends MLabel-- extends MLabel, implements simple character formatting,
including subscripts, superscripts, and half-width spaces, also includes
code for some Unicode character substitutions (Greek letters, a few math
symbols), makes heavy use of Java2 HTML capabilities
- InputSet implements KeyHandler, FieldHandler
-- basic input set (label
plus text input field),
options include checkbox, color labels, additional label after field
- IntButtonSet
extends AButtonSet -- button set for handling integer values
- IntInputSet
extends InputSet -- input set designed for handling integer values
- interface KeyHandler
requires methods isKey() doKeyAction() addKeyListener() requestFocus() -- interface by
which return-key events select within a sequence of InputSet's (see KeyHandlerSequence
and InputSet for typical uses), typically on a key event the event is checked to see if isKey()
returns true, and if so, the doKeyAction() event is called (other required methods analogous to
JComponent versions)
- KeyHandlerSequence
inner class Listener implements KeyListener --
method registerKeyHandler(KeyHandler, ActionWrapper) associates an action with a KeyHandler's
specially-handled key event, similar to how a ButtonList associates an action with
a button
(method registerKeyHandler(KeyHandler) associates a NullAction with the key),
then moves input focus to the next KeyHandler in the sequence
- MLabel
extends JLabel -- utility extension of JLabel to implement
some methods simplifying changes of font size and style, and to make
some of the JLabel painting commands accessible to subclasses
- NullAction implements ActionWrapper -- the doThis() method
does nothing
- Parser and other classes derived from JavaCC file
Parser.jj -- implement the expression parser,
input is string, output is stack representation of parsed string
- RepeatingButton
extends JButton, captures the button's
click event, repeats it as long as the button is being held down
(using a Swing Timer)
The following examples include a broad sampling of the above
shared classes:
- Parabolas (ASimpleGraphCanvas,
DblButtonSet;
implicitly includes AButtonSet, RepeatingButton,
extensions of MLabel)
- Tangent Lines (AGraphPanel and its inner
classes, AParserInputSet, DoubleInputSet;
implicitly includes Parser and related classes, InputSet,
extensions of MLabel)
- 3-D Graphing (AGraphPanel3dFacets
and its inner classes,
AParserInputSet;
implicitly includes AGraphPanel3d and its inner classes,
Parser and related classes, DoubleInputSet, InputSet,
extensions of MLabel, DoubleTriad)
-- see also wireframe form, using
AGraphPanel3dWires
- Basic Animation: Limits incorporates a basic
animated sequence. None of the other applets use such a sequence, so
this code is not shared. This applet does use AParserInputSet and
DoubleInputSet.
- I'm pretty proud of my
Derivative Calculator applet,
so I decided to include
the code from it here as well. It uses its own
parser (also written in JavaCC), which returns
a parse tree rather than a stack representation of the expression.
To implement and construct the tree, the applet uses a basic
node class and a specialized utility
tree building class (which uses a stack
to keep track of newly added children).
The TParserInputSet class, which
extends the shared InputSet class, contains most of the heavy code,
including the actual derivative algorithm (in the "derivator()" method)
and an unparsing algorithm (translating from parse tree back to string,
in the "evaluateTree()" method).
The MenuGroup class (an inner
class of the main applet class) is my first use of a pop-up menu, in
this case to select a variable for partial derivatives.
- For development purposes, I use a short general-purpose
"wrapper"
application which loads an applet and displays it as a separate window
within the full application. The application includes code which allows
HTML parameters to be specified on the command line.
Revised - June 9, 2004
Back to Prof. Leathrum's Page
Back to MCIS Faculty
Back to the MCIS Department Home Page
Back to the
Jacksonville State University Home Page