//////////////////////////////////////////
//  Elephant.java                      //
////////////////////////////////////////
import qpr.math.alg.*; import qpr.math.svf.*; import qpr.math.grf.*;
import qpr.math.plotkit.*; import qpr.util.beanstuff.*;

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
 { GraphWindow gw1=new GraphWindow(),gw2=new GraphWindow();
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.");
 
 Button another=new Button("try another");
 Random r=new Random();

FormulaFunction f=new FormulaFunction("(x^3+x^2-2*x)/2-1");
Graph g=new FunctionGraph(f);

public Elephant(){setSize(800,600);setup();}

public void setup() {
 setBackground(new Color(255,255,220));setLayout(null);
 add(gw1);gw1.setBounds(400,0,400,600); gw1.setGraph(g);
 Graph g1=((GraphItem)(gw1.getStuff())).getGraph();
 if(g1==null)System.out.println("g1 is null");
 gw1.getRegion().setBounds(-2,2,-2,2);gw1.regionAdjusters.setup(1);
 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);
 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);
 another.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){reset();}
        });
      }

void reset(){
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;
    }

public void actionPerformed(ActionEvent e){}

public void init(){setSize(800,600);}

public void addNotify(){super.addNotify();start();}
 

public static void main(String[] args) {
Elephant jumbo=new Elephant();jumbo.init();frameIt(jumbo);
       }

}//endClass(Elephant)