package freegraph; import java.awt.*; import java.awt.event.*; import java.applet.*; /**** * Applet to solve equations. * Provides an GUI for the EquationSolver class */ public class EquationSolvingApplet extends Applet implements ActionListener { private TextField txtEquation; private Button btnSolve; private Label lblSolutions; private Button btnOptions; private OptionsFrame optionsFrame; /**** * some options for the solving...these can be changed by the used * using the options button and the OptionsFrame. */ private double bisectionLow = -1000; private double bisectionHigh = 1000; private double newtonStart = 1; private boolean repeatBisection = true; private boolean repeatNewton = true; public EquationSolvingApplet() { } /**** * initializes the Applet by setting up the GUI */ public void init() { try { internalInit(); } catch (Exception e) { e.printStackTrace(); } } /**** * internally used helper method which constructs a new * GridBagLayoutConstraint setup with the parameters */ private static GridBagConstraints getGBC(int left, int top, int width, int height, boolean fill) { GridBagConstraints result = new GridBagConstraints(); result.gridx = left; result.gridy = top; result.gridwidth = width; result.gridheight = height; if (fill) result.fill = GridBagConstraints.BOTH; return result; } /***** * destroys the option frame if it is not null. */ public void destroy() { if (optionsFrame != null) { optionsFrame.setVisible(false); optionsFrame.dispose(); } } /**** * called from init to setup the GUI for the applet. */ private void internalInit() throws Exception { setBackground(new Color(getParameterAsInt("bgcolor", 0xFFFFFF, 16))); setLayout(new GridBagLayout()); txtEquation = new TextField(); btnSolve = new Button("Solve"); lblSolutions = new Label(); btnOptions = new Button("..."); GridBagConstraints gbc; gbc = getGBC(0,0,2,1,true); gbc.weightx = 0.9; add(txtEquation, gbc); gbc = getGBC(0,1,2,1,true); gbc.weightx = 0.9; add(lblSolutions, gbc); gbc = getGBC(2,0,1,2,true); gbc.weightx = 0.1; add(btnSolve, gbc); gbc = getGBC(3,0,1,2,true); gbc.weightx = 0.05; add(btnOptions, gbc); btnSolve.addActionListener(this); btnOptions.addActionListener(this); txtEquation.addActionListener(this); } public String getAppletInfo() { return "Equation Solving Applet, copyright 1998 by Martin Lovell, " + "ALL RIGHTS RESERVED"; } /**** * responds to button actions */ public void actionPerformed(ActionEvent e) { if (e.getSource() == txtEquation || e.getSource() == btnSolve) { solve(); } else if (e.getSource() == btnOptions) { showOptions(); } } /**** * called in response to Solve button. Solves the equation using the * current options of the Applet. If a solution is found, it is displayed, * otherwise the text "No Solution Found" is displayed. */ private void solve() { String equ = txtEquation.getText(); lblSolutions.setText(""); int i = equ.indexOf('='); if (i == -1) return; String expr1Str = equ.substring(0, i); String expr2Str = equ.substring(i + 1, equ.length()); ExpressionEvaluator expr1 = null; ExpressionEvaluator expr2 = null; try { expr1 = new ExpressionEvaluator(expr1Str); expr2 = new ExpressionEvaluator(expr2Str); } catch (ExpressionException e) { lblSolutions.setText(e.getMessage()); return; } if (repeatNewton) { try { double answer = EquationSolver.repeatNewton(expr1, expr2, newtonStart, 1.0E-7, 1.0E-4, 'X'); lblSolutions.setText("x = " + Double.toString(answer)); return; } catch (NoSolutionException err) {} } else { try { double answer = EquationSolver.newton(expr1, expr2, newtonStart, 1.0E-7, 1.0E-4, 'X'); lblSolutions.setText("x = " + Double.toString(answer)); return; } catch (NoSolutionException err) {} } if (repeatBisection) { try { double answer = EquationSolver.repeatBisection(expr1, expr2, bisectionLow, bisectionHigh, 1.0E-7, 'X'); lblSolutions.setText("x = " + Double.toString(answer)); return; } catch (NoSolutionException err) {} } else { try { double answer = EquationSolver.bisection(expr1, expr2, bisectionLow, bisectionHigh, 1.0E-7, 'X'); lblSolutions.setText("x = " + Double.toString(answer)); return; } catch (NoSolutionException err) {} } lblSolutions.setText("No solution found."); } /**** * called in response to the options button. Displays the options dialog * which allows the user to set options for the techniques used for solving * the equation. */ private void showOptions() { if (optionsFrame == null || !optionsFrame.isVisible()) { optionsFrame = new OptionsFrame(); optionsFrame.setBackground(getBackground()); } else { optionsFrame.show(); optionsFrame.toFront(); } } /***** * private OptionsFrame class used to set options for the Applet. */ private class OptionsFrame extends Frame implements ActionListener { Panel pnlBisection; Panel pnlNewton; Panel pnlButtons; Checkbox chkRepeatBisection; Checkbox chkRepeatNewton; TextField txtBisectionLow; TextField txtBisectionHigh; TextField txtNewtonStart; Button btnOk; Button btnCancel; OptionsFrame() { super("Solver Options"); init(); } private void init() { setBounds(100,100,200,300); pnlBisection = new BorderPanel(); pnlNewton = new BorderPanel(); pnlButtons = new BorderPanel(); btnOk = new Button("OK"); btnCancel = new Button("Cancel"); chkRepeatBisection = new Checkbox("Repeat", repeatBisection); chkRepeatNewton = new Checkbox("Repeat", repeatNewton); txtBisectionLow = new TextField(Double.toString(bisectionLow)); txtBisectionHigh = new TextField(Double.toString(bisectionHigh)); txtNewtonStart = new TextField(Double.toString(newtonStart)); GridBagConstraints gbc; pnlBisection.setLayout(new GridBagLayout()); gbc = EquationSolvingApplet.getGBC(0,0,2,1,true); gbc.weightx = 1; gbc.weighty = 0.25; pnlBisection.add(new Label("Bisection Options:"), gbc); gbc = EquationSolvingApplet.getGBC(0,1,1,1,true); gbc.weightx = 0.5; gbc.weighty = 0.25; pnlBisection.add(new Label("Low:"), gbc); gbc = EquationSolvingApplet.getGBC(1,1,1,1,true); gbc.weightx = 0.5; gbc.weighty = 0.25; pnlBisection.add(new Label("High:"), gbc); gbc = EquationSolvingApplet.getGBC(0,2,1,1,true); gbc.weightx = 0.5; gbc.weighty = 0.25; pnlBisection.add(txtBisectionLow, gbc); gbc = EquationSolvingApplet.getGBC(1,2,1,1,true); gbc.weightx = 0.5; gbc.weighty = 0.25; pnlBisection.add(txtBisectionHigh, gbc); gbc = EquationSolvingApplet.getGBC(0,3,2,1,true); gbc.weightx = 1; gbc.weighty = 0.25; pnlBisection.add(chkRepeatBisection, gbc); pnlNewton.setLayout(new GridBagLayout()); gbc = EquationSolvingApplet.getGBC(0,0,2,1,true); gbc.weightx = 1; gbc.weighty = 0.25; pnlNewton.add(new Label("Newton Options:"), gbc); gbc = EquationSolvingApplet.getGBC(0,1,1,1,true); gbc.weightx = 1; gbc.weighty = 0.25; pnlNewton.add(new Label("Start:"), gbc); gbc = EquationSolvingApplet.getGBC(0,2,1,1,true); gbc.weightx = 1; gbc.weighty = 0.25; pnlNewton.add(txtNewtonStart, gbc); gbc = EquationSolvingApplet.getGBC(0,3,1,1,true); gbc.weightx = 1; gbc.weighty = 0.25; pnlNewton.add(chkRepeatNewton, gbc); pnlButtons.add(btnOk); pnlButtons.add(btnCancel); setLayout(new GridBagLayout()); gbc = EquationSolvingApplet.getGBC(0,0,1,1,true); gbc.weightx = 1; gbc.weighty = 0.4; add(pnlBisection, gbc); gbc = EquationSolvingApplet.getGBC(0,1,1,1,true); gbc.weightx = 1; gbc.weighty = 0.4; add(pnlNewton, gbc); gbc = EquationSolvingApplet.getGBC(0,2,1,1,true); gbc.weightx = 1; gbc.weighty = 0.2; add(pnlButtons, gbc); setVisible(true); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { setVisible(false); dispose(); } }); btnOk.addActionListener(this); btnCancel.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == btnOk) { try {bisectionLow = new Double(txtBisectionLow.getText()).doubleValue();} catch (Exception e1) {} try {bisectionHigh = new Double(txtBisectionHigh.getText()).doubleValue();} catch (Exception e1) {} try {newtonStart = new Double(txtNewtonStart.getText()).doubleValue();} catch (Exception e1) {} repeatBisection = chkRepeatBisection.getState(); repeatNewton = chkRepeatNewton.getState(); setVisible(false); dispose(); } else if (e.getSource() == btnCancel) { setVisible(false); dispose(); } } } /**** * simple Panel class with a simple black border around the edges. */ private static class BorderPanel extends Panel { public Insets getInsets() { return new Insets(6, 6, 6, 6); } public void paint(Graphics g) { Rectangle r = getBounds(); r.grow(-3, -3); g.setColor(Color.black); g.drawRect(2, 2, r.width, r.height); } } /*** * returns the value of a parameter as a String. If the parameter * is not defined, def is returned. */ public String getParameter(String key, String def) { return (getParameter(key) != null ? getParameter(key) : def); } /*** * returns the value of a parameter as an int. If the parameter * is not defined, def is returned. The parameter is parsed to integer * using the default radix of 10 */ public int getParameterAsInt(String key, int def) { return getParameterAsInt(key, def, 10); } /*** * returns the value of a parameter as an int. If the parameter * is not defined, def is returned. The parameter is parsed to integer * using the provided radix */ public int getParameterAsInt(String key, int def, int radix) { try { return (getParameter(key) != null ? Integer.parseInt(getParameter(key), radix) : def); } catch (Exception e) { return def; } } }