Search in sources :

Example 11 with Printable

use of java.awt.print.Printable in project jdk8u_jdk by JetBrains.

the class PSPathGraphics method redrawRegion.

/** Redraw a rectanglular area using a proxy graphics
      * To do this we need to know the rectangular area to redraw and
      * the transform & clip in effect at the time of the original drawImage
      *
      */
public void redrawRegion(Rectangle2D region, double scaleX, double scaleY, Shape savedClip, AffineTransform savedTransform) throws PrinterException {
    PSPrinterJob psPrinterJob = (PSPrinterJob) getPrinterJob();
    Printable painter = getPrintable();
    PageFormat pageFormat = getPageFormat();
    int pageIndex = getPageIndex();
    /* Create a buffered image big enough to hold the portion
         * of the source image being printed.
         */
    BufferedImage deepImage = new BufferedImage((int) region.getWidth(), (int) region.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    /* Get a graphics for the application to render into.
         * We initialize the buffer to white in order to
         * match the paper and then we shift the BufferedImage
         * so that it covers the area on the page where the
         * caller's Image will be drawn.
         */
    Graphics2D g = deepImage.createGraphics();
    ProxyGraphics2D proxy = new ProxyGraphics2D(g, psPrinterJob);
    proxy.setColor(Color.white);
    proxy.fillRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
    proxy.clipRect(0, 0, deepImage.getWidth(), deepImage.getHeight());
    proxy.translate(-region.getX(), -region.getY());
    /* Calculate the resolution of the source image.
         */
    float sourceResX = (float) (psPrinterJob.getXRes() / scaleX);
    float sourceResY = (float) (psPrinterJob.getYRes() / scaleY);
    /* The application expects to see user space at 72 dpi.
         * so change user space from image source resolution to
         *  72 dpi.
         */
    proxy.scale(sourceResX / DEFAULT_USER_RES, sourceResY / DEFAULT_USER_RES);
    proxy.translate(-psPrinterJob.getPhysicalPrintableX(pageFormat.getPaper()) / psPrinterJob.getXRes() * DEFAULT_USER_RES, -psPrinterJob.getPhysicalPrintableY(pageFormat.getPaper()) / psPrinterJob.getYRes() * DEFAULT_USER_RES);
    /* NB User space now has to be at 72 dpi for this calc to be correct */
    proxy.transform(new AffineTransform(getPageFormat().getMatrix()));
    proxy.setPaint(Color.black);
    painter.print(proxy, pageFormat, pageIndex);
    g.dispose();
    /* In PSPrinterJob images are printed in device space
         * and therefore we need to set a device space clip.
         */
    psPrinterJob.setClip(savedTransform.createTransformedShape(savedClip));
    /* Scale the bounding rectangle by the scale transform.
         * Because the scaling transform has only x and y
         * scaling components it is equivalent to multiply
         * the x components of the bounding rectangle by
         * the x scaling factor and to multiply the y components
         * by the y scaling factor.
         */
    Rectangle2D.Float scaledBounds = new Rectangle2D.Float((float) (region.getX() * scaleX), (float) (region.getY() * scaleY), (float) (region.getWidth() * scaleX), (float) (region.getHeight() * scaleY));
    /* Pull the raster data from the buffered image
         * and pass it along to PS.
         */
    ByteComponentRaster tile = (ByteComponentRaster) deepImage.getRaster();
    psPrinterJob.drawImageBGR(tile.getDataStorage(), scaledBounds.x, scaledBounds.y, scaledBounds.width, scaledBounds.height, 0f, 0f, deepImage.getWidth(), deepImage.getHeight(), deepImage.getWidth(), deepImage.getHeight());
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) PageFormat(java.awt.print.PageFormat) ByteComponentRaster(sun.awt.image.ByteComponentRaster) AffineTransform(java.awt.geom.AffineTransform) Printable(java.awt.print.Printable)

Aggregations

Printable (java.awt.print.Printable)11 PageFormat (java.awt.print.PageFormat)6 PrinterException (java.awt.print.PrinterException)6 BufferedImage (java.awt.image.BufferedImage)4 PrintRequestAttributeSet (javax.print.attribute.PrintRequestAttributeSet)4 Graphics2D (java.awt.Graphics2D)3 AffineTransform (java.awt.geom.AffineTransform)3 Rectangle2D (java.awt.geom.Rectangle2D)3 PrinterJob (java.awt.print.PrinterJob)3 HashPrintRequestAttributeSet (javax.print.attribute.HashPrintRequestAttributeSet)3 HeadlessException (java.awt.HeadlessException)2 Paper (java.awt.print.Paper)2 PrinterAbortException (java.awt.print.PrinterAbortException)2 File (java.io.File)2 IOException (java.io.IOException)2 MessageFormat (java.text.MessageFormat)2 PrintException (javax.print.PrintException)2 PrintService (javax.print.PrintService)2 Copies (javax.print.attribute.standard.Copies)2 MediaPrintableArea (javax.print.attribute.standard.MediaPrintableArea)2