|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||
com.numobjects.mathbeans.awt.CalcGraphPanel
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.
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 |
public CalcGraphPanel()
| Method Detail |
public java.lang.String getFormulae()
public void setFormulae(java.lang.String formulae)
formulae - formulae stringpublic void setPrecision(int value)
value - precision to setpublic int getPrecision()
public void setRounding(int value)
value - roundingpublic int getRounding()
public void setInsets(java.awt.Insets insets)
insets - insetspublic java.awt.Insets getInsets()
public void setVerticalSpacing(int value)
value - value to setpublic int getVerticalSpacing()
public void setHorizontalSpacing(int value)
value - value to setpublic int getHorizontalSpacing()
public void setFieldWidth(int value)
value - value to setpublic int getFieldWidth()
public void setFieldHeight(int value)
value - value to setpublic int getFieldHeight()
public void setDisplayLogo(boolean flag)
flag - value to setpublic boolean isDisplayLogo()
public void setDisplayInfo(boolean flag)
flag - value to setpublic boolean isDisplayInfo()
public void setHMargin(int value)
value - value to setpublic int getHMargin()
public void setVMargin(int value)
value - value to setpublic int getVMargin()
public void setGMargin(int value)
value - value to setpublic int getGMargin()
public void setResultNames(java.lang.String[] names)
names - result names to setpublic java.lang.String[] getResultNames()
public java.lang.String getResultName(int index)
public void setParameterField(java.lang.String parameterName,
java.awt.TextField field)
parameterName - parameter namefield - text field to tagpublic java.awt.TextField getParameterField(java.lang.String parameterName)
parameterName - parameter name
public void setResultField(java.lang.String resultName,
java.awt.TextField field)
field - text field to tagpublic java.awt.TextField getResultField(java.lang.String resultName)
public void setExtended(boolean flag)
flag - extended flagpublic boolean isExtended()
public void paint(java.awt.Graphics g)
g - graphicspublic java.awt.Dimension getMinimumSize()
public void setBackground(java.awt.Color c)
c - color
|
||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||