/** 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 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); } }
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; }
/** * The printing interface. * * @param g the graphic context. * @param pf the page format. * @param page the page number. * @return PAGE_EXISTS if the page has to be printed. * @throws PrinterException if a printing error occurs. */ public int print(Graphics g, PageFormat pf, int page) throws PrinterException { int npages = 0; // This might be explained as follows: // 1 - The Java printing system normally works with an internal // resolution which is 72 dpi (probably inspired by Postscript). // 2 - To have a sufficient resolution, this is increased by 16 times, // by using the scale method of the graphic object associated to the // printer. This gives a 72 dpi * 16=1152 dpi resolution. // 3 - The 0.127 mm pitch used in FidoCadJ corresponds to a 200 dpi // resolution. Calculating 1152 dpi / 200 dpi gives the 5.76 constant double xscale = 1.0 / 16; // Set 1152 logical units for an inch double yscale = 1.0 / 16; // as the standard resolution is 72 double zoom = 5.76; // act in a 1152 dpi resolution as 1:1 Graphics2D g2d = (Graphics2D) g; // User (0,0) is typically outside the imageable area, so we must // translate by the X and Y values in the PageFormat to avoid clipping if (printMirror) { g2d.translate(pf.getImageableX() + pf.getImageableWidth(), pf.getImageableY()); g2d.scale(-xscale, yscale); } else { g2d.translate(pf.getImageableX(), pf.getImageableY()); g2d.scale(xscale, yscale); } int printerWidth = (int) pf.getImageableWidth() * 16; // Perform an adjustement if we need to fit the drawing to the page. if (printFitToPage) { MapCoordinates zoomm = DrawingSize.calculateZoomToFit( cc.dmp, (int) pf.getImageableWidth() * 16, (int) pf.getImageableHeight() * 16, false); zoom = zoomm.getXMagnitude(); } MapCoordinates m = new MapCoordinates(); m.setMagnitudes(zoom, zoom); PointG o = new PointG(0, 0); int imageWidth = DrawingSize.getImageSize(cc.dmp, zoom, false, o).width; npages = (int) Math.floor((imageWidth - 1) / (double) printerWidth); // Check if we need more than one page if (printerWidth < imageWidth) { g2d.translate(-(printerWidth * page), 0); } // Check if printing is finished. if (page > npages) { return NO_SUCH_PAGE; } // Now we perform our rendering cc.drawingAgent.draw(new Graphics2DSwing(g2d), m); /* tell the caller that this page is part of the printed document */ return PAGE_EXISTS; }