Example Tool for the MVT

This document goes through a single Tool (the PlotTool) in detail. The full source code is located here.

The package declaration

Each tool (or related groups of tools) are located in a package:

package tools.plottool;

The class declaration

If the tool is to only have a single plotting panel, it should extend GraphicsTool.

public class PlotTool extends GraphicsTool  

The member variables

This section obviously varies from tool to tool. Here are some of the reasons for the member variables in this case:

  • There is only one function and one variable, and these are member variables. In this case we also use the derivative of the function, and this is also stored.
  • Since the graphics object that will be needed is a Line, this is also kept as a member variable.
  • Lastly, the input and options panels are stored. This is very important and should be done for each tool. The classes PlotInput must extend InputPanel and PlotOptions must extend OptionsPanel
   
    // Stores information about the function, its derivative and
    // its independent variable. 
    
    private Function function = null;
    private Function derivative = null;
    private Variable indVar = null;

        
    // This is where the plotting of the functions is done.

    private Line line = null;


    // The input and option panels is where the user can 
    // add function input and set options such as color. 
    
    
    private PlotInput inputPanel = new PlotInput();
    private PlotOptions optionsPanel = new PlotOptions();

The Constructor

The constructor has a number of key features:

  • The call to the superclass constructor. super("Function Plotter"). The string gives the name of the internal window.
  • The lines super.setInputPanel(inputPanel); and super.setOptionsPanel(optionsPanel); is an easy way to setup the input and options features of the tool.
  • The caret listener is specific to this tool. If the variable is changed then input panel is updated immediately.
  • Buttons on the input panel are added. Both of the listeners are included in this class as inner classes.
   public PlotTool() 
    { 
	super("Function Plotter");
	super.setInputPanel(inputPanel);
	super.setOptionsPanel(optionsPanel);

	setHelpFile("FunctionPlotter/plot1d.html","Function Plotter Help");
	
	
	// Add the Variable update Listener

	inputPanel.indVariableBox.getTextBox().
	    addCaretListener(new CaretListener(){
		    public void caretUpdate(CaretEvent e){

      			inputPanel.functionBox
			    .setLabel("f("+inputPanel.indVariableBox);
		    }
		});

	// Add a listener to the plot button
	
	inputPanel.plotButton.addActionListener(new PlotButtonListener()); 

	inputPanel.clearButton.addActionListener(new ClearButtonListener());
    } 

Other method

The following methods are also in the PlotTool:

  • private void createPoints(): This method gets an array of points for the plotting. In this case, the plotting method is adaptive in that it adds more points for highly oscillatory functions.
  • private void parseOptions(): This method parses all of the options available.
  • private void parseInput(): This method parses all of the input within the InputPanel