/////////////////////////////////////////////////////////////////////// // // SpringCanvas: // /////////////////////////////////////////////////////////////////////// // // Copyright (c) 1998-00 by the IBM Corporation. All Rights Reserved. // // Author: Barry M. Trager bmt@watson.ibm.com // Angel L. Diaz aldiaz@us.ibm.com // // IBM MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IBM SHALL NOT BE LIABLE FOR // ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING OR MODIFYING // THIS SOFTWARE OR ITS DERIVATIVES. // /////////////////////////////////////////////////////////////////////// import java.awt.*; import ibm.techexplorer.dom.*; import org.w3c.dom.*; import ibm.techexplorer.techexplorer; import ibm.techexplorer.control.techexplorerControl; import ibm.techexplorer.axtchexp.AxTchExpRaw; public class SpringCanvas extends BufferedCanvas implements Runnable { private TEDocument document = null; private Thread running = null; private boolean suspend; private boolean debug = false; private techexplorer techexplorer = null; private SpringMass springs = null; public void start(SpringMass springs) { if (debug) System.out.println( "SpringCanvas: start" ); suspend = false; if (running==null) { running = new Thread(this); running.start(); } this.springs = springs; } public void stop() { if (debug) System.out.println( "SpringCanvas: stop" ); suspend = true; if (running != null && running.isAlive()) running.interrupt(); running = null; document = null; } void suspend() { suspend = true; } void resume () { suspend = false; } public void run() { double tDelta = .1; while (true) { if (!suspend) { currentVals = rungeKutta(currentVals, currentTime, tDelta); currentTime += tDelta; repaint(); } try {Thread.sleep(50);} catch (InterruptedException e) {} } } public void setPlugin( techexplorer obj ) { if ( obj instanceof techexplorer ) { techexplorer = obj; getDocument(); } } public void setControl( AxTchExpRaw obj ) { if ( obj instanceof AxTchExpRaw ) { techexplorer = new techexplorerControl( obj ); getDocument(); } } public void getDocument() { document = (TEDocument) techexplorer.getDocumentNode(); } double[] initVals; double[] currentVals; String[] initNames; double currentTime = 0; void setInits(int initValsIndex) { initVals = initVec(initValsIndex); currentVals = initVals; initNames = initString(); } SpringCanvas() { setInits(0); // 1 for alternate initial conditions } double[] evalFun(double t, double[] y) { return new double[] {y[1], 2*y[2]-5*y[0], y[3], 2*y[0]-2*y[2]}; } double[] initVec(int initValsIndex) { switch(initValsIndex) { case 0: return new double[] {1, 0, 2, 0}; case 1: return new double[] {-2, 0, 1, 0}; default: return new double[] {1, 0, 2, 0}; } } String[] initString() { return new String[] {"u1 = ", "u1' = ", "u2 = ", "u2' = "}; } /* * updates second arg */ double[] vecAddTo(double[] v1, double[] v2) { for (int i=0; i 3*Ymax || y[0] < 3*Ymin) return; } } int selectedBlock = 0; public boolean mouseDown(Event evt, int x, int y) { int mouse_x = x; int mouse_y = y; int m1offset = (int)(20*currentVals[0]); int m2offset = (int)(20*currentVals[2]); int xmid = size().width/2; int mleft = xmid - mwidth/2; // left border block.setLocation(mleft, slength + m1offset); if (block.contains(mouse_x, mouse_y)) { selectedBlock = 1; //initList.select(0); repaint(); return true; } block.translate(0, mheight+slength+m2offset); if (block.contains(mouse_x, mouse_y)) { selectedBlock = 2; //initList.select(1); repaint(); return true; } selectedBlock = 0; suspend = !suspend; repaint(); return true; } public boolean mouseUp(Event evt, int x, int y) { selectedBlock = 0; repaint(); return true; } public boolean mouseEnter(Event evt, int x, int y) { requestFocus(); return true; } String formatInit(double loc) { if (loc < 0) return "-" + formatInit(-loc); int iloc = (int)Math.round(loc*10); String result = iloc % 10 + ""; iloc = iloc / 10; return iloc + "." + result; } public boolean mouseDrag(Event evt, int x, int y) { if (selectedBlock == 1) { currentVals[0] = (y - slength - mheight/2)/20.0; initVals[0] = currentVals[0]; /* * Update the initial conditions via the Document Object Model */ Node centerLine = findFirstNodeFromName( (Node) document, "centerline" ); Node inlineMath = findFirstNodeFromName( (Node) centerLine.getNextSibling().getNextSibling(), "inline-math" ); NodeList nodeList = inlineMath.getChildNodes(); Text text = (Text) nodeList.item(2); if (debug) System.out.println( text.getNodeName() + " value (as text):" + text.getNodeValue() ); text.setData( formatInit(initVals[0]) ); nodeList = inlineMath.getNextSibling().getNextSibling().getChildNodes(); text = (Text) nodeList.item(2); if (debug) System.out.println( text.getNodeName() + " value (as text):" + text.getNodeValue() ); text.setData( formatInit(initVals[2]) ); document.recomposeAndRedraw(); repaint(); } if (selectedBlock == 2) { currentVals[2] = (y - 2*slength - 3*mheight/2)/20.0 - currentVals[0]; initVals[2] = currentVals[2]; /* * Update the initial conditions via the Document Object Model */ Node centerLine = findFirstNodeFromName( (Node) document, "centerline" ); Node inlineMath = findFirstNodeFromName( (Node) centerLine.getNextSibling().getNextSibling(), "inline-math" ); NodeList nodeList = inlineMath.getChildNodes(); Text text = (Text) nodeList.item(2); if (debug) System.out.println( text.getNodeName() + " value (as text):" + text.getNodeValue() ); text.setData( formatInit(initVals[0]) ); nodeList = inlineMath.getNextSibling().getNextSibling().getChildNodes(); text = (Text) nodeList.item(2); if (debug) System.out.println( text.getNodeName() + " value (as text):" + text.getNodeValue() ); text.setData( formatInit(initVals[2]) ); document.recomposeAndRedraw(); repaint(); } return true; } private double initRange = 6; private double Xmax = initRange; private double Xmin = -initRange; private double Ymax = initRange; private double Ymin = -initRange; private Node findFirstNodeFromName( Node node, String name ) { Node inlineMathNode = null; if ( node != null ) { if (debug) System.out.println( "findFirstNodeFromName: current node name: " + node.getNodeName() ); if ( node.getNodeType() == Node.TEXT_NODE ) if (debug) System.out.println( "findFirstNodeFromName: current node value: " + node.getNodeValue() ); if ( node.getNodeName().compareTo( name ) != 0 ) { if ( node.hasChildNodes() ) { NodeList nodeList = node.getChildNodes(); if (debug) System.out.println( "findFirstNodeFromName: current node number of children: " + nodeList.getLength() ); for( int i=0; i