[ prev | main | next ]

Slider Graph



This applet that draws the graph of a function that can include
references to the "constants" a, b, and c. The values of these
constants can be adjusted using the sliders at the bottom of the
applet. The applet responds to the mouse actions click, shift-click,
click-and-drag, and right-click-and-drag. I've added another
buttons to the limit control panel. The "Equalize Axes" button
adjusts the x- and y-limits so that the scales on the two axes
are the same.

The following source code shows how the applet is built from
WCM components. Most of the comments are for features that were
not already covered in previous examples.



import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JApplet;
import javax.swing.JLabel;

import net.sourceforge.webcompmath.awt.ComputeButton;
import net.sourceforge.webcompmath.awt.Controller;
import net.sourceforge.webcompmath.awt.DisplayLabel;
import net.sourceforge.webcompmath.awt.ExpressionInput;
import net.sourceforge.webcompmath.awt.VariableSlider;
import net.sourceforge.webcompmath.awt.WcmPanel;
import net.sourceforge.webcompmath.data.Parser;
import net.sourceforge.webcompmath.data.Value;
import net.sourceforge.webcompmath.data.Variable;
import net.sourceforge.webcompmath.draw.DisplayCanvas;
import net.sourceforge.webcompmath.draw.DrawBorder;
import net.sourceforge.webcompmath.draw.Graph1D;
import net.sourceforge.webcompmath.draw.LimitControlPanel;
import net.sourceforge.webcompmath.draw.Panner;
import net.sourceforge.webcompmath.draw.WcmAxes;

/**
* Example WCM applet
*/
public class SliderGraph extends JApplet {

private static final long serialVersionUID = -1499011553591404783L;

private DisplayCanvas canvas;

WcmPanel makeSliderPanel(VariableSlider v) {
/*
* A small utility routing that makes a JCMPanel that contains a
* VariableSlider and a DisplayLabel that shows the value of the
* variable associated with that slider.
*/
WcmPanel p = new WcmPanel();
p.add(v, BorderLayout.CENTER);
p.add(new DisplayLabel(v.getName() + " = #", new Value[] { v }),
BorderLayout.EAST);
return p;
}

/**
* @see java.applet.Applet#init()
*/
public void init() {

Parser parser = new Parser();
Variable x = new Variable("x");
parser.add(x);

/*
* Create the three VariableSliders. In this case, the sliders have
* names. There is also a Variable associated with each slider, which
* has the same name. This variable is added to the parser which is
* passed as the fourth parameter to the constructor, making it possible
* to use "a", "b", and "c" in expressions parsed by the parser.
* Adjusting the value on a slider changes the value of the associated
* variable, and therefore changes the value of any expression that
* refers to that variable. The second and third parameters to the
* constructor give the minimum and maximum Values on the slider.
* Passing "null,null" uses the defaults, namely new Constant(-5) and
* new Constant(5).
*/
VariableSlider a = new VariableSlider("a", null, null, parser);
VariableSlider b = new VariableSlider("b", null, null, parser);
VariableSlider c = new VariableSlider("c", null, null, parser);

canvas = new DisplayCanvas();
canvas.setHandleMouseZooms(true);
canvas.add(new Panner());

LimitControlPanel limits = new LimitControlPanel(
LimitControlPanel.SET_LIMITS | LimitControlPanel.RESTORE
| LimitControlPanel.EQUALIZE, false);
limits.addCoords(canvas);

ExpressionInput input = new ExpressionInput("a*x^2 + b*x + c", parser);
Graph1D graph = new Graph1D(input.getFunction(x));

ComputeButton button = new ComputeButton("Graph it!");

canvas.add(new WcmAxes());
canvas.add(graph);
canvas.add(new DrawBorder(Color.darkGray, 2));

WcmPanel main = new WcmPanel(); // Build interface out of WcmPanels!
main.add(canvas, BorderLayout.CENTER);
main.add(limits, BorderLayout.EAST);
WcmPanel bot = new WcmPanel(5, 1);
main.add(bot, BorderLayout.SOUTH);

bot.add(new JLabel(
"Enter a function f(x), which can use the constants a, b, and c:"));
WcmPanel inputPanel = new WcmPanel();
bot.add(inputPanel);
inputPanel.add(input, BorderLayout.CENTER);
inputPanel.add(button, BorderLayout.EAST);

bot.add(makeSliderPanel(a)); // Create and add the sliders.
bot.add(makeSliderPanel(b));
bot.add(makeSliderPanel(c));

// Set up error reporting.
Controller controller = main.getController();
controller.setErrorReporter(canvas);
limits.setErrorReporter(canvas);

main.gatherInputs();
/*
* Set up main panel to respond to changes in input objects. This works
* since the interface is built of WcmPanels. For the same reason, I
* don't have to add the objects the the controller.
*/

// Set controller to respond to button.
button.setOnUserAction(controller);

setBackground(Color.lightGray);
setLayout(new BorderLayout());
add(main, BorderLayout.CENTER);

} // end init()

} // end class SliderGraph



[ prev | main | next ]