package com.maplesoft.maplenet.demo;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.URL;
import javax.swing.*;
import java.util.Vector;
import com.maplesoft.maplenet.client.*;
public class SolveEquations extends Applet {
// local variables
boolean isStandalone = false;
String host;
int port;
String user;
String password;
JLabel title = new JLabel("Please Enter the Equation(s) or Inequation(s) below",JLabel.CENTER);
JPanel eqnPanel = new JPanel();
JPanel varPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel buttonPanel = new JPanel(new FlowLayout());
JPanel solPanel = new JPanel();
JLabel varLabel = new JLabel("variables: ",JLabel.LEFT);
Vector tfs = new Vector(3);
JTextField varTF = new JTextField(20);
JButton solve = new JButton("solve");
JButton evalf = new JButton("num.solve");
JButton add = new JButton("add");
JButton remove = new JButton("remove");
JButton clear = new JButton("clear");
String sTemp = "";
mathmlEquation sol = new mathmlEquation( sTemp );
public SolveEquations() {} // constructor
public void init() {
try {
URL url=getDocumentBase();
String sDocHost=url.getHost();
host=this.getParameter("host",sDocHost);
} catch (Exception e){e.printStackTrace();}
try {
port = Integer.parseInt(this.getParameter("port","14444"));
} catch(Exception e){e.printStackTrace();}
try {
user=this.getParameter("user","test");
} catch(Exception e){e.printStackTrace();}
try {
password=this.getParameter("password","test");
} catch(Exception e){e.printStackTrace();}
try {jbInit();} catch(Exception e) {e.printStackTrace();}
} // end init()
public void start() {} // end start()
public void stop() {} // end stop()
public void destroy() {} // end destroy()
private void jbInit() throws Exception {
iniTfs();
setEqnPanel(); setVarPanel(); setButtonPanel(); setSolPanel();
eqnPanel.setBackground(SystemColor.info);
varPanel.setBackground(SystemColor.info);
buttonPanel.setBackground(SystemColor.info);
solPanel.setBackground(SystemColor.info);
solPanel.setMinimumSize(new Dimension(376,200));
solPanel.setPreferredSize(new Dimension(376,200));
this.setSize(400,500);
this.setLayout(new GridBagLayout());
this.setBackground(SystemColor.info);
GridBagAdder.add(this, title, 0, 0, 1, 1, 1, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
GridBagAdder.add(this, eqnPanel, 0, 1, 1, 1, 1, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
GridBagAdder.add(this, varPanel, 0, 2, 1, 1, 1, 0, GridBagConstraints.VERTICAL, GridBagConstraints.CENTER);
GridBagAdder.add(this, buttonPanel, 0, 3, 1, 1, 1, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
GridBagAdder.add(this, solPanel, 0, 4, 1, 1, 1, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
} // end ibInit
private void iniTfs() {
for (int i=0; i\": ";
mp[1] += "for i from 1 to (m) do ";
mp[1] += "n := nops(solutions[i]): ";
mp[1] += "for j from 1 to (n) do ";
mp[1] += "oneSol:=substring(ExportPresentation(solutions[i][j]),50..-8): ";
mp[1] += "solString := cat(solString, \"\", oneSol, \"\"): ";
mp[1] += "end do: ";
mp[1] += "end do: ";
mp[1] += "if(m=0) then solString := cat(solString, \"null\" ): end if:";
mp[1] += "solString := cat(solString,\"\"): ";
mp[1] += "end use: solString: end proc: ";
mp[2] = "exportSol(sol);";
return mp;
} // end withProc()
private boolean isValidEqn(String eqn) {
if (eqn.trim().length()>0) {return true;} return false;
} // end isValidEqn()
private void solveEqns() {
mapleExecute(getSolveStatement());
} // end solveEqns()
private void evalfEqns() {
mapleExecute(getEvalfStatement());
} // end evalfEqns()
private void mapleExecute(String[] mapleIn) {
System.out.println("connect: "+host+":"+port);
System.out.println("username: "+user);
//System.out.println("password: "+password);
MapleStatement maple = new MapleStatement(host,port,user,password);
System.out.println("Maple Engine Input: "+mapleIn[0]+" "+mapleIn[2]);
Dimension d = solPanel.getSize();
try {
String[] mapleOut = maple.execute(mapleIn);
System.out.println("Maple Engine Output: "+mapleOut[0]+" "+mapleOut[2]);
sol.setBounds(new Rectangle(0,0, d.width+5, d.height+5 ));
sol.setSize( d.width+5, d.height+5 );
sol.setEquation( "" );
sol.setBounds(new Rectangle(0,0, d.width, d.height ));
sol.setSize( d.width, d.height );
sol.setEquation(mapleOut[2]);
} catch (Exception ex) {System.out.println("maple computation error"+ex.toString());
sol.setBounds(new Rectangle(0,0, d.width, d.height ));
sol.setSize( d.width, d.height );
sol.setEquation( "");
} //
// sol.repaint();
} // end mapleExecute()
private String ignoreProc(String multiLines) {
if (multiLines!=null) {
String endproc = "end proc";
String mathml;
int m = multiLines.indexOf(endproc);
mathml = multiLines.substring(m+endproc.length());
mathml = mathml.trim();
if (mathml.startsWith("\"\"")) {
return mathml.substring(mathml.indexOf("\"")+1,mathml.lastIndexOf("\""));
} else {return "";}
} // end if
else {return "";}
} // end ignoreProc()
public String getAppletInfo() {
return "Applet Information: solving a system of equation(s) or inequation(s)";
} // end getAppletInfo()
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} // end getParameter()
public String[][] getParameterInfo() {
return null;
} // end getParameterInfo()
public void addEqn() {
if (tfs.size()<10) {
if (tfs.add(new JTextField(34))) {this.setEqnPanel();}
this.validate();
} // end if (max 10 equations could be input)
} // end addEqn()
public void removeEqn() {
for (int i=tfs.size()-1; i>=0; i--) {
if (!isValidEqn(((JTextField)tfs.get(i)).getText())&&tfs.size()>1) {
tfs.remove(i); this.setEqnPanel(); this.validate(); return;
} // end if
} // end for
if (tfs.size()>1) {tfs.remove(tfs.size()-1); this.setEqnPanel(); this.validate();}
} // removeEqn()
public static void main(String[] args) {
SolveEquations applet = new SolveEquations();
applet.isStandalone = true;
Frame frame;
frame = new Frame();
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(600,500);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
} // end main
static class GridBagAdder {
static GridBagConstraints cons = new GridBagConstraints();
public static void add(Container cont, Component comp, int x, int y,
int width, int height, int weightx, int weighty, int fill, int anchor) {
cons.gridx=x; cons.gridy=y;
cons.gridwidth=width; cons.gridheight=height;
cons.weightx=weightx; cons.weighty=weighty;
cons.fill=fill; cons.anchor=anchor;
cont.add(comp,cons);
} // end add()
} // end static inner class GridBagAdder
public class SolveButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
solveEqns();
} // end actionPerformed
} // end SolveButtonListener()
public class EvalfButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
evalfEqns();
} // end actionPerformed()
} // end SolveButtonListener
public class AddButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
addEqn();
} // end actionPerformed()
} // end AddButtonListener
public class RemoveButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
removeEqn();
} // end actionPerformed()
} // end RemoveButtonListener
public class ClearButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
for (int i=0; i