import java.awt.*;import java.awt.event.*;import java.beans.*;
import java.util.*;import java.io.*;import java.applet.Applet;
public class Elephant extends AppletAppBean implements ActionListener
{
But now we need two GraphWindows.
GraphWindow gw1=new GraphWindow(),gw2=new GraphWindow();
One way to present some instructions is in java.awt.Label's.
(The text in quotes provided as an argument is
what a Label displays wherever it is located)
Label instructions1=new Label("Try to set the domain and range
of ");
Label instructions2=new Label("the graph on the right so it looks
");
Label instructions3=new Label("the same as the one below.");
We also declare a java.awt.Button labelled
with the message "try another"
Button another=new Button("try another");
and a random number generator (from the java.util
package)
Random r=new Random();
which we will use to help generate a new graph
segment when the user chooses to try another.
FormulaFunction f=new FormulaFunction("(x^3+x^2-2*x)/2-1");
The class FormulaFunction is one of
several ways of defining something like a function provided in the qpr.math.svf
package.
Graph g=new FunctionGraph(f);
(and likewise FunctionGraph is one of
several ways of defining a graph provided in the qpr.math.grf
package)
As described also in the discussion of Graphit, although we need to declare the types and names of any variables that will be used in more than one method, the actual initialization assignments made above could have been deferred to (say) be included in the 'constructor' method below
public Elephant(){setSize(800,600); [
setup();}
(Often some of the work of a constructor will
be identified for convenience as a separate method. In this case it's kind
of pointless, but I was mindlessly modelling the code on that of another
class where it made more sense to do this. So if everything here between
[ and ] were deleted it would actually make no difference.)
public void setup() { ]
setBackground(new Color(255,255,220));setLayout(null);
add(gw1);gw1.setBounds(400,0,400,600); gw1.setGraph(g);
The last line above illustrates setting a GraphWindow
to display just a single Graph.
(In the Liner applet, we'll see that
he same end can be achieved by adding a GraphItem to the GraphSetManager)
Graph g1=((GraphItem)(gw1.getStuff())).getGraph();
if(g1==null)System.out.println("g1 is null");
(The above is just a bit of debugging code that
got left in. )
gw1.getRegion().setBounds(-2,2,-2,2);gw1.regionAdjusters.setup(1);
In qpr.math.plotkit, the class GraphWindow
has a method getRegion() which returns the GraphRegion
to be displayed, and a java.awt.Panel of regionAdjusters
which can be set up either as in Graphit (with zoom buttons,etc),
or, as here, with just number entry boxes for the domain and range bounds.
My GraphRegion.setBounds method takes as arguments the values
of xmin,xmax,ymin,and ymax. (It should probably be called something different
so as to avoid confusion with the use of the same name for the method sizing
java screen components in terms of pixels (and with a conflicting interpretation
of the arguments).)
add(gw2);gw2.setBounds(0,200,400,400);
gw2.setGraph(g);gw2.gridShow=false;
gw2.getRegion().setBounds(-0.1,0.1,-1.1,-0.9);
gw2.regionAdjusters.setBounds(0,0,0,0); (just
to make them inaccessible to the user)
add(instructions1);instructions1.setBounds(20,20,300,20);
add(instructions2);instructions2.setBounds(20,40,300,20);
add(instructions3);instructions3.setBounds(20,60,300,20);
add(another);another.setBounds(100,150,100,50);
This next bit is a standard Java construct for
making a Button do something
(for a different button and action, their names
should just replace 'another' and 'reset')
another.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){reset();}
});
}
void reset(){
This method just uses the java.util.Random
utility to help pick another bit of the graph.
double x=r.nextGaussian(),y=f.evalAt(x)+0.02*r.nextGaussian();
double w=Math.abs(0.5*r.nextGaussian()),x1=x-w/2,x2=x+w/2,y1=y-w/2,y2=y+w/2;
gw2.getRegion().setBounds(x1,x2,y1,y2);gw2.getRegion().changed=true;
}
and the rest goes basically the same way as the end of Graphit.java . . .
public void actionPerformed(ActionEvent e){}(this was left in by accident)
public void init(){setSize(800,600);}
public void addNotify(){super.addNotify();start();}(this
too)
public static void main(String[] args) {
Elephant jumbo=new Elephant();jumbo.init();frameIt(jumbo);
}
}//endClass(Elephant)