// ------------------------------------------------------------------ // the UserCanvas constructor UserCanvas( ForcesMaster f, int p, int m, int pM, String w, String an, String ac, String cS, ProblemCanvas v) { problemNo = p; maxTriesEachForce = m; problemMode = pM; cgiScript = cS; problemCanvas = v; forcesMaster = f; weightIn = w; angleIn = an; accelerationIn = ac; problemSelector = new ProblemSelector(problemNo, angleIn, weightIn, accelerationIn); problemCanvas.setProblemSelectorHandle(problemSelector); numForces = problemSelector.getNumForces(); matchForceComps = problemSelector.getMatchForceComps(); numEquations = problemSelector.getNumEquations(); units = problemSelector.getUnits(); scale = problemSelector.getScale(); // if (!weightIn.equals("none")) {scale = 18 * scale /(double)(Integer.parseInt(weightIn));} forceTolerance = (int) Math.round(scale * userTolerance); resultsSpacingStrings = problemSelector.getResultsSpacingStrings(); blinkerStrings = problemSelector.getBlinkerStrings(); messageStrings = problemSelector.getMessageStrings(); forceNames = problemSelector.getForceNames(); truAnsX = problemSelector.getTruAnsX(); truAnsY = problemSelector.getTruAnsY(); tailPosX = problemSelector.getTailPosX(); tailPosY = problemSelector.getTailPosY(); cosSin = problemSelector.getCosSin(); truHdsX = problemSelector.getTruHdsX(); truHdsY = problemSelector.getTruHdsY(); cost = cosSin[0]; sint = cosSin[1]; // initialize the force variables maxForceNumber = numForces - 1; mouseUpsString = ""; for (int n = 0; n <= numForces; n++) { mouseUpsString += "0"; } ifBrowserPaint = false; // initialize to the "see data" message ifDataMessage = true; forceNumber = -1; fInit = mUp = mDown = mDrag = false; }
// ------------------------------------------------------------------ // initialize the canvas contents or judge the user's mouse events and respond public void paint(Graphics g) { // if forceNumber < 0 and !mUp: do nothing until mUp // if forceNumber < 0 and mUp (click): start the forces if (forceNumber < 0 && mUp) { fInit = true; mDown = false; // next line added 4/17/08 (correction for case of zero force true answer) mUp = false; forceNumber = 0; forceName = forceNames[forceNumber]; userPosX = tailPosX; userPosY = tailPosY; numTries = 0; messageCanvas.notifyMouseClick(blinkerStrings[forceNumber]); // create the force equation and vector objects canvasEquation = new CanvasEquation(g); canvasVector = new CanvasVector(g); } // if mouseDown and all forces finished: do end game if (mDown && (forceNumber == maxForceNumber + 1)) { endGame("OK", cgiScript, problemNo, mouseUpsString); } // if mouseDown or mouseDrag and really a force: // erase user's old vector & eqn, update position if ((mDown || mDrag) && (forceNumber >= 0) && (forceNumber <= maxForceNumber)) { messageCanvas.notifyMouseDown(blinkerStrings[forceNumber]); eraseUserStuff(g, forceX[0], forceY[0], 0); if (numEquations == 2) { eraseUserStuff(g, forceX[1], forceY[1], 1); } userPosX = newUserPosX; userPosY = newUserPosY; } // if browserPaint or forceInit or mouseDown or mouseDrag and really a force: // plot fixed & user's new stuff if ((ifBrowserPaint || fInit || mDown || mDrag) && (forceNumber >= 0) && (forceNumber <= maxForceNumber)) { // redraw the userCanvas bounding box and the apparatus g.setColor(Color.black); g.drawRect(0, 0, userFrameX, userFrameY); problemSelector.drawUserApparatus(g); // draw the user's previously-done correct vectors if (forceNumber > 0) { drawDoneVectors(forceNumber - 1, g); } // draw the user's new vector and equation canvasVector.plotVector(tailPosX, tailPosY, userPosX, userPosY, g); forceX[0] = p2fX(userPosX, userPosY); forceY[0] = p2fY(userPosX, userPosY); canvasEquation.plotEquation(g, forceName, units, forceX[0], forceY[0], 0); if (numEquations == 2) { forceX[1] = (int) Math.round(cost * forceX[0] - sint * forceY[0]); forceY[1] = (int) Math.round(sint * forceX[0] + cost * forceY[0]); canvasEquation.plotEquation(g, forceName, units, forceX[1], forceY[1], 1); } // get ready up for new input fInit = mDown = mDrag = false; if (ifBrowserPaint) { ifBrowserPaint = false; } } // if mouseUp, evaluate the user's answer if (mUp && (forceNumber >= 0) && (forceNumber < numForces)) { // keep track of the mouse ups String leftStr = ""; String rightStr = ""; String midStr = mouseUpsString.substring(forceNumber, forceNumber + 1); if (forceNumber > 0) { leftStr = mouseUpsString.substring(0, forceNumber); } if (forceNumber < numForces - 1) { rightStr = mouseUpsString.substring(forceNumber + 1, numForces); } int numTries = Integer.parseInt(midStr); numTries++; if (numTries <= 9) { midStr = Integer.toString(numTries); } else { midStr = "9"; } mouseUpsString = leftStr + midStr + rightStr; // if correct, cycle to the next force or to the trailer message int matchIndex = matchForceComps[forceNumber] - 1; int depX = Math.abs(forceX[matchIndex] - truAnsX[forceNumber][matchIndex]); int depY = Math.abs(forceY[matchIndex] - truAnsY[forceNumber][matchIndex]); if ((depX <= forceTolerance) && (depY <= forceTolerance)) { // erase the user's stuff for (int n = 0; n <= numEquations - 1; n++) { eraseUserStuff(g, forceX[n], forceY[n], n); } // draw the apparatus problemSelector.drawUserApparatus(g); // draw the set of correctly done vectors drawDoneVectors(forceNumber, g); // notify the resultsCanvas of the user's correctly drawn force boolean ifResultsDone = false; if (forceNumber == maxForceNumber) { ifResultsDone = true; } resultsCanvas.notifyMouseUpOk( ifResultsDone, forceNumber, forceNames, units, truAnsX, truAnsY, resultsSpacingStrings, numEquations, numForces); // cycle the force or do the end game if (forceNumber < maxForceNumber) { forceNumber++; numTries = 0; forceName = forceNames[forceNumber]; userPosX = tailPosX; userPosY = tailPosY; forceX[0] = forceY[0] = forceX[1] = forceY[1] = 0; // draw the equation and message for the *next* force for (int n = 0; n <= numEquations - 1; n++) { canvasEquation.plotEquation(g, forceName, units, forceX[n], forceY[n], n); } // notify the messageCanvas that there is a new force messageCanvas.notifyMouseUpOk(blinkerStrings[forceNumber]); } else { g.setColor(userTrailerColor); g.drawString(userTrailer, userTrailerPosX, userTrailerPosY); // notify the messageCanvas that we are all done messageCanvas.notifyMouseUpOk("done"); forceNumber++; } } // if not correct and mode > 1, check # of tries else { if (problemMode > 1 && numTries >= maxTriesEachForce) { endGame("notOK", cgiScript, problemNo, mouseUpsString); } } fInit = mUp = mDown = false; } }