@Override public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { System.out.println("PageFormat: width=" + pf.getWidth() + ", height=" + pf.getHeight()); Logger.getGlobal().log(Level.INFO, "PageFormat {0}", pf); System.out.println("pageIndex " + pageIndex); Logger.getGlobal().log(Level.INFO, "pageIndex {0}", pageIndex); if (pageIndex == 0) { Graphics2D g2d = (Graphics2D) g; Font font = g2d.getFont(); g2d.setFont(font.deriveFont((float) fontSize)); g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.setColor(Color.black); int step = g2d.getFont().getSize(); step += step / 4; double y = paddingTop + g2d.getFont().getSize(); for (String s : printStringList) { Logger.getGlobal().log(Level.INFO, "printStringList: {0}", s); g2d.drawString(s, (float) paddingLeft, (float) y); y += step; } // g2d.fillRect(0, 0, 200, 200); return Printable.PAGE_EXISTS; } else { return Printable.NO_SUCH_PAGE; } }
/** * Gets the page count of this section. * * @param g2 the graphics context * @param pf the page format * @return the number of pages needed */ public int getPageCount(Graphics2D g2, PageFormat pf) { if (message.equals("")) return 0; FontRenderContext context = g2.getFontRenderContext(); Font f = new Font("Serif", Font.PLAIN, 72); Rectangle2D bounds = f.getStringBounds(message, context); scale = pf.getImageableHeight() / bounds.getHeight(); double width = scale * bounds.getWidth(); int pages = (int) Math.ceil(width / pf.getImageableWidth()); return pages; }
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); }