package com.maplesoft.maplenet.tutorial; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.net.URL; import javax.swing.*; import com.maplesoft.client.mathmlEquation; import com.maplesoft.client.mathmlEditEquation; import com.maplesoft.client.MapleToMathML; public class MathTestApplet extends Applet { boolean isStandalone = false; String host; int port; String user; String password; TextField upperLimit = new TextField(); TextField lowerLimit = new TextField(); TextField equationString = new TextField(); TextField variable = new TextField(); Button calcButton = new Button(); mathmlEquation integralSign = new mathmlEquation("&Space;"); mathmlEquation mathml = new mathmlEquation(); JLabel derivativeSign = new JLabel(); //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } //Construct the applet public MathTestApplet() { } //Initialize the applet 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 { port = Integer.parseInt(this.getParameter("port", "14444")); } catch (Exception e) { e.printStackTrace(); } try { user = this.getParameter("user", "client"); } catch(Exception e) { e.printStackTrace(); } try { password = this.getParameter("password", "demopass"); } catch(Exception e) { e.printStackTrace(); } try { jbInit(); myInit(); } catch(Exception e) { e.printStackTrace(); } } private void myInit() { this.setSize( 400, 400 ); mathml.setControls( true ); mathml.registerControls( true ); mathml.allow_cut = true; lowerLimit.requestFocus(); } //Component initialization private void jbInit() throws Exception { upperLimit.setBackground(Color.white); upperLimit.setBounds(new Rectangle(78, 30, 25, 25)); this.setLayout(null); lowerLimit.setBackground(Color.white); lowerLimit.setBounds(new Rectangle(78, 74, 25, 25)); equationString.setBackground(Color.white); equationString.setBounds(new Rectangle(112, 51, 137, 25)); equationString.setFont(new java.awt.Font("Dialog", 0, 14)); equationString.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(KeyEvent e) { equationString_keyTyped(e); } }); variable.setBackground(Color.white); variable.setBounds(new Rectangle(281, 51, 25, 25)); variable.setFont(new java.awt.Font("Dialog", 0, 14)); variable.setText("x"); calcButton.setBounds(new Rectangle(316, 50, 46, 30)); calcButton.setLabel("="); calcButton.addActionListener(new MathTestApplet_calcButton_actionAdapter(this)); this.setBackground(SystemColor.info); this.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(KeyEvent e) { this_keyTyped(e); } }); this.addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(ComponentEvent e) { this_componentResized(e); } }); integralSign.setVAlign( "baseline" ); integralSign.initBG(SystemColor.info); integralSign.setBackground(SystemColor.info); integralSign.setBounds(new Rectangle(45, 30, 32, 94)); integralSign.setPointSize( 48 ); mathml.initBG(SystemColor.info); mathml.setBackground(SystemColor.info); mathml.setBounds(new Rectangle(20, 150, 350, 130)); mathml.setPrefix(""); mathml.setLocation( 20, 150 ); mathml.setSize( 350, 130 ); mathml.setLinebreak( true ); mathml.setDrawBorder( true ); derivativeSign.setHorizontalAlignment(SwingConstants.RIGHT); derivativeSign.setText("d"); derivativeSign.setBounds(new Rectangle(258, 55, 16, 16)); this.add(integralSign, null); this.add(lowerLimit, null); this.add(upperLimit, null); this.add(equationString, null); this.add(variable, null); this.add(calcButton, null); this.add(mathml, null); this.add(derivativeSign, null); } //Start the applet public void start() { } //Stop the applet public void stop() { } //Destroy the applet public void destroy() { } //Get Applet information public String getAppletInfo() { return "Applet Information"; } //Get parameter info public String[][] getParameterInfo() { String[][] pinfo = { {"host", "String", ""}, {"port", "int", ""}, {"user", "String", ""}, {"password", "String", ""}, }; return pinfo; } //Main method public static void main(String[] args) { MathTestApplet applet = new MathTestApplet(); applet.isStandalone = true; Frame frame; frame = new Frame(); frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(600,420); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } void do_something() { String sIntegrand; String sVariable; String sInput; String tInput; String sOutput; // ******************************************************************************** // The String sent to Maple is of the form // // "Int( expression) = int( expression )" // // so that the left represents the Raw Integral and the // right side is the evaluated integral // ******************************************************************************** sIntegrand = "("+equationString.getText()+")"; sVariable = variable.getText(); if( lowerLimit.getText() != null && upperLimit.getText() != null) { if( lowerLimit.getText().length() > 0 && upperLimit.getText().length()>0) sVariable = sVariable.concat( "="+lowerLimit.getText()+".."+upperLimit.getText() ); } sInput = "Int( " + sIntegrand + "," + sVariable + " )"; tInput = "int( " + sIntegrand + "," + sVariable + " )"; System.out.println("Try :"+host+":"+port+" user="+user); System.out.println("Input :"+sInput); MapleToMathML convertor = new MapleToMathML( host, port, user, password ); try { sOutput = convertor.execute( sInput + " = " + tInput , 2 ); System.out.println("Output :"+sOutput); mathml.setEquation( sOutput ); } catch( Exception ex ) { System.out.println("Convert error :" + ex.toString() ); mathml.setEquation( "???" ); } equationString.requestFocus(); equationString.selectAll(); repaint(); } void resize_the_applet() { int w = getWidth(); int h = getHeight(); int x; int y; int xw; int yh; System.out.println( "Resizing "+w+","+h); integralSign.setBounds( new Rectangle( 10, 30, 30, 90)); upperLimit.setBounds( new Rectangle( 50, 30, 25, 25)); lowerLimit.setBounds( new Rectangle( 50, 75, 25, 25)); y = 50; yh = 25; xw = 45; x = w - 15 - xw; calcButton.setBounds( new Rectangle( x, y, xw, yh)); xw = 25; x = x -10 - xw; variable.setBounds( new Rectangle( x, y, xw, yh) ); xw = 10; x = x - 10 - xw; derivativeSign.setBounds( new Rectangle( x, y, xw, yh)); xw = x - 85 - 5; if(xw < 0) xw = 10; x = 85; equationString.setBounds( new Rectangle( x, y, xw, yh)); mathml.setBounds( new Rectangle( 20, 150, w-40, h-150-10) ); } void calcButton_actionPerformed(ActionEvent e) { do_something(); } void this_componentResized(ComponentEvent e) { resize_the_applet(); } void this_keyTyped(KeyEvent e) { if( e.getKeyCode() == KeyEvent.VK_ENTER) { do_something(); } } void equationString_keyTyped(KeyEvent e) { if( e.getKeyCode() == KeyEvent.VK_ENTER) { do_something(); } } } class MathTestApplet_calcButton_actionAdapter implements java.awt.event.ActionListener { MathTestApplet adaptee; MathTestApplet_calcButton_actionAdapter(MathTestApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.calcButton_actionPerformed(e); } }