/** * This method will draw the axes. * * @param gc the graphics * @return the (clipped) graphics for the line */ Graphics paintAxis(Graphics gc) { int originx = 0; int originy = 0; Rectangle space = gc.getClipBounds(); int yTickLen = (int) (space.width * axispercent / 100.0); if (yTickLen < 2) { yTickLen = 2; } else if (yTickLen > 5) { yTickLen = 5; } int xTickLen = (int) (space.height * axispercent / 100.0); if (xTickLen < 2) { xTickLen = 2; } else if (xTickLen > 5) { xTickLen = 5; } originx = yTickLen; originy = space.height - xTickLen; // do the Y axis int tickoff = space.height / (numYTicks - 1); int x1 = 0; int x2 = originx; int y1 = originy; for (int tick = 0; tick < numYTicks; tick++) { gc.drawLine(x1, y1, x2, y1); y1 -= tickoff; } gc.drawLine(originx, originy, originx, y1 + tickoff); // do the X axis tickoff = space.width / (numXTicks - 1); x1 = originx; y1 = space.height; int y2 = originy; for (int tick = 0; tick < numXTicks; tick++) { gc.drawLine(x1, y1, x1, y2); x1 += tickoff; } // now draw the X axis gc.drawLine(originx, originy, space.width, originy); gc = gc.create(originx + 1, 0, space.width - originx - 1, originy); return gc; }
/** * Draw the line numbers * * @param g */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); ((Graphics2D) g) .setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Determine the width of the space available to draw the line number FontMetrics fontMetrics = component.getFontMetrics(component.getFont()); Insets insets = getInsets(); int availableWidth = getSize().width - insets.left - insets.right; // Determine the rows to draw within the clipped bounds. Rectangle clip = g.getClipBounds(); int rowStartOffset = component.viewToModel(new Point(0, clip.y)); int endOffset = component.viewToModel(new Point(0, clip.y + clip.height)); while (rowStartOffset <= endOffset) { try { if (isCurrentLine(rowStartOffset)) g.setColor(getCurrentLineForeground()); else g.setColor(getForeground()); // Get the line number as a string and then determine the // "X" and "Y" offsets for drawing the string. String lineNumber = getTextLineNumber(rowStartOffset); int stringWidth = fontMetrics.stringWidth(lineNumber); int x = getOffsetX(availableWidth, stringWidth) + insets.left; int y = getOffsetY(rowStartOffset, fontMetrics); g.drawString(lineNumber, x, y); // Move to the next row rowStartOffset = Utilities.getRowEnd(component, rowStartOffset) + 1; } catch (Exception e) { break; } } }
/** * This method will draw the line. * * @param gc the graphics * @see #setLog */ void paintLine(Graphics g) { double dvalue; int[] values = dataSet.getValues(); // first decide the x scale factor; if ((dataSet == null) || (dataSet.getSize() < 1)) { return; // nothing to draw } // the maximum number of values we are every going to get int capacity = dataSet.getCapacity(); // the available number of values at the moment int nr = dataSet.getSize(); Rectangle space = g.getClipBounds(); // pixel width of x values int width = space.width; int xscale = (int) (width / capacity); if (xscale < 1) { xscale = 1; } width = xscale * capacity; if (width > space.width) { width = space.width; } // pixel width of y values float yscale = (space.height) / (float) (dmax - dmin); // offset into array int xstart = 0; if (width < (nr * xscale)) { // we are lacking space xstart = nr - (int) (width / xscale); } // if needed reallocate some space int totx = nr - xstart + 1; if (xline.length < totx) { xline = new int[totx + 100]; yline = new int[totx + 100]; } // now workout where to put the first point so that // the last one ends up on the right hand edge int x = width - (int) ((nr - xstart - 1) * xscale); if (x < 0) { x = 0; // can't be negative System.out.println("negative start point in graph."); } int y; int ox = x; x += xscale; dvalue = values[xstart]; if (doLog) { dvalue = log10(values[xstart]); } xstart++; int oy = space.height - (int) ((dvalue - dmin) * yscale); int pt = 0; for (int i = xstart; i < nr; i++) { dvalue = values[i]; if (doLog) { dvalue = log10(values[i]); } y = space.height - (int) ((dvalue - dmin) * yscale); xline[pt] = x; yline[pt] = y; pt++; ox = x; oy = y; x += xscale; } g.drawPolyline(xline, yline, pt); }