com.numobjects.mathbeans.awt
Class CalcGraphPanel

com.numobjects.mathbeans.awt.CalcGraphPanel

public class CalcGraphPanel
implements java.io.Serializable

Class CalcGraphPanel (Calculator graph panel bean) provides the following functionality: based solely on a string representing a set of several mathematical expressions with any number of variables, automatically constructs and displays a mini-calculator and graph panel for this expression. The mini-calculator panel is a CalcPanel bean. It provides the possibility to calculate the results according to the formulas. The graph panel lets the user plot any of the result as a function of any of its variables. For this purpose there is a graph parameter panel containing the Plot and Print buttons, choices for the function and independent variable and fields for specifying the range of the independent variable. This bean provides a unique possibility of creating various calculators, static or dynamic, avoiding the hassle of setting up GUI, adding logic, dispatching events, actual plotting, etc., etc. The user's only input is the set of formulas itself, all the rest is figured out by the bean.

The formulas must have the form

Result1=expression1;...ResultN=expressionN

Here "ResultX" is any string providing the name for the result, e.g. "Total amount". The "expressionX" must be a valid non-constant mathematical expression (i.e. must contain variables). Variable names in the expression cannot contain spaces; if there is a need for the name to consist of several words, use quotes around the name in the formula. Here is one simple example:

"Total tax"=Quantity*"Item price"*"Tax rate"/100;
"Total amount"=Quantity*"Item price"*(1+"Tax rate"/100)+Shipping

The resulting calculator panel would contain six fields labeled "Quantity", "Item price", "Tax rate", "Shipping", "Total tax" and "Total amount". As soon as one enters values in the first four fields and presses Enter, the fields "Total tax" and "Total amount" will display the computed values. Missing values are treated as zero.

The graph parameter panel would contain the variable names "Quantity", "Item price", "Tax rate" and "Shipping" in the choice. It would also contain fields for entering the independent variable range. To plot the graph, enter the required values, choose the dependent and independent variables and press the Plot button. On the calculator panel, the field corresponding to the chosen independent variable may contain anything as well as be blank. The fields for the other variables must contain values. If a field is blank, the value is assumed to be zero.

The graph panel is initially blank. After the graph is plotted it would display also the X and Y axes if they fall within the graph range, and marks for measurements in the X and Y directions. Also, an info string is shown. It indicates the scaling used for X and Y, and the ratio between the displayed measurements in the X and Y direction. Another optional information describing the dependencies is shown at the bottom. After the graph is plotted, the Print button gets enabled, and the graph can be printed.

Here is the code that you have to include in your program:

import com.numobjects.mathbeans.awt.*;
...
String formula = "\"Total tax\"=Quantity*\"Item price\"*\"Tax rate\"/100;\"Total amount\"=Quantity*\"Item price\"*(1+\"Tax rate\"/100)+Shipping";
CalcGraphPanel panel = new CalcGraphPanel ();
panel.setFormula (formula);

The calculator graph panel is ready to be added to your container.

The parameter entry fields can be any subclasses of java.awt.TextField, but the default entry fields are in fact CalcFields, so it is safe to cast them to this type if desired as well as use them to compute constant expressions. The panel is dynamically resizable with the automatic adjustment of the graph. It can also be rolled up to a usual CalcPanel and extended to its full form by pressing a single (+/-) button. If such resizing occurs, the parent of the panel is notified of the componentResized event (with null ComponentEvent), so it can adjust its size accordingly.

The new feature in version 2 is the ability to support several formulas.

See Also:
CalcField, CalcPanel, Serialized Form

Constructor Summary
CalcGraphPanel()
          Constructor
 
Method Summary
 int getFieldHeight()
          Gets entry field height
 int getFieldWidth()
          Gets entry field width
 java.lang.String getFormulae()
          Gets formulae for the panel
 int getGMargin()
          Gets margin for the graph panel (vertical space between the graph itself and the graph display area)
 int getHMargin()
          Gets horizontal margin for the graph panel
 int getHorizontalSpacing()
          Gets horizontal spacing between fields
 java.awt.Insets getInsets()
          Gets insets
 java.awt.Dimension getMinimumSize()
          Gets minimal size for the panel
 java.awt.TextField getParameterField(java.lang.String parameterName)
          Gets parameter entry field for given parameter name
 int getPrecision()
          Gets precision
 java.awt.TextField getResultField(java.lang.String resultName)
          Gets field for the result
 java.lang.String getResultName(int index)
          Gets name (label) for the result field
 java.lang.String[] getResultNames()
          Gets names (labels) for the result fields
 int getRounding()
          Gets rounding
 int getVerticalSpacing()
          Gets vertical spacing between fields
 int getVMargin()
          Gets vertical margin for the graph panel
 boolean isDisplayInfo()
          Gets info display flag
 boolean isDisplayLogo()
          Gets logo display flag
 boolean isExtended()
          Gets extended view flag (whether the graph panel is visible)
 void paint(java.awt.Graphics g)
          Paints the panel
 void setBackground(java.awt.Color c)
          Sets background
 void setDisplayInfo(boolean flag)
          Sets info display flag
 void setDisplayLogo(boolean flag)
          Sets logo display flag
 void setExtended(boolean flag)
          Sets extended view of the panel (to include the graph panel)
 void setFieldHeight(int value)
          Sets entry field height
 void setFieldWidth(int value)
          Sets entry field width
 void setFormulae(java.lang.String formulae)
          Sets formulae
 void setGMargin(int value)
          Sets margin for the graph panel (vertical space between the graph itself and the graph display area)
 void setHMargin(int value)
          Sets horizontal margin for the graph panel
 void setHorizontalSpacing(int value)
          Sets horizontal spacing
 void setInsets(java.awt.Insets insets)
          Sets insets for the panel
 void setParameterField(java.lang.String parameterName, java.awt.TextField field)
          Tags a text field as a parameter field for given parameter name
 void setPrecision(int value)
          Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6
 void setResultField(java.lang.String resultName, java.awt.TextField field)
          Tags a text field as the result field
 void setResultNames(java.lang.String[] names)
          Sets result labels
 void setRounding(int value)
          Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding)
 void setVerticalSpacing(int value)
          Sets vertical spacing
 void setVMargin(int value)
          Sets vertical margin for the graph panel
 

Constructor Detail

CalcGraphPanel

public CalcGraphPanel()
Constructor
Method Detail

getFormulae

public java.lang.String getFormulae()
Gets formulae for the panel
Returns:
formulae

setFormulae

public void setFormulae(java.lang.String formulae)
Sets formulae
Parameters:
formulae - formulae string

setPrecision

public void setPrecision(int value)
Sets precision (the number of significant digits in the calculation); range 1 - 15, default precision is 6
Parameters:
value - precision to set

getPrecision

public int getPrecision()
Gets precision
Returns:
precision value (the number of significant digits in the calculation)

setRounding

public void setRounding(int value)
Sets rounding (the number of digits displayed after the decimal dot); range -1 - 15, default rounding is -1 (no rounding)
Parameters:
value - rounding

getRounding

public int getRounding()
Gets rounding
Returns:
rounding value (the number of digits displayed after the decimal dot)

setInsets

public void setInsets(java.awt.Insets insets)
Sets insets for the panel
Parameters:
insets - insets

getInsets

public java.awt.Insets getInsets()
Gets insets
Returns:
insets

setVerticalSpacing

public void setVerticalSpacing(int value)
Sets vertical spacing
Parameters:
value - value to set

getVerticalSpacing

public int getVerticalSpacing()
Gets vertical spacing between fields
Returns:
vertical spacing

setHorizontalSpacing

public void setHorizontalSpacing(int value)
Sets horizontal spacing
Parameters:
value - value to set

getHorizontalSpacing

public int getHorizontalSpacing()
Gets horizontal spacing between fields
Returns:
horizontal spacing

setFieldWidth

public void setFieldWidth(int value)
Sets entry field width
Parameters:
value - value to set

getFieldWidth

public int getFieldWidth()
Gets entry field width
Returns:
field width

setFieldHeight

public void setFieldHeight(int value)
Sets entry field height
Parameters:
value - value to set

getFieldHeight

public int getFieldHeight()
Gets entry field height
Returns:
field height

setDisplayLogo

public void setDisplayLogo(boolean flag)
Sets logo display flag
Parameters:
flag - value to set

isDisplayLogo

public boolean isDisplayLogo()
Gets logo display flag
Returns:
logo flag

setDisplayInfo

public void setDisplayInfo(boolean flag)
Sets info display flag
Parameters:
flag - value to set

isDisplayInfo

public boolean isDisplayInfo()
Gets info display flag
Returns:
info flag

setHMargin

public void setHMargin(int value)
Sets horizontal margin for the graph panel
Parameters:
value - value to set

getHMargin

public int getHMargin()
Gets horizontal margin for the graph panel
Returns:
margin value

setVMargin

public void setVMargin(int value)
Sets vertical margin for the graph panel
Parameters:
value - value to set

getVMargin

public int getVMargin()
Gets vertical margin for the graph panel
Returns:
margin value

setGMargin

public void setGMargin(int value)
Sets margin for the graph panel (vertical space between the graph itself and the graph display area)
Parameters:
value - value to set

getGMargin

public int getGMargin()
Gets margin for the graph panel (vertical space between the graph itself and the graph display area)
Returns:
margin value

setResultNames

public void setResultNames(java.lang.String[] names)
Sets result labels
Parameters:
names - result names to set

getResultNames

public java.lang.String[] getResultNames()
Gets names (labels) for the result fields
Returns:
result label names

getResultName

public java.lang.String getResultName(int index)
Gets name (label) for the result field
Returns:
result label name

setParameterField

public void setParameterField(java.lang.String parameterName,
                              java.awt.TextField field)
Tags a text field as a parameter field for given parameter name
Parameters:
parameterName - parameter name
field - text field to tag

getParameterField

public java.awt.TextField getParameterField(java.lang.String parameterName)
Gets parameter entry field for given parameter name
Parameters:
parameterName - parameter name
Returns:
parameter field

setResultField

public void setResultField(java.lang.String resultName,
                           java.awt.TextField field)
Tags a text field as the result field
Parameters:
field - text field to tag

getResultField

public java.awt.TextField getResultField(java.lang.String resultName)
Gets field for the result
Returns:
result field

setExtended

public void setExtended(boolean flag)
Sets extended view of the panel (to include the graph panel)
Parameters:
flag - extended flag

isExtended

public boolean isExtended()
Gets extended view flag (whether the graph panel is visible)
Returns:
extended view flag

paint

public void paint(java.awt.Graphics g)
Paints the panel
Parameters:
g - graphics

getMinimumSize

public java.awt.Dimension getMinimumSize()
Gets minimal size for the panel
Returns:
minimal size

setBackground

public void setBackground(java.awt.Color c)
Sets background
Parameters:
c - color