import java.awt.*; import java.applet.Applet; public class Newton extends Applet { public final static int ADD = 0; public final static int EVAL = 1; public final static int GRAPH = 2; public final static int STATES = 3; public void init() { // Do whatever work that the Applet wants to do on init. super.init(); // Make an Interpolator: the Interpolator class is the heart // of the whole thing. It does everything. p = new Interpolator(); // I don't want a GridLayout or anything like that. I want // to place my components MYSELF setLayout(null); // These dimensions look right. resize(400,242); // A big text area to remind you what values you gave it // as well as some debug info (namely the a value for each // point given). txtValues=new TextArea(7,45); // Make it: 7x45 txtValues.reshape(15,10,360,115); // Resize the Widget add(txtValues); // Manage the Widget txtValues.setEditable(false); // Editing is BAD // A text area for entering data for x txtXVal=new TextField(15); // Make it txtXVal.reshape(50,135,125,30); // Resize it add(txtXVal); // Manage the Widget txtXVal.requestFocus(); // I want the focus // A text area for entering data for y txtYVal=new TextField(15); // I'm sure you get the idea txtYVal.reshape(245,135,125,30); add(txtYVal); // And m... txtMVal=new TextField(15); txtMVal.reshape(245,170,125,30); add(txtMVal); txtMVal.hide(); // For graph only. // A label to say which one is X lblXvalue=new Label("X="); lblXvalue.reshape(15,135,25,30); add(lblXvalue); // And one to say which one is Y lblYvalue=new Label("Y="); lblYvalue.reshape(210,135,30,30); add(lblYvalue); // I wonder what this one is? lblMvalue=new Label("M="); lblMvalue.reshape(210,170,30,30); add(lblMvalue); lblMvalue.hide(); // Peekaboo // A status line to tell user what is going on lblStatus=new Label("Program is running."); lblStatus.reshape(5,220,385,15); add(lblStatus); // Button to clear the polynomial. VERY DANGEROUS. cmdClear=new Button("Clear Polynomial"); cmdClear.setFont(new Font("Dialog",Font.BOLD,16)); cmdClear.reshape(190,170,180,30); add(cmdClear); // A dropdown box to say what you want to do. cmbChoice= new Choice(); //add(cmbChoice); // WM wants it HERE! cmbChoice.addItem("Add"); cmbChoice.addItem("Eval"); cmbChoice.addItem("Graph"); cmbChoice.reshape(15,175,120,30); add(cmbChoice); // Initialization for choice cmbChoice.select(ADD); selectedCmbChoice(); // A button to say DO IT cmdOK=new Button("OK"); cmdOK.reshape(140,170,45,30); add(cmdOK); } // Central Event Handler. public boolean handleEvent(Event event) { if (event.id == Event.ACTION_EVENT && event.target == cmbChoice) { selectedCmbChoice(); return true; } else if (event.id == Event.ACTION_EVENT && event.target == cmdOK) { clickedCmdOK(); return true; } else if (event.id == Event.ACTION_EVENT && event.target == cmdClear) { clickedCmdClear(); return true; } else if (event.id == Event.KEY_PRESS && event.key == 9) { handleTabEvent(event); return true; } else if (event.id == Event.KEY_PRESS && event.key == 10) { clickedCmdOK(); return true; } return super.handleEvent(event); } // Handles TAB, which signals a focus change public void handleTabEvent(Event event) { Component lookup[] = {txtXVal,txtYVal,txtMVal,cmbChoice,cmdOK}; int MOVE[][][] = { // State 0 = ADD { { 1, 4, 2, 0, 3, 0 }, // Shift is up { 3, 0, 2, 4, 1, 0 } }, // Shift is down // State 1 = EVAL { { 1, 4, 2, 0, 3, 0 }, // Shift is up { 3, 0, 2, 4, 1, 0 } }, // Shift is down // State 2 = GRAPH { { 1, 2, 4, 0, 3, 0 }, // Shift is up { 3, 0, 1, 4, 2, 0 } } // Shift is down }; // Java doesn't like treating booleans as integers. int sd = event.shiftDown() ? 1 : 0; int me; // Which component am I? for (me=0;me "+ p.double_to_string(yn)+" ("+ p.double_to_string(p.getA(p.size()-1))+ ")\n"); // Clear the value boxes for the user's convienence. txtXVal.setText(""); txtYVal.setText(""); } // This is a simplified verson of the above public void clickedCmdEval() { double xn, yn; Double d = new Double(0.0); // Parse X try { xn = d.valueOf(txtXVal.getText()).doubleValue(); } catch (NumberFormatException e) { txtXVal.requestFocus(); lblStatus.setText("Illegal X Value for Eval"); return; } // Evaluate the function at x yn = p.Eval(xn); // Set the text of the Y value to reflect this evaluation. txtYVal.setText(p.double_to_string(yn)); } // The user wants a graph. Let's go make one public void clickedCmdGraph() { double xmin, xmax; int m; Double d=new Double(0.0); // for string conversion Integer i=new Integer(0); // for string conversion // First we go grab xmin and xmax (aka a and b), catching // any exceptions that arise try { xmin = d.valueOf(txtXVal.getText()).doubleValue(); } catch (NumberFormatException e) { txtXVal.requestFocus(); lblStatus.setText("Illegal a value for graph"); return; } try { xmax = d.valueOf(txtYVal.getText()).doubleValue(); } catch (NumberFormatException e) { txtYVal.requestFocus(); lblStatus.setText("Illegal b value for graph"); return; } // Perform a sanity check if (xmin >= xmax) { txtXVal.requestFocus(); lblStatus.setText("b must be strictly greater than a"); return; } // Parse m. try { m = i.valueOf(txtMVal.getText()).intValue(); } catch (NumberFormatException e) { txtMVal.requestFocus(); lblStatus.setText("Illegal M value"); return; } // Another sanity check if (m <= 0) { txtMVal.requestFocus(); lblStatus.setText("M must be positive."); return; } // Make a new Graph and let Graph do all the work. Graph theGraph = new Graph(); theGraph.x = p.MiddlePoints(xmin,xmax,m); theGraph.y = p.MultiEval(theGraph.x); theGraph.show(); // You never want two graphs in a row... let's pop the user // back into ADD mode. cmbChoice.select(ADD); selectedCmbChoice(); return; } // We have made a choice. public void selectedCmbChoice() { // Find out what choice we made for (state=0; state