protected void drawVectors(Graphics g, Rectangle w) { int ix0, iy0; int ix1, iy1; double x0, y0, x1, y1, dx, dy; // Is there any data to draw? Sometimes the draw command will // will be called before any data has been placed in the class. if (data == null || data.length < stride) return; // Lets draw the vectors for (int i = 0; i < length; i += stride) { x0 = data[i]; y0 = data[i + 1]; dx = data[i + 2] * getScaleFactor(); dy = data[i + 3] * getScaleFactor(); x1 = x0 + dx; y1 = y0 + dy; if (inside(x0, y0) || inside(x1, y1)) { ix0 = (int) (w.x + ((x0 - xmin) / xrange) * w.width); iy0 = (int) (w.y + (1.0 - (y0 - ymin) / yrange) * w.height); ix1 = (int) (w.x + ((x1 - xmin) / xrange) * w.width); iy1 = (int) (w.y + (1.0 - (y1 - ymin) / yrange) * w.height); g.drawLine(ix0, iy0, ix1, iy1); /* ** Now draw the head of the vector. To avoid scaling problems ** the head is drawn using pixel units. This would not work ** if we had multiple output devices. */ dx = (double) (ix1 - ix0); dy = (double) (iy1 - iy0); ix0 = ix1 - (int) (0.25 * (dx - dy) + 0.5); iy0 = iy1 - (int) (0.25 * (dx + dy) + 0.5); g.drawLine(ix0, iy0, ix1, iy1); ix0 = ix1 - (int) (0.25 * (dx + dy) + 0.5); iy0 = iy1 - (int) (0.25 * (-dx + dy) + 0.5); g.drawLine(ix0, iy0, ix1, iy1); } } }
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)); }
/** * Draw a legend for this Vector set * * @param g Graphics context * @param w Data Window */ protected void draw_legend(Graphics g, Rectangle w) { Color c = g.getColor(); TextLine value = new TextLine(); double dx; int ix, iy; int length; if (!drawlegend) return; /* ** Calculate the vector magnitude of a line legend_length ** pixels long. This will be our standard vector */ dx = xrange * ((double) legend_length) / ((double) w.width) / getScaleFactor(); value.parseDouble(dx, 3); /* ** Calculate the length of the legend */ length = legend_length + value.getWidth(g) + value.charWidth(g, ' '); /* ** Calculate the position of the legend if needed. */ if (legend_ix == 0 && legend_iy == 0) { legend_ix = (int) (w.x + ((legend_dx - xmin) / xrange) * w.width); legend_iy = (int) (w.y + (1.0 - (legend_dy - ymin) / yrange) * w.height); } else if (legend_ix == -1 && legend_iy == -1) { legend_ix = w.x + w.width / 2 - length / 2; legend_iy = w.y - value.getAscent(g) / 2; } /* ** In what follows the vector tail is the zero point. It is on ** the right - the vector points to the left */ if (linecolor != null) g.setColor(linecolor); /* ** Draw the standard vector */ g.drawLine(legend_ix, legend_iy, legend_ix + legend_length, legend_iy); ix = legend_ix + (int) (0.25 * (double) legend_length + 0.5); iy = legend_iy - (int) (0.25 * (double) legend_length + 0.5); g.drawLine(legend_ix, legend_iy, ix, iy); ix = legend_ix + (int) (0.25 * (double) legend_length + 0.5); iy = legend_iy + (int) (0.25 * (double) legend_length + 0.5); g.drawLine(legend_ix, legend_iy, ix, iy); /* ** Add the value of the standard vector. To the right of the vector */ value.draw(g, legend_ix + legend_length + value.charWidth(g, ' '), iy, TextLine.LEFT); /* ** Add any legend text (above the vector) that might have been ** defined. */ g.setColor(c); if (legend_text != null && !legend_text.isNull()) { legend_text.draw( g, legend_ix + length / 2, iy - value.getAscent(g) - legend_text.getDescent(g) - legend_text.getLeading(g), TextLine.CENTER); } }