public void drawCluster(Cluster cluster) { int index = clusters.indexOf(cluster); if (index != -1) { cluster.y = maxY; cluster.x = minX + index * factor; if (cluster.size() > 1) g.setColor(Color.RED); else g.setColor(Color.BLACK); g.drawRect(cluster.x - 1, cluster.y - cluster.size(), 2, 1 + cluster.size()); } else { Cluster left = cluster.getLeft(); Cluster right = cluster.getRight(); drawCluster(left); drawCluster(right); int yBar = minY + (int) ((maxY - minY) * (cluster.getSimilarity() / threshold)); g.setColor(Color.DARK_GRAY); if (left.y > yBar) { g.drawLine(left.x, left.y - 1, left.x, yBar); writeMap(left, yBar); } if (right.y > yBar) { g.drawLine(right.x, right.y - 1, right.x, yBar); writeMap(right, yBar); } g.setColor(Color.BLACK); g.drawLine(left.x, yBar, right.x, yBar); cluster.x = (right.x + left.x) / 2; cluster.y = yBar; } }
// From: http://forum.java.sun.com/thread.jspa?threadID=378460&tstart=135 void drawArrow( Graphics2D g2d, int xCenter, int yCenter, int x, int y, float stroke, BasicStroke drawStroke) { double aDir = Math.atan2(xCenter - x, yCenter - y); // Line can be dashed. g2d.setStroke(drawStroke); g2d.drawLine(x, y, xCenter, yCenter); // make the arrow head solid even if dash pattern has been specified g2d.setStroke(lineStroke); Polygon tmpPoly = new Polygon(); int i1 = 12 + (int) (stroke * 2); // make the arrow head the same size regardless of the length length int i2 = 6 + (int) stroke; tmpPoly.addPoint(x, y); tmpPoly.addPoint(x + xCor(i1, aDir + .5), y + yCor(i1, aDir + .5)); tmpPoly.addPoint(x + xCor(i2, aDir), y + yCor(i2, aDir)); tmpPoly.addPoint(x + xCor(i1, aDir - .5), y + yCor(i1, aDir - .5)); tmpPoly.addPoint(x, y); // arrow tip g2d.drawPolygon(tmpPoly); // Remove this line to leave arrow head unpainted: g2d.fillPolygon(tmpPoly); }
public void draw(node leaf, Graphics2D g, int px, int py) { int lvl = leaf.getLevel(); double l = lvl; counts[lvl]++; double xfraq = counts[lvl] / (spacing[lvl] + 1); double yfraq = l / depth; int x = new Double(1600 * xfraq).intValue(); int y = new Double(1200 * yfraq).intValue() + 10; if (leaf.getAttr() != null) { g.drawString(leaf.getAttr(), x - 20, y); } if (leaf.getCrit() != null) { g.drawString(leaf.getCrit(), x - 20, y + 10); } if (leaf.getResult() != null) { g.drawString(leaf.getResult(), x - 20, y + 10); } g.drawLine(x, y, px, py); // g.fillRect(x,y,20,20); ArrayList children = leaf.getChildren(); while (!children.isEmpty()) { draw((node) children.remove(0), g, x, y); } }
/** * Draws a double headed arrow with arrow heads of a given width and height. * * @param aG the canvas to draw on; * @param aX1 the starting X position of the arrow; * @param aY1 the starting Y position of the arrow; * @param aX2 the ending X position of the arrow; * @param aY2 the ending Y position of the arrow; * @param aArrowWidth the total width of the arrow head; * @param aArrowHeight the total height of the arrow head. */ public static final void drawDoubleHeadedArrow( final Graphics aG, final int aX1, final int aY1, final int aX2, final int aY2, final int aArrowWidth, final int aArrowHeight) { final Graphics2D g2d = (Graphics2D) aG.create(); final int lineWidth = Math.abs(aX2 - aX1); final int threshold = (2 * aArrowWidth) + 2; try { int x1 = aX1; int x2 = aX2; if (lineWidth > threshold) { drawArrowHead(g2d, aX1, aY1, LEFT_FACING, aArrowWidth, aArrowHeight); // why x2 needs to be shifted by one pixel is beyond me... drawArrowHead(g2d, aX2 + 1, aY2, RIGHT_FACING, aArrowWidth, aArrowHeight); x1 += aArrowWidth - 1; x2 -= aArrowWidth + 1; } g2d.drawLine(x1, aY1, x2, aY2); } finally { g2d.dispose(); } }
/** PaintComponent to draw everything. */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.WHITE); // If using images, use cool dragon background if (useImages) g.drawImage(background, 0, 0, 310, 300, null); // Use light gray to not overpower the background image g.setColor(Color.LIGHT_GRAY); for (int y = 1; y < ROWS; ++y) g.fillRoundRect(0, cellSize * y - 4, (cellSize * COLS) - 1, 8, 8, 8); for (int x = 1; x < COLS; ++x) g.fillRoundRect(cellSize * x - 4, 0, 8, (cellSize * ROWS) - 1, 8, 8); // Use graphics2d for when not using the images Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); for (int y = 0; y < ROWS; ++y) { for (int x = 0; x < COLS; ++x) { int x1 = x * cellSize + 16; int y1 = y * cellSize + 16; if (board[y][x] == Symbol.X) { // use image if set to true, otherwise use g2d // for thicker, better looking X's and O's if (useImages) g.drawImage(imageX, x1, y1, 75, 75, null); else { g2d.setColor(PURPLE); int x2 = (x + 1) * cellSize - 16; int y2 = (y + 1) * cellSize - 16; g2d.drawLine(x1, y1, x2, y2); g2d.drawLine(x2, y1, x1, y2); } } else if (board[y][x] == Symbol.O) { if (useImages) g.drawImage(imageO, x1, y1, 75, 75, null); else { g2d.setColor(Color.BLUE); g2d.drawOval(x1, y1, 70, 70); } } } // end for } // Set status bar based on gamestate. If CONTINUE, show whose turn it is if (gameStatus == GameStatus.CONTINUE) { statusBar.setForeground(Color.BLACK); if (currentPlayer == Symbol.X) statusBar.setText("X's Turn"); else statusBar.setText("O's Turn"); } else if (gameStatus == GameStatus.DRAW) { statusBar.setForeground(Color.RED); statusBar.setText("Draw! Click to play again!"); } else if (gameStatus == GameStatus.X_WIN) { statusBar.setForeground(Color.RED); statusBar.setText("X has won! Click to play again!"); } else if (gameStatus == GameStatus.O_WIN) { statusBar.setForeground(Color.RED); statusBar.setText("O has won! Click to play again!"); } }
public void plot(Color c, int x0, int y0, int z0) { Graphics2D g = bi.createGraphics(); g.setColor(c); if (x0 >= XRES || y0 >= YRES || x0 < 0 || y0 < 0) return; if (z0 > zbuffer[x0][y0]) { g.drawLine(x0, y0, x0, y0); zbuffer[x0][y0] = z0; } }
@Override public void paintIcon(Component c, Graphics g, int x, int y) { Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.translate(x, y); g2.setStroke(new BasicStroke(BORDER_WIDTH)); g2.setPaint(LINE_COLOR); g2.draw(BORDER); g2.setStroke(new BasicStroke(SLIT_WIDTH)); g2.setColor(UIManager.getColor("Panel.background")); int n = SLIT_NUM + 1; int v = ICON_SIZE / n; int m = n * v; for (int i = 1; i < n; i++) { int a = i * v; g2.drawLine(a, 0, a, m); g2.drawLine(0, a, m, a); } // g2.drawLine(1 * v, 0 * v, 1 * v, 4 * v); // g2.drawLine(2 * v, 0 * v, 2 * v, 4 * v); // g2.drawLine(3 * v, 0 * v, 3 * v, 4 * v); // g2.drawLine(0 * v, 1 * v, 4 * v, 1 * v); // g2.drawLine(0 * v, 2 * v, 4 * v, 2 * v); // g2.drawLine(0 * v, 3 * v, 4 * v, 3 * v); g2.setPaint(LINE_COLOR); Rectangle2D b = ARROW.getBounds(); Point2D p = new Point2D.Double(b.getX() + b.getWidth() / 2d, b.getY() + b.getHeight() / 2d); AffineTransform toCenterAT = AffineTransform.getTranslateInstance(ICON_SIZE / 2d - p.getX(), ICON_SIZE / 2d - p.getY()); g2.fill(toCenterAT.createTransformedShape(ARROW)); g2.dispose(); }
public void paintProbe(Graphics2D g2, int x1, int y1, int x2, int y2, double tmin, double tmax) { g2.setStroke(stroke); g2.setColor(color); int ppx = -1, ppy = -1; for (int i = 0; i < values.size(); i++) { Double v = values.get(i); if (v != null && tmax - tmin > 0.0) { double frac = (v - tmin) / (tmax - tmin); int pixY = y2 - (int) Math.round(frac * (y2 - y1)); int pixX = x1 + (int) Math.floor((double) (i + 1) / (double) (values.size() + 2)); if (ppx != -1) { g2.drawLine(ppx, ppy, pixX, pixY); } ppx = pixX; ppy = pixY; } else { ppx = ppy = -1; } } }
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; int h = getHeight(); int w = getWidth(); double factor = (h - (MARGIN_BOTTOM * 1.0)) / RSSI_MAX_VALUE; double sSpacing = (w - MARGIN_RIGHT) / (TOTAL * 1.0); int sWidth = (int) (sSpacing - 1); if (sWidth == 0) sWidth = 1; // Set white background in the plot g.setColor(Color.white); g.fillRect(0, 0, w, h); // Gradient example (ytics background) GradientPaint greytowhite = new GradientPaint(w - MARGIN_RIGHT, 0, Color.WHITE, w, 0, Color.lightGray, false); g2.setPaint(greytowhite); g2.fillRect(w - MARGIN_RIGHT, 0, w, h); // Draw the light grey channels from 11 to 26 double xpos = 10; for (int i = 4; i < TOTAL - 4; i++) { if (i == 4 + 5 * (INTERFERED_CHANNEL - 11)) g.setColor(Color.cyan); else g.setColor(Color.lightGray); g.fillRect((int) (xpos + i * sSpacing), 0, (int) (sSpacing * 3), h - MARGIN_BOTTOM); i = i + 4; g.setColor(Color.blue); g.drawString( String.valueOf(((i - 8) / 5) + 11), (int) (xpos + (i - 4) * sSpacing), MARGIN_TOP); } g.drawString(String.valueOf("Channel"), (int) (w - MARGIN_RIGHT + 20), MARGIN_TOP); // Write the y-axis with dBm int base_dBm = -100; // The bottom corresponds to -100 dBm int ytics = 10; // How many tics on the y-axis for (int i = -ytics; i <= 0; i++) { g.setColor(Color.red); g.drawString( String.valueOf(((ytics + i) * (base_dBm / ytics)) + "dBm"), (int) (w - MARGIN_RIGHT + 20), (int) (h + i * (h / ytics)) - MARGIN_BOTTOM); // Dashed line for the ytics final float dash1[] = {10.0f}; final BasicStroke dashed = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f); g2.setStroke(dashed); g2.drawLine( 0, (int) (h + i * (h / ytics)) - MARGIN_BOTTOM, (int) (w - MARGIN_RIGHT + 7), (int) (h + i * (h / ytics)) - MARGIN_BOTTOM); } // Write the x-axis with MHz g.setColor(Color.blue); int start_x_axis = 4; g.drawString(String.valueOf(2400 + "MHz"), (int) xpos, (int) h - start_x_axis); g.drawString(String.valueOf(2442.5 + "MHz"), (w - 119) / 2, (int) h - start_x_axis); g.drawString(String.valueOf(2485 + "MHz"), w - 119, (int) h - start_x_axis); // Draw the old RSSI in each MHz channel (grey) g.setColor(Color.gray); double xposition = xpos; for (int i = 0, n = rssi.length; i < n; i++) { int rssi = (int) (rssiMax[i] * factor); g.fillRect((int) xposition, h - MARGIN_BOTTOM - rssi, sWidth, rssi + 1); xposition += sSpacing; } // Draw the current RSSI in each MHz channel (black) g.setColor(Color.black); xposition = xpos; for (int i = 0, n = rssi.length; i < n; i++) { int rssiVal = (int) (rssi[i] * factor); g.fillRect((int) xposition, h - MARGIN_BOTTOM - rssiVal, sWidth, rssiVal + 1); xposition += sSpacing; } }
/** Get an image based on index numbers */ public BufferedImage getIndexedImage( int x, int y, int zoom, int cacheZoom, GMapListener listener) { if (listener != null) { if (!getGDataSource().isCached(x, y, zoom)) { listener.updateGMapPainting(); listener.updateGMapMessage(GMap.MESSAGE_DOWNLOADING); } else { listener.updateGMapMessage(GMap.MESSAGE_PAINTING); } } BufferedImage thumbImage = getGDataSource().getImage(x, y, zoom, true); if (thumbImage == null) return defaultImage; // if we dont have to paint cache, return here if (cacheZoom == (GPhysicalPoint.MIN_ZOOM - 1) || cacheZoom >= zoom) return thumbImage; BufferedImage paintedImage = new BufferedImage( GDataSource.sourceSize.width, GDataSource.sourceSize.height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics2D = paintedImage.createGraphics(); graphics2D.drawImage( thumbImage, 0, 0, GDataSource.sourceSize.width, GDataSource.sourceSize.height, null); // now lets move to painting the cache double imageNum = Math.pow(2, zoom - cacheZoom); // draw cache lines int startX = (int) (imageNum * x); int startY = (int) (imageNum * y); // get composite to restore later, set new transparent composite Composite originalComposite = graphics2D.getComposite(); graphics2D.setComposite(opacity40); // draw grid for (int i = 0; i < imageNum; i++) { for (int j = 0; j < imageNum; j++) { // points Point upperLeft = new Point( (int) (GDataSource.sourceSize.width / imageNum) * i, (int) (GDataSource.sourceSize.height / imageNum) * j); Dimension size = new Dimension( (int) (GDataSource.sourceSize.width / imageNum), (int) (GDataSource.sourceSize.height / imageNum)); // draw lines graphics2D.setColor(new Color(100, 100, 100)); graphics2D.drawLine(upperLeft.x, upperLeft.y, upperLeft.x + size.width, upperLeft.y); graphics2D.drawLine(upperLeft.x, upperLeft.y, upperLeft.x, upperLeft.y + size.height); // check if file exists if (getGDataSource().isCached(startX + i, startY + j, cacheZoom)) graphics2D.setColor(Color.RED); else graphics2D.setColor(new Color(155, 155, 155)); // shade rectangle graphics2D.fillRect(upperLeft.x, upperLeft.y, size.width, size.height); } } // restore composite graphics2D.setComposite(originalComposite); return paintedImage; }
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 VolatileImage render() { Graphics2D g2d = renderImage.createGraphics(); try { // Draw background layer: for (int i = 0; i < backgroundLayer.length; i++) { g2d.drawImage( backgroundLayer[i], -(int) (camera.position.x * Math.pow(0.5, backgroundLayer.length - i)), -(int) (camera.position.y * Math.pow(0.5, backgroundLayer.length - i)) + backgroundLayer[i].getHeight(this) - backgroundLayer[i].getHeight(this) / (i + 1), this); } } catch (Exception e) { } // Draw Tiles: (new) try { g2d.drawImage( tileLayer, camera.center.x - camera.position.x, camera.center.y - camera.position.y, this); } catch (Exception e) { } // Draw all kinds of Sprites: try { int a = 0; while (sprite[a] != null) { // Play Animation for sprite: if (sprite[a].animation.plays == true) { sprite[a].getAnimation().nextFrame(); } // -- Draw sprite: g2d.drawImage( sprite[a].img, /*X1*/ sprite[a].posx + ((sprite[a].flipH - 1) / (-2)) * sprite[a].size.width /*camera*/ - camera.position.x + camera.center.x, /*Y1*/ sprite[a].posy + ((sprite[a].flipV - 1) / (-2)) * sprite[a].size.height /*camera*/ - camera.position.y + camera.center.y, /*X2*/ sprite[a].posx + sprite[a].size.width * sprite[a].flipH + ((sprite[a].flipH - 1) / (-2)) * sprite[a].size.width /*camera*/ - camera.position.x + camera.center.x, /*Y2*/ sprite[a].posy + sprite[a].size.height * sprite[a].flipV + ((sprite[a].flipV - 1) / (-2)) * sprite[a].size.height /*camera*/ - camera.position.y + camera.center.y, // destination sprite[a].getAnimation().col * sprite[a].size.width, sprite[a].getAnimation().row * sprite[a].size.height, // source (sprite[a].getAnimation().col + 1) * sprite[a].size.width, (sprite[a].getAnimation().row + 1) * sprite[a].size.height, this); a++; } } catch (Exception e) { g2d.drawString("Error drawing a Sprite", 20, 20); } // Draw "GUI": g2d.drawImage(coinSpriteSheet, 16, 16, 32, 32, 0, 0, 16, 16, this); g2d.setColor(Color.BLACK); g2d.drawString("x " + collectedCoins, 32, 30); g2d.setColor(Color.WHITE); g2d.drawString("x " + collectedCoins, 32, 29); if (showSpritePos == true) { for (int i = 0; i < numberOfSprites; i++) { g2d.setColor(Color.red); g2d.drawRect( /*X1*/ sprite[i].posx /*camera*/ - camera.position.x + camera.center.x, /*Y1*/ sprite[i].posy /*camera*/ - camera.position.y + camera.center.y, 1, 1); g2d.setColor(Color.black); } } if (showSpriteNum == true) { for (int i = 0; i < numberOfSprites; i++) { g2d.setColor(Color.black); g2d.drawString( "" + i, /*X1*/ sprite[i].posx /*camera*/ - camera.position.x + camera.center.x, /*Y1*/ sprite[i].posy /*camera*/ - camera.position.y + camera.center.y); g2d.setColor(Color.white); g2d.drawString( "" + i, /*X1*/ sprite[i].posx /*camera*/ - camera.position.x + camera.center.x, /*Y1*/ sprite[i].posy /*camera*/ - camera.position.y + camera.center.y - 1); } } if (showSpritePos == true) { for (int i = 0; i < numberOfTiles; i++) { g2d.setColor(Color.red); g2d.drawRect( /*X1*/ tile[i].posx + ((tile[i].flipH - 1) / (-2)) * tile[i].size.width /*camera*/ - camera.position.x + camera.center.x, /*Y1*/ tile[i].posy + ((tile[i].flipV - 1) / (-2)) * tile[i].size.height /*camera*/ - camera.position.y + camera.center.y, 1, 1); g2d.setColor(Color.black); } } if (showCamera == true) { g2d.setColor(Color.red); g2d.drawLine( 0, camera.prefHeight - camera.position.y + camera.center.y, loadedLevel.getWidth() * 16, camera.prefHeight - camera.position.y + camera.center.y); g2d.setColor(new Color(1, 0, 0, 0.33f)); g2d.fillRect( 0, camera.prefHeight - camera.position.y + camera.tolerance, loadedLevel.getWidth() * 16, camera.tolerance); g2d.setColor(new Color(0, 1, 0, 0.33f)); g2d.fillRect( camera.center.x - camera.position.x + camera.center.x, camera.center.y - camera.position.y + camera.center.y, camera.bounds.width - 2 * camera.center.x, camera.bounds.height); g2d.setColor(Color.green); g2d.drawLine( camera.center.x - camera.position.x + camera.center.x, 0, camera.center.x - camera.position.x + camera.center.x, 999); g2d.drawLine( camera.bounds.width - camera.center.x - camera.position.x + camera.center.x, 0, camera.bounds.width - camera.center.x - camera.position.x + camera.center.x, 999); } return renderImage; }
public void drawLine(int x1, int y1, int x2, int y2) { gRef.drawLine(x1, y1, x2, y2); }
private void drawPlot(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setColor(Color.white); g2d.fillRect(0, 0, getWidth(), getHeight()); double activeWidth = getWidth() - leftMargin - rightMargin; double activeHeight = getHeight() - topMargin - bottomMargin; int bottomY = getHeight() - bottomMargin; int rightX = getWidth() - rightMargin; // draw data line int x1, x2, y1, y2; g2d.setColor(Color.red); for (int i = 1; i < numComponents; i++) { x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth); y1 = (int) (bottomY - (plotData[i - 1][plotIndex] * 10.0) / 1000 * activeHeight); x2 = (int) (leftMargin + ((double) (i) / (numComponents - 1)) * activeWidth); y2 = (int) (bottomY - (plotData[i][plotIndex] * 10) / 1000 * activeHeight); g2d.drawLine(x1, y1, x2, y2); } // draw data points int radius = 2; for (int i = 0; i < numComponents; i++) { x1 = (int) (leftMargin + ((double) (i) / (numComponents - 1)) * activeWidth); y1 = (int) (bottomY - (plotData[i][plotIndex] * 10.0) / 1000 * activeHeight); g2d.drawOval(x1 - radius - 1, y1 - radius - 1, 2 * radius + 2, 2 * radius + 2); } // draw axes g2d.setColor(Color.black); g2d.drawLine(leftMargin, bottomY, rightX, bottomY); g2d.drawLine(leftMargin, bottomY, leftMargin, topMargin); g2d.drawLine(leftMargin, topMargin, rightX, topMargin); g2d.drawLine(rightX, bottomY, rightX, topMargin); // draw ticks int tickSize = 4; for (int i = 1; i <= numComponents; i++) { x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth); g2d.drawLine(x1, bottomY, x1, bottomY + tickSize); } for (int i = 0; i <= 1000; i += 100) { y1 = (int) (bottomY - i / 1000.0 * activeHeight); g2d.drawLine(leftMargin, y1, leftMargin - tickSize, y1); } // labels DecimalFormat df = new DecimalFormat("#,###,###.###"); Font font = new Font("SanSerif", Font.PLAIN, 11); FontMetrics metrics = g.getFontMetrics(font); int hgt, adv; hgt = metrics.getHeight(); // x-axis labels String label; for (int i = 1; i <= numComponents; i++) { label = String.valueOf(i); x1 = (int) (leftMargin + ((double) (i - 1) / (numComponents - 1)) * activeWidth); adv = metrics.stringWidth(label) / 2; g2d.drawString(label, x1 - adv, bottomY + hgt + 4); } label = "Component"; adv = metrics.stringWidth(label); int xAxisMidPoint = (int) (leftMargin + activeWidth / 2); g2d.drawString(label, xAxisMidPoint - adv / 2, bottomY + 2 * hgt + 6); // y-axis labels // rotate the font Font oldFont = g.getFont(); Font f = oldFont.deriveFont(AffineTransform.getRotateInstance(-Math.PI / 2.0)); g2d.setFont(f); int yAxisMidPoint = (int) (topMargin + activeHeight / 2); int offset; label = "Explained Variance (%)"; offset = metrics.stringWidth("100.0") + 12 + hgt; adv = metrics.stringWidth(label); g2d.drawString(label, leftMargin - offset, yAxisMidPoint + adv / 2); // replace the rotated font. g2d.setFont(oldFont); df = new DecimalFormat("0.0"); for (int i = 0; i <= 1000; i += 100) { label = df.format(i / 10); y1 = (int) (bottomY - i / 1000.0 * activeHeight); adv = metrics.stringWidth(label); g2d.drawString(label, leftMargin - adv - 12, y1 + hgt / 2); } // title // bold font oldFont = g.getFont(); font = font = new Font("SanSerif", Font.BOLD, 12); g2d.setFont(font); label = "PCA Scree Plot"; adv = metrics.stringWidth(label); g2d.drawString(label, getWidth() / 2 - adv / 2, topMargin - hgt - 5); g2d.setFont(oldFont); }
// also clip, transform, composite, // public boolean isOpaque(){return false;}//theOpaque!=null&&theOpaque;} // --------------------------------------------------------- private void doPaint(Graphics2D g, int s, Object o) { // process an operation from the buffer // System.out.println(s); Object o1 = null, o2 = null, o3 = null, o4 = null, o5 = null, o6 = null, o7 = null, o8 = null, o9 = null, o10 = null, o11 = null; if (o instanceof Object[]) { Object[] a = (Object[]) o; if (a.length > 0) o1 = a[0]; if (a.length > 1) o2 = a[1]; if (a.length > 2) o3 = a[2]; if (a.length > 3) o4 = a[3]; if (a.length > 4) o5 = a[4]; if (a.length > 5) o6 = a[5]; if (a.length > 6) o7 = a[6]; if (a.length > 7) o8 = a[7]; if (a.length > 8) o9 = a[8]; if (a.length > 9) o10 = a[9]; if (a.length > 10) o11 = a[10]; } switch (s) { case clear: paintBackground(g, theBackground); break; // public void addRenderingHints(Map<?,?> hints) // {toBuffer("addRenderingHints",hints );} case addRenderingHints: g.addRenderingHints((Map<?, ?>) o); break; case clip1: g.clip((Shape) o); break; case draw1: g.draw((Shape) o); break; case draw3DRect: g.draw3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5); break; case drawGlyphVector: g.drawGlyphVector((GlyphVector) o1, (Float) o2, (Float) o3); break; case drawImage1: g.drawImage((BufferedImage) o1, (BufferedImageOp) o2, (Integer) o3, (Integer) o4); break; case drawImage2: g.drawImage((Image) o1, (AffineTransform) o2, (ImageObserver) o3); break; case drawRenderableImage: g.drawRenderableImage((RenderableImage) o1, (AffineTransform) o2); break; case drawRenderedImage: g.drawRenderedImage((RenderedImage) o1, (AffineTransform) o2); break; case drawString1: g.drawString((AttributedCharacterIterator) o1, (Float) o2, (Float) o3); break; case drawString2: g.drawString((AttributedCharacterIterator) o1, (Integer) o2, (Integer) o3); break; case drawString3: g.drawString((String) o1, (Float) o2, (Float) o3); break; case drawString4: g.drawString((String) o1, (Integer) o2, (Integer) o3); break; case fill: g.fill((Shape) o); break; case fill3DRect: g.fill3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5); break; case rotate1: g.rotate((Double) o); break; case rotate2: g.rotate((Double) o1, (Double) o2, (Double) o3); break; case scale1: g.scale((Double) o1, (Double) o2); break; case setBackground: g.setBackground( (Color) o); // paintBackground(g,(Color)o); /*super.setBackground((Color)o) ;*/ break; case setComposite: g.setComposite((Composite) o); break; case setPaint: g.setPaint((Paint) o); break; case setRenderingHint: g.setRenderingHint((RenderingHints.Key) o1, o2); break; case setRenderingHints: g.setRenderingHints((Map<?, ?>) o); break; case setStroke: g.setStroke((Stroke) o); break; case setTransform: g.setTransform(makeTransform(o)); break; case shear: g.shear((Double) o1, (Double) o2); break; case transform1: g.transform(makeTransform(o)); break; case translate1: g.translate((Double) o1, (Double) o2); break; case translate2: g.translate((Integer) o1, (Integer) o2); break; case clearRect: g.clearRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case copyArea: g.copyArea( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case drawArc: g.drawArc( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case drawBytes: g.drawBytes((byte[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5); break; case drawChars: g.drawChars((char[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5); break; case drawImage4: g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (Color) o4, (ImageObserver) o5); break; case drawImage5: g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (ImageObserver) o4); break; case drawImage6: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Color) o6, (ImageObserver) o7); break; case drawImage7: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (ImageObserver) o6); break; case drawImage8: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6, (Integer) o7, (Integer) o8, (Integer) o9, (Color) o10, (ImageObserver) o11); break; case drawImage9: g.drawImage( (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6, (Integer) o7, (Integer) o8, (Integer) o9, (ImageObserver) o10); break; case drawLine: g.drawLine((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawOval: g.drawOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawPolygon1: g.drawPolygon((int[]) o1, (int[]) o2, (Integer) o3); break; case drawPolygon2: g.drawPolygon((Polygon) o); break; case drawPolyline: g.drawPolyline((int[]) o1, (int[]) o2, (Integer) o3); break; case drawRect: g.drawRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case drawRoundRect: g.drawRoundRect( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case fillArc: g.fillArc( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case fillOval: g.fillOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; // {toBuffer("fillPolygon",mkArg(xPoints, yPoints, nPoints) );} case fillPolygon1: g.fillPolygon((int[]) o1, (int[]) o2, (Integer) o3); break; case fillPolygon2: g.fillPolygon((Polygon) o); break; case fillRect: g.fillRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4); break; case fillRoundRect: g.fillRoundRect( (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6); break; case setClip1: g.setClip((Shape) o); break; case setColor: g.setColor((Color) o); break; case setFont: g.setFont((Font) o); break; case setPaintMode: g.setPaintMode(); break; case setXORMode: g.setXORMode((Color) o); break; case opaque: super.setOpaque((Boolean) o); break; case drawOutline: // g.drawString((String)o1, (Integer)o2, (Integer)o3) ;break; { FontRenderContext frc = g.getFontRenderContext(); TextLayout tl = new TextLayout((String) o1, g.getFont(), frc); Shape s1 = tl.getOutline(null); AffineTransform af = g.getTransform(); g.translate((Integer) o2, (Integer) o3); g.draw(s1); g.setTransform(af); } ; break; default: System.out.println("Unknown image operation " + s); } }