//Title: //Version: 2.0 //Copyright: Copyright (c) Bill Huiskamp //Author: Bill Huiskamp //Company: Waterloo Maple Inc //Description: 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.maplenet.client.MapleImage2DPlot; import com.maplesoft.maplenet.client.ImagePanel; public class MaplePlotApplet extends JApplet { boolean isStandalone = false; String sHost; int iPort; String sUser; String sPassword; String sFunction; String xName; String xLower; String xUpper; String yName; String yLower; String yUpper; int iNumPoints; JTextField jFunction = new JTextField(); JLabel jLabel5 = new JLabel(); JLabel jFromX = new JLabel(); JTextField jXName = new JTextField(); JLabel jLabel7 = new JLabel(); JTextField jXLower = new JTextField(); JLabel jLabel8 = new JLabel(); JTextField jXUpper = new JTextField(); JTextField jYUpper = new JTextField(); JTextField jYLower = new JTextField(); JTextField jYName = new JTextField(); JLabel jLabel9 = new JLabel(); JLabel jLabel10 = new JLabel(); JLabel jFromY = new JLabel(); ImagePanel jPlotPanel = new ImagePanel(); JButton jPlotButton = new JButton(); JLabel jLabel6 = new JLabel(); JTextField jOption1 = new JTextField(); JLabel jLabel11 = new JLabel(); JTextField jValue1 = new JTextField(); JTextField jOption2 = new JTextField(); JTextField jValue2 = new JTextField(); //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 MaplePlotApplet() { } //Initialize the applet public void init() { try { if( isStandalone ) { sHost = this.getParameter("host", "localhost" ); } else { URL url = getDocumentBase(); String sDocHost = url.getHost(); sHost = this.getParameter("host", sDocHost ); System.out.println("DocHost " + sDocHost); } } catch(Exception e) { e.printStackTrace(); } try { iPort = Integer.parseInt(this.getParameter("Port", "14444")); } catch(Exception e) { e.printStackTrace(); } try { sUser = this.getParameter("Username", "client"); } catch(Exception e) { e.printStackTrace(); } try { sPassword = this.getParameter("Password", "demopass"); } catch(Exception e) { e.printStackTrace(); } try { sFunction = this.getParameter("Function", "sin(x)"); } catch(Exception e) { e.printStackTrace(); } try { xName = this.getParameter("xName", "x"); } catch(Exception e) { e.printStackTrace(); } try { xLower = this.getParameter("xLower", "-Pi"); } catch(Exception e) { e.printStackTrace(); } try { xUpper = this.getParameter("xUpper", "Pi"); } catch(Exception e) { e.printStackTrace(); } try { yName = this.getParameter("yName", "y"); } catch(Exception e) { e.printStackTrace(); } try { yLower = this.getParameter("yLower", "-1"); } catch(Exception e) { e.printStackTrace(); } try { yUpper = this.getParameter("yUpper", "1"); } catch(Exception e) { e.printStackTrace(); } try { iNumPoints = Integer.parseInt(this.getParameter("NumPoints", "50")); } catch(Exception e) { e.printStackTrace(); } try { jbInit(); init_from_parameters(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { this.getContentPane().setBackground(SystemColor.info); this.setSize(new Dimension(700, 580)); this.getContentPane().setLayout(null); jLabel5.setText("Function"); jLabel5.setBounds(new Rectangle(14, 29, 100, 21)); jFunction.setNextFocusableComponent(jXName); jFunction.setBounds(new Rectangle(124, 29, 560, 21)); jFromX.setText("Domain"); jFromX.setBounds(new Rectangle(14, 59, 65, 21)); jXName.setAlignmentX((float) 1.0); jXName.setNextFocusableComponent(jXLower); jXName.setText("x"); jXName.setHorizontalAlignment(SwingConstants.CENTER); jXName.setBounds(new Rectangle(79, 59, 35, 21)); jLabel7.setFont(new java.awt.Font("Dialog", 1, 14)); jLabel7.setHorizontalAlignment(SwingConstants.CENTER); jLabel7.setText("="); jLabel7.setBounds(new Rectangle(124, 59, 20, 21)); jXLower.setNextFocusableComponent(jXUpper); jXLower.setBounds(new Rectangle(149, 59, 225, 21)); jLabel8.setHorizontalAlignment(SwingConstants.CENTER); jLabel8.setText("to"); jLabel8.setBounds(new Rectangle(389, 59, 55, 21)); jXUpper.setNextFocusableComponent(jYName); jXUpper.setBounds(new Rectangle(459, 59, 225, 21)); jFromY.setText("Range"); jFromY.setBounds(new Rectangle(14, 89, 65, 21)); jYName.setBounds(new Rectangle(79, 89, 35, 21)); jYName.setHorizontalAlignment(SwingConstants.CENTER); jYName.setText("y"); jYName.setAlignmentX((float) 1.0); jYName.setNextFocusableComponent(jYLower); jLabel10.setBounds(new Rectangle(124, 89, 20, 21)); jLabel10.setText("="); jLabel10.setHorizontalAlignment(SwingConstants.CENTER); jLabel10.setFont(new java.awt.Font("Dialog", 1, 14)); jYLower.setNextFocusableComponent(jYUpper); jYLower.setBounds(new Rectangle(149, 89, 225, 21)); jLabel9.setBounds(new Rectangle(389, 89, 55, 21)); jLabel9.setText("to"); jLabel9.setHorizontalAlignment(SwingConstants.CENTER); jYUpper.setNextFocusableComponent(jOption1); jYUpper.setBounds(new Rectangle(459, 89, 225, 21)); jLabel6.setText("Maple Option"); jLabel6.setBounds(new Rectangle(15, 128, 125, 21)); jOption1.setNextFocusableComponent(jValue1); jOption1.setBounds(new Rectangle(150, 128, 225, 21)); jLabel11.setText("Value"); jLabel11.setBounds(new Rectangle(390, 128, 55, 21)); jValue1.setNextFocusableComponent(jOption2); jValue1.setBounds(new Rectangle(460, 128, 225, 21)); jOption2.setNextFocusableComponent(jValue2); jOption2.setBounds(new Rectangle(150, 158, 225, 21)); jValue2.setNextFocusableComponent(jPlotButton); jValue2.setBounds(new Rectangle(460, 158, 225, 21)); jPlotPanel.setBackground(Color.white); jPlotPanel.setLayout(null); jPlotPanel.setBounds(new Rectangle(16, 191, 665, 340)); jPlotPanel.setSize( 665, 320); jPlotButton.setNextFocusableComponent(jFunction); jPlotButton.setToolTipText("Click to generate a plot of the function"); jPlotButton.setText("Plot"); jPlotButton.setBounds(new Rectangle(275, 538, 150, 25)); jPlotButton.setDefaultCapable(true); jPlotButton.addActionListener(new MaplePlotApplet_jPlotButton_actionAdapter(this)); this.getContentPane().add(jFunction, null); this.getContentPane().add(jLabel5, null); this.getContentPane().add(jFromX, null); this.getContentPane().add(jXName, null); this.getContentPane().add(jLabel7, null); this.getContentPane().add(jXLower, null); this.getContentPane().add(jLabel8, null); this.getContentPane().add(jXUpper, null); this.getContentPane().add(jFromY, null); this.getContentPane().add(jYName, null); this.getContentPane().add(jLabel10, null); this.getContentPane().add(jYLower, null); this.getContentPane().add(jLabel9, null); this.getContentPane().add(jYUpper, null); this.getContentPane().add(jOption1, null); this.getContentPane().add(jLabel6, null); this.getContentPane().add(jLabel11, null); this.getContentPane().add(jValue1, null); this.getContentPane().add(jOption2, null); this.getContentPane().add(jValue2, null); this.getContentPane().add(jPlotPanel, null); this.getContentPane().add(jPlotButton, null); this.getRootPane().setDefaultButton( jPlotButton ); } //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", "Name of server"}, {"Port", "int", "TCPIP Port"}, {"Username", "String", "Name of user on server"}, {"Password", "String", "Password"}, {"Function", "String", ""}, {"xName", "String", "Name of X coord"}, {"xLower", "String", ""}, {"xUpper", "String", ""}, {"yName", "String", ""}, {"yLower", "String", ""}, {"yUpper", "String", ""}, {"NumPoints", "int", ""}, }; return pinfo; } //Main method - invoke when running standlaone public static void main(String[] args) { // Create the Plotting Applet MaplePlotApplet applet = new MaplePlotApplet(); applet.isStandalone = true; // Standalone requires its own frame JFrame frame = new JFrame(); frame.setDefaultCloseOperation(3); //EXIT_ON_CLOSE == 3 frame.setTitle("Maple Plotting Test"); frame.setSize( 720, 620 ); frame.getContentPane().add(applet, BorderLayout.CENTER); // Init the applet applet.init(); applet.start(); frame.setSize( applet.getWidth()+10, applet.getHeight()+40 ); // Center Frame Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } //static initializer for setting look & feel static { try { //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); } catch(Exception e) { } } /** * Move data from variables into the form */ private void init_from_parameters() { jFunction.setText( sFunction ); jXName.setText( xName ); jXLower.setText( xLower ); jXUpper.setText( xUpper ); jFromY.setText( yName ); jYLower.setText( yLower ); jYUpper.setText( yUpper ); jOption1.setText( "TITLE" ); jValue1.setText( sFunction ); } private void sync_host_and_user() { } /** * Data from the various fields are collected and sent to the MapleImage2DPlot */ private void do_something() { // Blank the current image jPlotPanel.setImage( null ); jPlotPanel.repaint(0); // Create an empty plot request sync_host_and_user(); MapleImage2DPlot mip = new MapleImage2DPlot( sHost, iPort, sUser, sPassword ); // Fill in the plot information mip.setType( MapleImage2DPlot.GIF ); mip.setOptions( "NUMPOINTS" , iNumPoints ); // Set the resultant plot width to match the ImagePanel component mip.setDimensions( jPlotPanel.getWidth(), jPlotPanel.getHeight() ); // Fill in function, domain (x-axis) and range (y-axis) mip.setFunction( jFunction.getText() ); mip.setDomain( jXName.getText(), jXLower.getText(), jXUpper.getText() ); mip.setRange( jYName.getText(), jYLower.getText(), jYUpper.getText() ); // In case no title option is set - use the function as the title mip.setOptions( "TITLE", jFunction.getText() ); // Copy any options over also if(jOption1.getText().length()>0 && jValue1.getText().length()>0) { mip.setOptions( jOption1.getText(), jValue1.getText() ); } if(jOption2.getText().length()>0 && jValue2.getText().length()>0) { mip.setOptions( jOption2.getText(), jValue2.getText() ); } try { Image img = mip.getPlot(); if(img != null ) jPlotPanel.setImage( img ); } catch( Exception ex ) { System.out.println("Plot generation exception: " + ex.toString()); } jFunction.requestFocus(); jFunction.selectAll(); } void jPlotButton_actionPerformed(ActionEvent e) { jPlotButton.setEnabled( false ); do_something(); jPlotButton.setEnabled( true ); } } class MaplePlotApplet_jPlotButton_actionAdapter implements java.awt.event.ActionListener { MaplePlotApplet adaptee; MaplePlotApplet_jPlotButton_actionAdapter(MaplePlotApplet adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jPlotButton_actionPerformed(e); } }