net.sourceforge.webcompmath.applets
Class GenericGraphApplet

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Panel
              extended by java.applet.Applet
                  extended by javax.swing.JApplet
                      extended by net.sourceforge.webcompmath.applets.GenericGraphApplet
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.ItemListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.RootPaneContainer
Direct Known Subclasses:
AnimatedGraph, Derivatives, EpsilonDelta, FamiliesOfGraphs, FunctionCombination, FunctionComposition, IntegralCurves, MultiGraph, Parametric, RiemannSums, SecantTangent, SimpleGraph

public class GenericGraphApplet
extends javax.swing.JApplet
implements java.awt.event.ActionListener, java.awt.event.ItemListener

GenericGraphApplet serves as a base class for applets that have a DisplayCanvas in the CENTER of a BorderLayout and that are configurable by a large number of applet parameters. This base class sets up the basic structure of the applet and processes many applet parameters. Most of the work is done in methods that can be overridden in subclasses. If the height of the applet is greater than 100 and if the applet parameter "LaunchButtonName" is not defined, then the main panel of the applet appears in the applet itself. Otherwise, the applet appears as a button. Clicking the button opens the main panel of the applet in a separate, resizable frame.

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JApplet
javax.swing.JApplet.AccessibleJApplet
 
Nested classes/interfaces inherited from class java.applet.Applet
java.applet.Applet.AccessibleApplet
 
Nested classes/interfaces inherited from class java.awt.Panel
java.awt.Panel.AccessibleAWTPanel
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  DisplayCanvas canvas
          The canvas for displaying axes, graphs, etc.
protected  javax.swing.JButton computeButton
          A button that the user presses to recompute the display.
protected  int[] defaultFrameSize
          The default size for the frame when the applet runs a launch button.
protected  WcmPanel exampleMenuPanel
          A panel containing examples specified in applet parameters, if any.
protected  float fontSize
          The font size to use for all fonts
protected  java.lang.String frameTitle
          The string that is used as the title of the frame, if the applet appears as a launch button.
protected  ExpressionInput functionInput
          An input box for inputting an expression.
protected  java.awt.Color ggBGColor
          The background color for the applet
protected  java.awt.Color ggFGColor
          The foreground color for the applet
protected  WcmPanel inputPanel
          A panel containing the functionInput.
protected  java.awt.Font lbf
          The font for the launch button
protected  LimitControlPanel limitsPanel
          Controls the limits of the x- and y-coordinates on the canvas.
protected  Controller mainController
          The Controller from the mainPanel.
protected  WcmPanel mainPanel
          The main panel of the applet, containing a display canvas and other components.
protected  java.util.Hashtable parameterDefaults
          A hash table that, if non-null, can hold values for applet params.
protected  Parser parser
          The parser which is used for parsing input from the functionInput box.
protected  java.awt.Color pBGColor
          The background color for the panel
protected  boolean presentation
          Whether to use thicker lines for projecting the graph on a screen.
protected  Variable xVar
          The variable that is created and added to the parser.
 
Fields inherited from class javax.swing.JApplet
accessibleContext, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
GenericGraphApplet()
           
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent evt)
          Respond when user clicks a button; not meant to be called directly.
protected  void addCanvasBorder()
          This method is called by setUpMainPanel() to add a border to the canvas (since the border is typically the last thing that should be added, on top of anything else in the canvas.
 void destroy()
          Closes the frame (if any) when the applet is destroyed.
protected  void doLoadExample(java.lang.String example)
          This method is called when the user loads an example from the example menu (if any).
protected  void doLoadFirstExample()
          Load the first example
protected  java.awt.Color getColorParam(java.lang.String data)
          Get The value of an applet parameter that specifies a color.
protected  java.awt.Color getColorParam(java.lang.String paramName, java.awt.Color defaultColor)
          Get The value of an applet parameter that specifies a color.
protected  double[] getNumericParam(java.lang.String paramName)
          Get The value of an applet parameter that consists of a list of numbers.
protected  double[] getNumericParam(java.lang.String paramName, double[] defaults)
          Get The value of an applet parameter that consists of a list of numbers.
 java.lang.String getParameter(java.lang.String paramName)
          Override the standard applet method getParameter(String) so that when no param value is provided in the applet tag, and if parameterDefaults is non-null, it will check for a value in the parameterDefaults.
protected  java.lang.String getParameter(java.lang.String paramName, java.lang.String defaultValue)
          Get the value of an applet parameter, but return a default if the value is null.
 void init()
          The init() method is called by the system to set up the applet.
 void itemStateChanged(java.awt.event.ItemEvent evt)
          Respond when user chooses an example from the example menu.
protected  WcmAxes makeAxes()
          Construct a set of Axes, based on applet params "AxesColor", "AxesLightColor", "XLabel", "YLabel", "LabelColor".
protected  TableFunction parseTableFuncDef(java.lang.String def)
          Create a TableFunction from a string.
protected  void setUpBottomPanel()
          This method checks the applet parameter "UseFunctionInput".
protected  void setUpCanvas()
          This method is called by mainPanel() to set up the display canvas and add it to the main panel.
protected  void setUpExampleMenu()
          This method processes applet parameters that specify examples for the applet and adds them to a menu of examples.
protected  void setUpLimitsPanel()
          This method sets up the limit control panel and adds it to the main panel.
protected  void setUpMainPanel()
          This method is called to set up the main panel of the applet.
protected  void setUpParameterDefaults()
          This method, which is empty in the GenericGraphApplet class, can be defined in a subclass to set default values for applet params that are different from the ones provided in this class.
protected  void setUpParser()
          This method is called by setUpMainPanel() to set up the parser to be used in the applet.
protected  void setUpTopPanel()
          This method is called by setUpMainPanel() to add a panel at the top of the applet.
 
Methods inherited from class javax.swing.JApplet
addImpl, createRootPane, getAccessibleContext, getContentPane, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isRootPaneCheckingEnabled, paramString, remove, repaint, setContentPane, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
 
Methods inherited from class java.applet.Applet
getAppletContext, getAppletInfo, getAudioClip, getAudioClip, getCodeBase, getDocumentBase, getImage, getImage, getLocale, getParameterInfo, isActive, newAudioClip, play, play, resize, resize, setStub, showStatus, start, stop
 
Methods inherited from class java.awt.Panel
addNotify
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getListeners, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

parser

protected Parser parser
The parser which is used for parsing input from the functionInput box.


mainPanel

protected WcmPanel mainPanel
The main panel of the applet, containing a display canvas and other components.


mainController

protected Controller mainController
The Controller from the mainPanel.


canvas

protected DisplayCanvas canvas
The canvas for displaying axes, graphs, etc.


limitsPanel

protected LimitControlPanel limitsPanel
Controls the limits of the x- and y-coordinates on the canvas.


functionInput

protected ExpressionInput functionInput
An input box for inputting an expression.


xVar

protected Variable xVar
The variable that is created and added to the parser. The default name of this variable is "x", but that can be changed using the "Variable" applet param. The variable is created in the setUpParser() method.


inputPanel

protected WcmPanel inputPanel
A panel containing the functionInput. It appears at the bottom of the main panel.


exampleMenuPanel

protected WcmPanel exampleMenuPanel
A panel containing examples specified in applet parameters, if any. It appears at the top of the main panel.


computeButton

protected javax.swing.JButton computeButton
A button that the user presses to recompute the display. (Ths user can also just press return in one of the input boxes.)


frameTitle

protected java.lang.String frameTitle
The string that is used as the title of the frame, if the applet appears as a launch button. This title is also used on the launch button, unless the applet parameter "LaunchButtonName" has a value. This is set in the init() method.


defaultFrameSize

protected int[] defaultFrameSize
The default size for the frame when the applet runs a launch button. Can be overidden by the FrameSize applet param.


fontSize

protected float fontSize
The font size to use for all fonts


presentation

protected boolean presentation
Whether to use thicker lines for projecting the graph on a screen. Classes that extend GenericGraphApplet should check this field when adding Drawables to a DisplayCanvas and adjust line widths, sizes, etc. as appropriate.


parameterDefaults

protected java.util.Hashtable parameterDefaults
A hash table that, if non-null, can hold values for applet params. While this is not set to a non-null value in this class, its value can be set in the setUpAppletDefaults() method. The values in this hash table will be returned by the getParameter() method when no value is provided in the applet. For example, this class uses a default value of "s" vor the the param "Variable". A subclass could put a different value in the parameterDefaults hash table, and this value will be used in preference to "x".


ggBGColor

protected java.awt.Color ggBGColor
The background color for the applet


ggFGColor

protected java.awt.Color ggFGColor
The foreground color for the applet


pBGColor

protected java.awt.Color pBGColor
The background color for the panel


lbf

protected java.awt.Font lbf
The font for the launch button

Constructor Detail

GenericGraphApplet

public GenericGraphApplet()
Method Detail

init

public void init()
The init() method is called by the system to set up the applet. If the applet does not appear as a button, then init() creates the main panel of the applet and calls setUpMainPanel to set it up. If any error occurs during calls to setUpParser(), setUpExampleMenu(), or setUpMainPanel(), then the applet will just show an error message, and stack trace will be printed to standard output.

Overrides:
init in class java.applet.Applet

setUpMainPanel

protected void setUpMainPanel()
This method is called to set up the main panel of the applet. The main panel object, of type WcmPanel, already exists in the variable mainPanel when it is called. It begins by calling setUpParser() and setUpExampleMenu(). This method processes applet parameters "Insets", "BackgroundColor", and "ForegroundColor" to set the gap and colors of the panel. It creates a DisplayCanvas and, if the applet parameter "UseLimitsPanel" is not equal to "no", it creates a LimitControlPanel. The coordinate limits are set using the "Limits" applet param, if presetn. This method assigns the Controller from the mainPanel to the member variable mainController. It then calls setUpBottomPanel(), setUpTopPanel(), setUpCanvas(), addCanvasBorder(), and, if there is a limits panel, setUpLimitsPanel(), in that order. Finally, if the value of the applet parameter "LoadFirstExample" is not "no", and if any examples were specifed, then the first ewxample is loaded.


doLoadFirstExample

protected void doLoadFirstExample()
Load the first example


setUpCanvas

protected void setUpCanvas()
This method is called by mainPanel() to set up the display canvas and add it to the main panel. The canvas already exists in the member variable canvas when this method is called. This method adds only a set of axes to the canvas, sets the mainController to report errors using the canvas, and adds the canvas to the CENTER of the main panel. This method processes applet parameters "UsePanner", "CanvasColor", "UseMouseZoom", and "UseOffscreenCanvas". Typically, this method will be overridden in subclasses to add more Drawable items to the canvas. In this case, super.setUpCanvas() should be called first.


makeAxes

protected WcmAxes makeAxes()
Construct a set of Axes, based on applet params "AxesColor", "AxesLightColor", "XLabel", "YLabel", "LabelColor".

Returns:
the newly made axes

addCanvasBorder

protected void addCanvasBorder()
This method is called by setUpMainPanel() to add a border to the canvas (since the border is typically the last thing that should be added, on top of anything else in the canvas. It processes the applet parameters "BorderWidth" and "BorderColor". If the border width is zero, no border is added. (The default width is 2.)


setUpBottomPanel

protected void setUpBottomPanel()
This method checks the applet parameter "UseFunctionInput". If the value is anything but "no", then a panel is created that contains an ExpressionInput (stored in the member variable functionInput) and possibly a ComputeButton and lable for the input box. This panel is a WcmPanel using BorderLayout. In is stored in the member variable inputPanel and is added to the SOUTH position of the mainPanel. The method also processes applet parameters "Function", "UseComputeButton", "ComputeButtonName", and "FunctionLabel". The ComputeButton, if it exists, is stored in the member variable computeButton. Note that nothing at all is done by this method if the value of the applet parameter "UseFunctionInput" is no.


setUpLimitsPanel

protected void setUpLimitsPanel()
This method sets up the limit control panel and adds it to the main panel. The limit control panel already exists when this method is called and is stored in the member variable limitsPanel. The applet parameters "TwoLimitsColumns", "UseSetLimitsButton", "UseZoomButtons", "UseEqualizeButtons", "UseRestoreButton", "UseSaveButton", "PanelBackground", and "LimitsOnLeft" are processed. The limits panel is set to report its errors using the display canvas.


setUpTopPanel

protected void setUpTopPanel()
This method is called by setUpMainPanel() to add a panel at the top of the applet. If there is an example menu, then the panel containing that menu is added to the NORTH position of the main panel. Otherwise, the value of the applet parameter named "PanelTitle" is checked. If this value exists, it is placed in a label at the top of the panel. The color of the text on the label is given by the valur of the applet parameter "TitleColor" (default is new Color(200,0,0)).


setUpExampleMenu

protected void setUpExampleMenu()
This method processes applet parameters that specify examples for the applet and adds them to a menu of examples. If any examples exist, they are placed in a menu and a panel is created to hold the menu. The panel is stored in the instance variable exampleMenuPanel, which is used in the setUpTopPanel() method. This method is called by init(). If you don't want to process examples, you could override it to do nothing. If you do want to process examples, you have to override the doLoadExample() method to process a string from the menu. Examples are specified in applet parameters named "Example", "Example1", "Example2", .... The value should consist of a name for the example (which appears in the menu) followed by a ";" followed by a string that defines the example. The string that defines the example is passed to the doLoadExample() method when the user loads the example.


setUpParser

protected void setUpParser()
This method is called by setUpMainPanel() to set up the parser to be used in the applet. When it is called, a parser already esists and is stored in the member variable named parser. This method configures the parser according to the values of the applet parameters "StandardFunctions", "Booleans", "OptionalStars", "OptionalParens", "Factorials", "OptionalFunctions", and "RationalExponents". It then looks for function defintions in applet parameters named "Define", "Define1", "Define2", ...., and if any are found the functions are added to the parser so that they can be used in expressions. A function definition can take a form such as "g(x)=x^2" or "fred(s,t) = 3*s + sin(t)", for example, or it can be defined by a table of values. A table function is defined using the syntax decribed in the parseTableFuncDef() method. Finally, a variable is created and added to the parser using the value of the applet param "Variable", with "x" as the default variable name.


doLoadExample

protected void doLoadExample(java.lang.String example)
This method is called when the user loads an example from the example menu (if any). The parameter is the string that defines the example. By default, this method does nothhing. It should be overridden to load the example.

Parameters:
example - the function to be graphed

setUpParameterDefaults

protected void setUpParameterDefaults()
This method, which is empty in the GenericGraphApplet class, can be defined in a subclass to set default values for applet params that are different from the ones provided in this class. The method should create a new HashTable, assign it to the instance variable parameterDefaults, and then add name/value pairs to the hash table. This method is called at the very beginning of the init() method.


getParameter

public java.lang.String getParameter(java.lang.String paramName)
Override the standard applet method getParameter(String) so that when no param value is provided in the applet tag, and if parameterDefaults is non-null, it will check for a value in the parameterDefaults. (The parameterDefaults instance variable can be defined in the setUpParameterDefaults() method.)

Overrides:
getParameter in class java.applet.Applet
Parameters:
paramName - the parameter to get
Returns:
the parameter's value

getParameter

protected java.lang.String getParameter(java.lang.String paramName,
                                        java.lang.String defaultValue)
Get the value of an applet parameter, but return a default if the value is null.

Parameters:
paramName - The name of the applet parameter.
defaultValue - The value to be returned if getParameter(paramName) is null.
Returns:
the parameter's value

getNumericParam

protected double[] getNumericParam(java.lang.String paramName)
Get The value of an applet parameter that consists of a list of numbers. The parameter value, if any, is parsed and returned as array of double values. The numbers can be separated by commas, spaces, tabs, or semicolons. If there is a parse error, null is returned.

Parameters:
paramName - the name of the parameter to get
Returns:
the parameter's value

getNumericParam

protected double[] getNumericParam(java.lang.String paramName,
                                   double[] defaults)
Get The value of an applet parameter that consists of a list of numbers. The parameter value, if any, is parsed and returned as array of double values. The numbers can be separated by commas, spaces, tabs, or semicolons.

Parameters:
paramName - The name of the applet parameter.
defaults - The value to be returned if getParameter(paramName) is null or is not a valid list of numbers.
Returns:
the parameter's array of values

getColorParam

protected java.awt.Color getColorParam(java.lang.String data)
Get The value of an applet parameter that specifies a color. The color can be specfied as a list of three numbers in the range 0 to 255 or by one of the standard color names ("black", "red", "blue", "green", "yellow", "cyan", "magenta", "gray", "darkgray", "lightgray", "pink", "orange", "white"). Color names are not case sensitive. If the value of the applet parameter is null does not specify a legal color, then the return value is null.

Parameters:
data - the name of the parameter
Returns:
value of the parameter

getColorParam

protected java.awt.Color getColorParam(java.lang.String paramName,
                                       java.awt.Color defaultColor)
Get The value of an applet parameter that specifies a color. The color can be specfied as a list of three numbers in the range 0 to 255 or by one of the standard color names ("black", "red", "blue", "green", "yellow", "cyan", "magenta", "gray", "darkgray", "lightgray", "pink", "orange", "white"). Color names are not case sensitive.

Parameters:
paramName - The name of the applet parameter.
defaultColor - The value to be returned if getParameter(paramName) is null or is not a valid color.
Returns:
the parameter's value as a Color

destroy

public void destroy()
Closes the frame (if any) when the applet is destroyed.

Overrides:
destroy in class java.applet.Applet

parseTableFuncDef

protected TableFunction parseTableFuncDef(java.lang.String def)
Create a TableFunction from a string. The string can start with the word "table", which is ignored. The next item can optionally be one of the table styles "smooth", "linear", or "step". The default is "smooth". Then values must be specified. If the next word is "intervals", it can be followed by numbers giving the number of intervals, the minumum x, and the maximum x, and the y-values for up to (intervals+1) points evenly distributed between xmin and xmax (unassigned y-values will be zero). If no data is specified, a table function with 6 intervals between xmin = -5 and xmax = 5 and all y-values zero is created. If "intervals" is not specified, the remaining items are numbers giving pairs of (x,y)-values. Items can be separated spaces, tabs, and commas. A ParseError will be thrown if the data is illegal.

Parameters:
def - string that defines the TableFunction to create
Returns:
the newly created TableFunction

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent evt)
Respond when user clicks a button; not meant to be called directly.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
evt - event generated when user clicks a button

itemStateChanged

public void itemStateChanged(java.awt.event.ItemEvent evt)
Respond when user chooses an example from the example menu. (This will only happen if the param UseLoadButton is not set to "yes".)

Specified by:
itemStateChanged in interface java.awt.event.ItemListener
Parameters:
evt - event created when user makes a menu selection