[ next | main ]

Arithmetic Applet

This is a simple applet that computes x+y, x-y, x*y, and x/y
continuously, as you type values for x and y:

The source code for this applets shows how it was assembled
from WCM components:

import javax.swing.*;
import java.awt.Color;
import java.awt.BorderLayout;
// Packages that define some WCM classes.
import net.sourceforge.webcompmath.data.*;
import net.sourceforge.webcompmath.awt.*;

* This applet is an example of how to use WCM to build a simple math applet.
public class ArithmeticApplet extends JApplet {

private static final long serialVersionUID = 5161593440140835445L;

* The makeLabel() routine is just a small utility routine that makes a
* label containing a given string. Labels made with this routine are used
* in the little boxes on the left side of the above applet.

JLabel makeLabel(String str) {
JLabel lab = new JLabel(str, JLabel.RIGHT);
lab.setBackground(new Color(255, 255, 220));
lab.setOpaque(true); // important in Swing
return lab;

* The makeDisplayLable() routine is a small utility routine that makes a
* "DisplayLabel". A DisplayLabel is one of the WCM components whose purpose
* is to display one or more Values. A "Value" is just an object that
* implements the interface net.sourceforge.webcompmath.data.Value. It has
* an assoicated numeric value that can be retrieved with getVal(). This is
* a fundamental interface in the WCM system which can represent constant
* values, variables, and results of computations. DisplayLabels are used
* for the lower four boxes on the right of the above applet.

JLabel makeDisplayLabel(Value val) {
JLabel lab = new DisplayLabel("#", val);
// In the string displayed by this label, the # is
// replaced by the value of val.
lab.setBackground(new Color(255, 255, 220));
return lab;

* The standard Applet routine, init(), sets up and lays out the applet. For
* a WCM Applet, there is usually not much to do besides setting it up. The
* WCM components are "active", and they pretty much take care of themselves
* if they are set up properly.
* @see java.applet.Applet#init()
public void init() {

setBackground(Color.blue); // Set the background color and
setLayout(new BorderLayout()); // layout of the applet as a whole. The
// whole applet will be filled with a single WcmPanel.

WcmPanel main = new WcmPanel(6, 2, 2);
* The applet will be built with WcmPanels, a subclass of JPanel,
* defined in net.sourceforge.webcompmath.awt. If an applet is
* constructed using WcmPanels, a lot of the WCM setup is taken care of
* automatically (at some possible loss of efficiency). This WcmPanel
* constructor creates a panel with a GridLayout with 6 rows and two
* columns, and with 2-pixel gaps between the rows and columns.
add(main, BorderLayout.CENTER);
// The WcmPanel will fill the whole applet.

main.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
// This allows a two-pixel-wide blue border to show around the edges.

Parser parser = new Parser();
* A Parser can be used to turn strings into Expressions. An Expression
* is a Value that is computed from some mathematical formula such as
* x+y or 3*sin(x^2-2).

VariableInput xInput = new VariableInput("x", "0", parser);
VariableInput yInput = new VariableInput("y", "0", parser);
* A VariableInput is a text input box where the user can input the
* value of a variable. The variable can be used in Expressions,
* provided that the variable is registered with the Parser that makes
* the Expressions. The two lines above make two VariableInputs for
* inputting the values of two variables. The names of the variables are
* "x" and "y", and they are registered with the Parser named parser
* which was created above.

main.add(makeLabel("Input x = "));
// Add components for the top row of the applet.

main.add(makeLabel("Input y = ")); // Components for the second row.

main.add(makeLabel("x + y = ")); // Components for the third row.
* Here, parser.parse("x+y") creates an Expression that represents the
* value of the formula x+y. x and y are the names for the values in the
* input boxes xInput and yInput, so this formula represents the sum of
* the numbers input by the user. Note that and Expression is a type of
* Value object, and so can be passed to the makeDisplayLabel() routine,
* which is defined above. This routine creates a label that shows the
* value of the Expression. Note: For the simple expression x+y, I could
* have used the ValueMath class to create the value object, instead of
* a parser. Just replace parser.parse("x+y") with: new
* ValueMath(xInput,yInput,'+'). ValueMath objects would let me do
* without the parser entirely in this applet, but parsers are necessary
* for more complicated expressions, and they are used for expressions
* entered by the user.

main.add(makeLabel("x - y = ")); // Components for the fourth row.

main.add(makeLabel("x * y = ")); // Components for the fifth row.

main.add(makeLabel("x / y = ")); // Components for the sixth row.

Controller c = main.getController();
* A Controller is another of the fundamental WCM classes. A controller
* makes things happen. It "listens" for user actions and does the
* computations necessary to update any WCM components that it controls.
* A WcmPanel has a controller for recomputing the WCM components in the
* panel. Calling main.getController() retrieves the Controller for the
* WcmPanel, main.

* A Controller must still be told what user actions to listen for.
* These two lines set up the Controller, c, to listen for any changes
* in the text in the input boxes xInput and yInput. Thus, every time
* the user types a character in one of these boxes, all the
* DisplayLabels are recomputed.

} // end init()

} // end class ArithmeticApplet

[ next | main ]