/** This method is required to implement the Printable interface */ public int print(Graphics g, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= 1) { return NO_SUCH_PAGE; } Graphics2D g2 = (Graphics2D) g; Dimension cs = printTarget.getSize(); g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY()); double imageableWidth = pageFormat.getImageableWidth(); double imageableHeight = pageFormat.getImageableHeight(); double scale = 1; if (cs.width >= imageableWidth) { scale = imageableWidth / cs.width; } g2.scale(scale, scale); // g2.translate((imageableWidth - cs.width)*scale/2, // (imageableHeight - cs.height)*scale/2); printTarget.paintAll(g2); return Printable.PAGE_EXISTS; }
public int print(Graphics g, PageFormat pf, int pageIndex) { int response = NO_SUCH_PAGE; Graphics2D g2 = (Graphics2D) g; disableDoubleBuffering(componentToBePrinted); Dimension d = componentToBePrinted.getSize(); double panelWidth = d.width; double panelHeight = d.height; double pageHeight = pf.getImageableHeight(); double pageWidth = pf.getImageableWidth(); double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); if (pageIndex >= totalNumPages) { response = NO_SUCH_PAGE; } else { g2.translate(pf.getImageableX(), pf.getImageableY()); g2.translate(0f, -pageIndex * pageHeight); g2.scale(scale, scale); componentToBePrinted.paint(g2); enableDoubleBuffering(componentToBePrinted); response = Printable.PAGE_EXISTS; } return response; }
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { // Component printMe = getPrintComponent(); Graphics2D g2 = (Graphics2D) g; g2.setColor(Color.black); // set default foreground color to black // for faster printing, turn off double buffering // RepaintManager.currentManager(this).setDoubleBufferingEnabled(false); Dimension d = printComponent.getSize(); // get size of document double panelWidth = d.width; // width in pixels double panelHeight = d.height; // height in pixels double pageHeight = pf.getImageableHeight(); // height of printer page double pageWidth = pf.getImageableWidth(); // width of printer page double scale = pageWidth / panelWidth; int totalNumPages = (int) Math.ceil(scale * panelHeight / pageHeight); // make sure we don't print empty pages if (pageIndex >= totalNumPages) { return Printable.NO_SUCH_PAGE; } // shift Graphic to line up with beginning of print-imageable region g2.translate(pf.getImageableX(), pf.getImageableY()); // shift Graphic to line up with beginning of next page to print g2.translate(0f, -pageIndex * pageHeight); // scale the page so the width fits... g2.scale(scale, scale); // PRINT IT! printComponent.paint(g2); return Printable.PAGE_EXISTS; }
public int print(Graphics g, PageFormat f, int pageIndex) { if (pageIndex > 0) { return (NO_SUCH_PAGE); } else { Graphics2D g2d = (Graphics2D) g; // Rectangle2D rect = new Rectangle2D.Double(f.getImageableX(), // f.getImageableY(), // f.getImageableWidth(), // f.getImageableHeight()); f.getImageableX(); f.getImageableY(); f.getImageableWidth(); f.getImageableHeight(); // g2d.translate(f.getImageableX(), f.getImageableY()); disableDoubleBuffering(componentToBePrinted); componentToBePrinted.print(g2d) /*.paint(g2d)*/; enableDoubleBuffering(componentToBePrinted); return (PAGE_EXISTS); } }