/* * Title: SplineGraph * Copyright: Waterloo Maple Inc * @author Stephen Forrest * @version 1.0 */ package com.maplesoft.maplenet.demo; import java.applet.*; import java.util.*; import java.awt.*; import java.awt.event.*; import java.net.*; import com.maplesoft.client.mathmlEquation; import com.maplesoft.client.MapleStatement; import com.maplesoft.client.MapleException; public class SplineGraph extends Applet implements MouseListener { boolean isStandalone = false; String host; int port; String user; String password; int maxX = 350; int maxY = 350; HashSet pointset = new HashSet(); LinkedList plotPoints = null; mathmlEquation mathml = new mathmlEquation(); Button clearButton = new Button(); Button calcButton = new Button(); Point holdingPoint = null; //Get a parameter value public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /** * Initialize the applet. Resize and load images. */ public void init() { mapleInit(); try { jbInit(); myInit(); } catch(Exception e) { e.printStackTrace(); } // mathml.setEquation("&Space;"); mathml.setEquation(""); addMouseListener(this); } public void mapleInit() { 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", "client"); } catch(Exception e) { e.printStackTrace(); } try { password = this.getParameter("password", "demopass"); } catch (Exception e) { e.printStackTrace(); } } private void myInit() { //this.setSize( 450, 350 ); mathml.setControls( true ); mathml.registerControls( true ); mathml.allow_cut = true; mathml.setVAlign( "top" ); } private void jbInit() throws Exception { this.setLayout(null); calcButton.setBounds(new Rectangle(180, 350, 100, 30)); calcButton.setLabel("Interpolate"); calcButton.addActionListener(new SplineGraph_calcButton_actionAdapter(this)); clearButton.setBounds(new Rectangle(70, 350, 100, 30)); clearButton.setLabel("Clear Points"); clearButton.addActionListener(new SplineGraph_clearButton_actionAdapter(this)); this.setBackground(SystemColor.info); mathml.initBG(SystemColor.info); mathml.setBackground(SystemColor.info); mathml.setBounds(new Rectangle(320, 60, 250, 310)); mathml.setPrefix(""); mathml.setLinebreak( true ); mathml.setDrawBorder( true ); this.add(calcButton, null); this.add(clearButton, null); this.add(mathml, null); } public void paint(Graphics g) { String label = ""; g.drawString("Interpolating function:", 385, 30 ); g.drawLine( 50, maxY-50, maxX-50, maxY-50 ); // x axis g.drawString("x", (int)(maxY/2), maxY-20); g.drawLine( 50, maxY-50, 50, 50 ); // y axis g.drawString("y", 10, (int)(maxY/2)); g.drawString("0", 35, maxY-35 ); for (int x=1 ; x <= 10 ; x++) { g.drawLine( 50+(int)((maxX-100)*x/10), maxY-50, 50+(int)((maxX-100)*x/10), maxY-60 ); g.drawLine( 50, maxY-(50+(int)((maxX-100)*x/10)), 60, maxY-(50+(int)((maxY-100)*x/10)) ); label = x*10 + ""; g.drawString( label, -8 + 50+(int)((maxX-100)*x/10) , maxY-35 ); g.drawString( label, 30, maxY-(-5 + 50+(int)((maxY-100)*x/10)) ); } Iterator pointIterator = pointset.iterator(); Point myPoint = null; while (pointIterator.hasNext()) { myPoint = (Point)pointIterator.next(); drawVirtualPoint( g, myPoint ); } drawCurve(g); } private Point getVirtualPoint( Point p ) { int x = (int)p.getX(); int y = (int)p.getY(); return( new Point( (int)(100*(x-50)/(maxX-100)), (int)(100*(maxY-y-50)/(maxY-100)) ) ); } private Point getVirtualPoint( double x, double y ) { return( new Point( (int)(100*(x-50)/(maxX-100)), (int)(100*(maxY-y-50)/(maxY-100)) ) ); } private Point getPhysicalPoint( Point p ) { int x = (int)p.getX(); int y = (int)p.getY(); return( new Point( (int)(x*(maxX-100)/100)+50, maxY-50-(int)(y*(maxY-100)/100) ) ); } private Point getPhysicalPoint( double x, double y ) { return( new Point( (int)(x*(maxX-100)/100)+50, maxY-50-(int)(y*(maxY-100)/100) ) ); } private void parseNumericList ( String pointdata, LinkedList myList ) { String token; int pos = 1; int next_delim = 1; if ( pointdata.charAt(0) != '[' ) { return; // something wrong happened } while (pos < pointdata.length()) { next_delim = pointdata.indexOf(',', pos); if (next_delim == -1) { next_delim = pointdata.indexOf(']', pos); token = pointdata.substring( pos, next_delim ); myList.add( new Double( Double.parseDouble(token)) ); break; } token = pointdata.substring( pos, next_delim ); myList.add( new Double( Double.parseDouble(token)) ); pos = next_delim + 1; } } public String getAppletInfo() { return "Draws a sin graph."; } public void drawPoint(Graphics g, int x, int y) { g.setColor( Color.red ); g.drawOval( x-3, y-3 , 6, 6); } public void drawVirtualPoint(Graphics g, Point p) { Point physicalPoint = getPhysicalPoint(p); g.setColor( Color.red ); g.drawOval( physicalPoint.x-3, physicalPoint.y-3 , 6, 6); } private boolean isWithinBounds(Point p) { return (p.x>=50 && p.x<=maxX-50 && p.y>=50 && p.y<=maxY-50); } public void drawCurve(Graphics g) { int i = 0; Point prevPoint; g.setColor( Color.green ); if (plotPoints != null && !plotPoints.isEmpty()) { Point myPoint = (Point)(plotPoints.get(0)); for (i=1; i < plotPoints.size(); i++) { prevPoint = myPoint; myPoint = (Point)(plotPoints.get(i)); if (isWithinBounds(prevPoint) && isWithinBounds(myPoint)) { g.drawLine( prevPoint.x, prevPoint.y, myPoint.x, myPoint.y ); } } } } private Point getClosePoint(int x, int y) { Point vPoint = null; if (x >= 50 && x <= maxX-50 && y >= 50 && y <= maxY-50) { vPoint = getVirtualPoint(x,y); Iterator pointIterator = pointset.iterator(); Point myPoint = null; while (pointIterator.hasNext()) { myPoint = (Point)pointIterator.next(); if ( (myPoint.x-vPoint.x)*(myPoint.x-vPoint.x)+(myPoint.y-vPoint.y)*(myPoint.y-vPoint.y)<5) { return myPoint; } } } return null; } private void removePoint(Point p) { pointset.remove(p); } private void addPoint(int x, int y) { Point vPoint = null; if (x >= 50 && x <= maxX-50 && y >= 50 && y <= maxY-50) { vPoint = getVirtualPoint(x,y); Iterator pointIterator = pointset.iterator(); Point myPoint = null; while (pointIterator.hasNext()) { myPoint = (Point)pointIterator.next(); if (myPoint.x == vPoint.x) { return; } } // Having got to here, we know our x-value is distinct. pointset.add( vPoint ); drawVirtualPoint( getGraphics(), vPoint ); } } public void mouseReleased(MouseEvent e) { addPoint( e.getX(), e.getY() ); if (holdingPoint != null) { removePoint( holdingPoint ); holdingPoint = null; repaint(); } } public void mousePressed(MouseEvent e) { // grab hold of point for later moving holdingPoint = getClosePoint( e.getX(), e.getY() ); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } void clearButton_actionPerformed(ActionEvent e) { clear_pointset(); } void clear_pointset() { pointset.clear(); plotPoints = null; mathml.setEquation(""); repaint(); } void calcButton_actionPerformed(ActionEvent e) { do_something(); } void do_something() { String[] mapleIn = new String[4]; String[] mapleOut = new String[4]; String sortedlist = ""; String pointlist = "["; Iterator pointIterator = pointset.iterator(); Point myPoint; while (pointIterator.hasNext()) { myPoint = (Point)pointIterator.next(); pointlist = pointlist.concat("["+myPoint.x+","+myPoint.y+"]"); if (pointIterator.hasNext()) { pointlist = pointlist.concat(", "); } } pointlist = pointlist.concat("]"); System.out.println( pointlist ); sortedlist = "sort(" + pointlist + ",(x,y)->is(x[1]