import java.applet.*; import java.util.*; import java.io.*; import java.awt.*; // import java.net.*; public class Test2 extends java.applet.Applet implements Runnable { Thread thread; WeightFrame w; Arrow shortRoot = Arrow2d(-2,0); Arrow longRoot = Arrow2d(2,-2); // Path path = leg2d(8,-16).thenDo(leg2d(-2,1)) // .thenDo(leg2d(6,3)).thenDo(leg2d(12,24)); // int SCALE = 50; Path path = leg2d(0,0); int SCALE = 20; Runtime myRuntime = Runtime.getRuntime(); int delayForPaint = 30; int currentVertex = 0; Path drawVertex = null, newLeft = null, newDiag = null; boolean done = false; boolean runFreely = false; Vector paths = new Vector(); Hashtable pathsTable = new Hashtable(); int numPaths = 0; Vector endPoints = new Vector(); Hashtable endPointsTable = new Hashtable(); int counts[] = new int[1000]; int numEndPoints = 0; int numPathRefs = 0; int numEndsRefs = 0; int stage = 0; Path firstPath = null; Path provPath = null; Arrow curPt = Arrow2d(0,0); Arrow mouseAt = Arrow2d(0,0); public Arrow Arrow2d(int x, int y) { Arrow ans = new Arrow(2); ans.setCoord(0, new Szam(x,1)); ans.setCoord(1, new Szam(y,1)); return ans; } public Path leg2d(int x, int y) { return new Path(Arrow2d(x,y)); } public void debug(int i, String s) { if (i>=4) System.out.println(s); } public void drawPath(Path which, Graphics g, int xShift, Color col) { int syze = Math.min(size().width,size().height)/2; int curx = size().width/2 + xShift; int cury = size().height/2; if (which != null) which.draw(g, curx, cury, syze/SCALE, col); } public void paint(Graphics g) { int syze = Math.min(size().width,size().height)/2; int curx = size().width/2; int cury = size().height/2; int newx,newy; Szam coord0 = new Szam(0,1); Szam coord1 = new Szam(0,1); if (stage == 0) { for (int x=0; x-1) { // debug(0,"This is already endpoint "+where); counts[where]++; } else { numEndsRefs++; // debug(0,"New endpoint"); endPointsTable.put(aToString,new Integer(numEndPoints)); endPoints.addElement(a); counts[numEndPoints++] = 1; } } boolean kickStart; public boolean findNewPath() { boolean ans = true; drawVertex = null; numPathRefs = 0; numEndsRefs = 0; if (currentVertex < numPaths) { Path p = (Path)paths.elementAt(currentVertex); // debug(1,""); // debug(1,"Processing path "+currentVertex+" = "+p+"..."); Path newPath = Path.newPath(p); newLeft = newDiag = null; if (newPath.Littelmann(shortRoot)) { newLeft = Path.newPath(newPath); notherElement(newPath); } newPath.copy(p); if (newPath.Littelmann(longRoot)) { newDiag = new Path(newPath); notherElement(newPath); } newPath.free(); // String spaces = ""; // long free = myRuntime.freeMemory(); // long tot = myRuntime.totalMemory(); // for (long j=free; j0) System.out.println("All paths found"); ans = false; } return ans; } public boolean mouseMove(Event evt, int x, int y) { int syze = Math.min(size().width,size().height)/2; int curx = size().width/2; int cury = size().height/2; mouseAt.setCoord(0, new Szam((x-curx)*SCALE*2/syze,2)); mouseAt.setCoord(1, new Szam((cury-y)*SCALE*2/syze,2)); Szam X = mouseAt.coord(0); //.over(Szam.TWO); Szam diff = (mouseAt.coord(1).minus(mouseAt.coord(0))).over(Szam.TWO); X.numerator = X.numerator/X.denominator; diff.numerator = diff.numerator/diff.denominator; X.denominator = 1; diff.denominator = 1; mouseAt.setCoord(0,X); //.plus(x); mouseAt.setCoord(1,diff.plus(diff).plus(X)); this.repaint(10); if (stage == 0) { curPt = Arrow2d(0,0); curPt.setCoord(0, new Szam((x-curx)*SCALE*2/syze,2)); curPt.setCoord(1, new Szam((cury-y)*SCALE*2/syze,2)); provPath = path.thenDo(new Path(curPt.minus(path.endPoint()))); curPt.free(); } return true; } public boolean mouseDown(Event evt, int X, int Y) { int syze = Math.min(size().width,size().height)/2; int curx = size().width/2; int cury = size().height/2; if (stage == 0) { path = new Path(provPath); debug(0,"Path so far: "+path+" (from "+provPath+")"); } else { Arrow endPt = mouseAt; if (endPointsTable.containsKey(endPt.toString()) ) { int whichEndPoint = ((Integer)endPointsTable. get(endPt.toString())).intValue(); if (w == null) { w = new WeightFrame(paths,pathsTable,numPaths,endPoints, endPointsTable,counts, whichEndPoint, SCALE); w.resize(400,400); w.show(); w.repaint(); } else { w.changeEndPoint(whichEndPoint); w.repaint(); } } } return true; } public boolean keyDown(Event evt, int key) { char ch = (char)key; if (stage == 0) { Arrow endPt = path.endPoint(); Szam x = endPt.coord(0); //.over(Szam.TWO); Szam diff = (endPt.coord(1).minus(endPt.coord(0))).over(Szam.TWO); if (x.numerator != 0) x.numerator = (x.denominator - (x.numerator % x.denominator)); if (diff.numerator != 0) diff.numerator = (diff.denominator - (diff.numerator % diff.denominator)); endPt.setCoord(0,x); //.plus(x); endPt.setCoord(1,diff.plus(diff).plus(x)); Arrow lastStep = (Arrow) path.step.elementAt(path.step.size()-1); path.step.setElementAt(lastStep.plus(endPt), path.step.size()-1); path.endPt.copy(path.endPoint()); debug(4,"High weight = "+path.endPt+", path = "+path); stage = 1; notherElement(path); debug(0,"Initializing with "+path); } if (ch == 'a') { delayForPaint += 5; System.out.println("Changing delay to "+delayForPaint); } if ((ch == 'A') && (delayForPaint > 0)) { delayForPaint -= 5; System.out.println("Changing delay to "+delayForPaint); } if (ch == 'g') { runFreely = kickStart = !runFreely; } if (ch == 'r') { stage = 0; provPath = path = leg2d(0,0); kickStart = runFreely = false; paths = new Vector(); pathsTable = new Hashtable(); numPaths = 0; currentVertex = 0; endPoints = new Vector(); endPointsTable = new Hashtable(); counts = new int[1000]; numEndPoints = 0; if (w != null) { w.dispose(); w = null; } this.repaint(10); } else { kickStart = true; debug(2,"Kickstarting"); } return true; } public void start() { thread = new Thread(this); debug(2,"In start"); thread.setPriority(Math.max(Thread.MIN_PRIORITY,thread.getPriority()-2)); thread.start(); debug(2,"Started new thread"); } public void run() { debug(2,"Running"); while (true) { if ((runFreely) || (kickStart)) { debug(2,"runFreely = "+runFreely+", kickStart = "+kickStart); kickStart = false; if (!findNewPath()) runFreely = false; // String a = ""; // for (int i=0; i