public void Message(String s1, String s2, int i) { Graphics g = getGraphics(); if (s1 == null || s2 == null) { g.setColor(bgcolor); g.drawRect(400, 50 * i - 10, 200, 20); g.fillRect(400, 50 * i - 10, 200, 20); return; } /* g.setColor(Color.blue); g.drawRect(400,50 * i - 10,200,20); g.fillRect(400,50 * i - 10,200,20); */ g.setColor(txtcolor); g.drawString(s1, 400, 50 * i); g.drawString(s2, 400, 50 * i + 10); }
/** * This method is to be called from the <code>paint(Graphics g)</code> method of the drawing * panel/canvas. It uses the graphical context of the drawing panel/canvas to construct the * commentary box on the drawing panel/canvas. * * @param g Graphical context from the <code>paint(Graphics g)</code> method of the drawing * panel/canvas. * @see Graphics */ public void draw(Graphics g) { if (str.length() < 1) return; g.setColor(bg); g.fillPolygon(xPts, yPts, nPts); g.setColor(Color.black); g.drawPolygon(xPts, yPts, nPts); g.setColor(fg); g.setFont(font); g.drawString(str, topLeft.x + 4, topLeft.y + height - 10); }
public void paint(Graphics g) { Message("Copyright Feb, 2000 Weicon Conan Yuan.", "All Rights Reserved", 7); for (int i = 0; i < NUM_LOCATIONS; i++) { locs[i].draw(g, this); g.setColor(txtcolor); if (locs[i].ToDraw()) { g.drawString("" + i, locs[i].GetX(), locs[i].GetY() + 10); } } g.setColor(Color.red); g.drawRect(400, 200, 30, 30); g.fillRect(400, 200, 30, 30); Message("Cheat", 4); }
public void centerText(String s1, String s2, Graphics g, Color c, int x, int y, int w, int h) { // locs[0].centerText(s1, s2, this.getGraphics(), // Color.white, 400,0,200,50); // g.setXORMode(unselected_color); // centerText("pic" + im, null, g, Color.black, x, y, w, h); Font f = g.getFont(); FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f); int ascent = fm.getAscent(); int height = fm.getHeight(); int width1 = 0, width2 = 0, x0 = 0, x1 = 0, y0 = 0, y1 = 0; width1 = fm.stringWidth(s1); if (s2 != null) width2 = fm.stringWidth(s2); x0 = x + (w - width1) / 2; x0 = x + (w - width2) / 2; if (s2 == null) y0 = y + (h - height) / 2 + ascent; else { y0 = y + (h - (int) (height * 2.2)) / 2 + ascent; y1 = y0 + (int) (height * 1.2); } g.setColor(c); g.drawString(s1, x0, y0); if (s2 != null) g.drawString(s2, x1, y1); }
private void Display_Value(Graphics g, int[] ax, int[] ay) { int pos_x, pos_y; pos_x = 0; pos_y = 0; switch (testcase) { case 1: pos_x = ax[2]; pos_y = ay[2]; break; case 2: pos_x = ax[2]; pos_y = ay[2]; break; case 3: pos_x = ax[0]; pos_y = ay[0]; break; case 4: pos_x = ax[1]; pos_y = ay[1]; break; case 5: pos_x = ax[1]; pos_y = ay[1]; break; case 6: pos_x = ax[2]; pos_y = ay[2]; break; case 7: pos_x = ax[2]; pos_y = ay[2]; break; case 8: pos_x = ax[2]; pos_y = ay[2]; break; case 10: pos_x = ax[2]; pos_y = ay[2]; break; default: break; } g.drawString("" + value, pos_x, pos_y); }
/** * Parse the text then draw it without any rotation. * * @param g Graphics context * @param x pixel position of the text * @param y pixel position of the text */ public void draw(Graphics g, int x, int y) { TextState ts; int xoffset = x; int yoffset = y; if (g == null || text == null) return; Graphics lg = g.create(); parseText(g); if (justification == CENTER) { xoffset = x - width / 2; } else if (justification == RIGHT) { xoffset = x - width; } if (background != null) { lg.setColor(background); lg.fillRect(xoffset, yoffset - ascent, width, height); lg.setColor(g.getColor()); } if (font != null) lg.setFont(font); if (color != null) lg.setColor(color); for (int i = 0; i < list.size(); i++) { ts = ((TextState) (list.elementAt(i))); if (ts.f != null) lg.setFont(ts.f); if (ts.s != null) lg.drawString(ts.toString(), ts.x + xoffset, ts.y + yoffset); } lg.dispose(); lg = null; }
// ------------------------------------------------------------------ // 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; } }
/** The method to call when the thread starts */ public void run() { boolean draw = true; FontMetrics fm; Rectangle r; int sw = 0; int sa = 0; int x = 0; int y = 0; setPriority(Thread.MIN_PRIORITY); while (true) { if (newmessage != null && draw) { message = newmessage; newmessage = null; } if (lg == null) { lg = g2d.getGraphics(); if (lg != null) lg = lg.create(); } if (lg != null) { if (f != null) lg.setFont(f); fm = lg.getFontMetrics(lg.getFont()); sw = fm.stringWidth(message); sa = fm.getAscent(); } else { draw = false; } if (draw) { lg.setColor(foreground); r = g2d.bounds(); x = r.x + (r.width - sw) / 2; y = r.y + (r.height + sa) / 2; lg.drawString(message, x, y); g2d.repaint(); try { sleep(visible); } catch (Exception e) { } } else { if (lg != null) { lg.setColor(g2d.getBackground()); lg.drawString(message, x, y); g2d.repaint(); } try { sleep(invisible); } catch (Exception e) { } } draw = !draw; } }
public void paint(Graphics g) { path = findOptimizedPath(srcID, dstID, type); /// Create the drawing board Dimension d = getSize(); g.setColor(Color.white); g.fillRect(1, 1, d.width - 2, d.height - 2); g.setColor(Color.black); g.drawRect(1, 1, d.width - 2, d.height - 2); g.setFont(serifFont); /// Draw the whole network, including all routers with /// delay and flow level, sources and destinations. int numR = 1; int w = 95; int h = d.height / 5; int pos = -1; for (int i = 0; i < 3; i++) { g.drawOval(w, h + 100 * i, 40, 40); g.drawString("S" + String.valueOf(i + 1), w + 13, h + 100 * i - 5); } for (int i = 0; i < 3; i++) { pos++; Router temp = statMux.getRouter(pos); g.drawOval(w + 110, h + 100 * i, 40, 40); g.drawString("R" + String.valueOf(numR++), w + 123, h + 100 * i - 5); g.drawString( String.valueOf(temp.getDelay() * temp.getPriority(type) + temp.getFlowLevel()), w + 125, h + 100 * i + 15); g.drawString(String.valueOf(temp.getFlowLevel()), w + 125, h + 100 * i + 35); } h = d.height / 11; for (int i = 0; i < 4; i++) { pos++; Router temp = statMux.getRouter(pos); g.drawOval(w + 210, h + 100 * i, 40, 40); g.drawString("R" + String.valueOf(numR++), w + 223, h + 100 * i - 5); g.drawString( String.valueOf(temp.getDelay() * temp.getPriority(type) + temp.getFlowLevel()), w + 225, h + 100 * i + 15); g.drawString(String.valueOf(temp.getFlowLevel()), w + 225, h + 100 * i + 35); } h = 20; for (int i = 0; i < 6; i++) { pos++; Router temp = statMux.getRouter(pos); g.drawOval(w + 310, h + 80 * i, 40, 40); g.drawString("R" + String.valueOf(numR++), w + 320, h + 80 * i - 5); g.drawString( String.valueOf(temp.getDelay() * temp.getPriority(type) + temp.getFlowLevel()), w + 325, h + 80 * i + 15); g.drawString(String.valueOf(temp.getFlowLevel()), w + 325, h + 80 * i + 35); } for (int i = 0; i < 4; i++) { g.drawOval(w + 410, d.height / 11 + 100 * i, 40, 40); g.drawString("D" + String.valueOf(i + 1), w + 423, d.height / 11 + 100 * i - 5); } g.setColor(Color.black); int[][] connection = statMux.getConnections(); /// Check buffer for connections at each step and draw links at layer1 for (int i = 0; i < connection[path[0] - 1].length; i++) { int temp = connection[path[0] - 1][i] - 3; g.drawLine(w + 40, (path[0]) * d.height / 5 + 20, w + 110, temp * d.height / 5 + 20); } /// Check buffer for connections at each step and draw links at layer2 for (int i = 0; i < connection[path[1] - 1].length; i++) { int temp = connection[path[1] - 1][i] - 7; g.drawLine( w + 150, (path[1] - 3) * d.height / 5 + 20, w + 210, (d.height / 11) + 100 * temp + 20); } /// Check buffer for connections at each step and draw links at layer3 for (int i = 0; i < connection[path[2] - 1].length; i++) { int temp = connection[path[2] - 1][i] - 11; g.drawLine(w + 250, (d.height / 11) + 100 * (path[2] - 7) + 20, w + 310, 80 * temp + 40); } /// Draw optimized path for packets traveling between source /// and destination h = d.height / 5; Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(2)); g2.setColor(Color.red); g2.drawLine(w + 40, h * (path[0]) + 20, w + 110, h * (path[1] - 3) + 20); g2.drawLine( w + 150, h * (path[1] - 3) + 20, w + 210, (d.height / 11) + 100 * (path[2] - 7) + 20); g2.drawLine( w + 250, (d.height / 11) + 100 * (path[2] - 7) + 20, w + 310, 80 * (path[3] - 11) + 40); g2.drawLine( w + 350, 80 * (path[3] - 11) + 40, w + 410, (d.height / 11) + 100 * (path[4] - 17) + 20); /// Calculate and display loss, delay, and throughput delayTime = getDelay(path, type); throughPut = getThroughput(path); int numPackLost = getLossRate(numTransfer); lossRate = numPackLost / 100000.0 + 0.0005 * delayTime; delayVal.setText(String.format("%.2f", delayTime)); throuVal.setText(String.valueOf(throughPut)); lossVal.setText(String.format("%.4f", lossRate)); }
public void drawTitleScreen(Graphics g) { // color the screen g.setColor(Color.red); g.fillRect(posx_cursor - 5, posy_cursor - 5, 150, 50); g.fillRect(posx_cursor + 5, posy_cursor - 5, 150, 50); // Place the body of the drawing method here g.setColor(Color.white); g.setFont(new Font("Vineta BT", Font.ITALIC, 50)); g.drawString("SUPER", 125, 80); g.drawString("SMASH", 120, 130); g.drawString("BRICKS", 115, 180); g.setFont(new Font("Vineta BT", Font.PLAIN, 50)); g.drawString("SUPER", 125, 80); g.drawString("SMASH", 120, 130); g.drawString("BRICKS", 115, 180); g.setFont(new Font("Arial", Font.PLAIN, 20)); g.setColor(Color.white); g.fillRect(posx_new, posy_new, 150, 40); g.setColor(Color.black); g.fillRect(185, 255, 140, 30); g.setColor(Color.white); g.setFont(new Font("Imprint MT Shadow", Font.PLAIN, 20)); g.drawString("New Game", 205, 275); g.setColor(Color.white); g.fillRect(posx_diffi, posy_diffi, 150, 40); g.setColor(Color.black); g.fillRect(185, 305, 140, 30); g.setColor(Color.white); g.drawString("Difficulty", 215, 325); g.setColor(Color.white); g.fillRect(posx_controls, posy_controls, 150, 40); g.setColor(Color.black); g.fillRect(185, 355, 140, 30); g.setColor(Color.white); g.drawString("Controls", 217, 375); g.setColor(Color.white); g.fillRect(posx_credits, posy_credits, 150, 40); g.setColor(Color.black); g.fillRect(185, 405, 140, 30); g.setColor(Color.white); g.drawString("Credits", 222, 425); g.setColor(Color.white); g.fillRect(posx_highscores, posy_highscores, 150, 40); g.setColor(Color.black); g.fillRect(185, 455, 140, 30); g.setColor(Color.white); g.drawString("High Scores", 205, 475); g.setFont(new Font("Arial", Font.BOLD, 15)); g.setColor(Color.white); g.drawString("Instructions:", 218, 520); g.setFont(new Font("Arial", Font.PLAIN, 15)); g.drawString("Scroll to the appropriate button and hit [Enter] to continue.", 70, 535); g.drawString("Press [Esc] to return back to Main Menu.", 130, 550); }