package tangentLine; import java.awt.*; import java.applet.Applet; import graphingApplet.*; import functionParser.*; import java.util.*; public class TangentLine extends GraphingApplet{ /////////// SUBCLASS INSTANCE VARIABLES HERE //// public TwoPoint[] points; //AN ARRAY TO HOLD THE POINTS ON THE GRAPH OF OUR FUNCTION double xmin;//Left edge of the graph double xmax;//Right edge of the graph double increment; // THE DISTANCE BETWEEN PIXELS ON THE CURRENT SCALE int pointCount; // THE NUMBER OF PIXELS ACROSS THE GRAPH double presentX; // X CORDINATE LAST CLICKED ON double presentY; // Y CORDINATE LAST CLICKED ON double x_L; //X COORDINATE OF THE LEFT OF THE GRAPH double x_R; //X COORDINATE OF THE RIGHT OF THE GRAPH double y_1L; //Y COORDINATE OF TANGENT LINE AT LEFT double y_1R; //Y COORDINATE OF TANGENT LINE AT RIGHT double tangentSlope; boolean clicked = false; public FunctionEnteringPanel fPanel; Evaluator eval; ///////////////////////////////////////////////// public void init(){ super.init(); ///////////// ADD FUNCTION PANELS ////////// fPanel = new FunctionEnteringPanel(mc,"(2+4*x)/(2+x^2)","x","f"); fep = new FunctionEnteringPanel[1]; fep[0] = fPanel; ////// SET CARD LABELS AND NAMES, INCLUDING NUMBER ////////// labels = new String[3]; labels[0] = "cursor position"; labels[1] = "graph range controls"; labels[2] = "slope info"; cardNames = new String[3]; cardNames[0] = "cursor"; cardNames[1] = "graph"; cardNames[2] = "line"; //////////////////////////////////////////// selector = new CardSelector(cardbox, labels, cardNames, mc); cr = new CursorReport(mc); gp = new GraphParameters(mc,this); ////// GET PROPER SLATE //// rs = new TangentSlate(mc,"line"); cards = new Panel[3]; cards[0] = cr; cards[1] = gp; cards[2] = rs; ////// NOW FOR THE GRAPH... gc = new GraphCanvas(mc,"graphcanvas"); //////THIS CREATES A CursorBadge OF THE MATCHING WIDTH cbdg = new CursorBadge(mc,(gc.getPreferredSize()).width); //AND THIS THE CORESPONDING GraphhLocationBadge. glbdg = new GraphLocationBadge(mc,(gc.getPreferredSize()).width); //FINALLY, THE COPYRIGHT PANEL: copyPanel = new CopyrightPanel("copyright Eric A. Carlen 1996, 1997. All rights reserved",font); //AT THIS POINT, STEP(1) IS ALMOST DONE -- THE FOUR KEY ARRAYS ARE READY TO BE USE TO CONSTRUCT //top AND bottom. top = new TopPanel(this); bottom = new BottomPanel(mc,fep); //NOW FOR STEP (2): add(top); add(bottom); mc.arrayConversion();//NOTHING THAT CALLS mc (EXCEPT TO SUBSCRIBE) ABOVE HERE... gc.updateParameters();//NOTHINNG THAT USES GRAPH PARAMETERS ABOVE HERE... selector.showNamedCard("cursor"); //NOW COMES INITIALIZATION OF SUBCLASS INSTANCE VARIABLES eval = mc.getEvaluator("f"); fPanel.setColor(50,50,255); //AND THEN FINALY, THE PRELIMINARY COMPUTATIONS. handleAllData();//THIS HAS TO BE DEFINED BELOW; ABSTRACT IN Super }//END OF INIT public void runNamedMethod(String name, int i,int j){ Graphics graphics = gc.getGraphics(); if(name.equals("click")){ mc.writeIntData("clicked",1); presentX = gc.pixelToX(i); presentY = eval.evaluate(presentX); clicked = true; mc.writeDoubleData("x_0",presentX); mc.writeDoubleData("y_0",presentY); tangentSlope = (10000000.0)*(eval.evaluate(presentX+0.0000001) - eval.evaluate(presentX)); mc.writeDoubleData("tangentSlope",tangentSlope); mc.updateSubscribers(); gc.paint(graphics); } }//END OF RUN NAMED METHOD public void handleAllData(){ //What we need to do here is to read in the graph size and location, //and then to fill in a corresponding array ot TwoPonts, points on the //graph of f, so that it is ready to be used by doAllPainting(). //First, initialize the variables holding graph information: xmin = mc.readDoubleData("xLeft"); xmax = xmin + mc.readDoubleData("graphWidth"); pointCount = mc.readIntData("graphSize"); increment = (xmax - xmin)/pointCount; //And now that we have the size, initialize our array of TwoPoionts. points = new TwoPoint[pointCount]; //We need the evaluator before we can fill this in, so call a method //of fPanel that prepares its Evaluator: try{fPanel.computeGrid();}catch(FunctionParsingException ex) {System.out.println("boo-boo");} //Now fill in the TwoPoints -- the dots to be connected below in the painting. for(int i=0;i