Search in sources :

Example 1 with ProxyGraphics2D

use of sun.print.ProxyGraphics2D in project jdk8u_jdk by JetBrains.

the class WPathGraphics method redrawRegion.

/**
     * Have the printing application redraw everything that falls
     * within the page bounds defined by <code>region</code>.
     */
@Override
public void redrawRegion(Rectangle2D region, double scaleX, double scaleY, Shape savedClip, AffineTransform savedTransform) throws PrinterException {
    WPrinterJob wPrinterJob = (WPrinterJob) 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, wPrinterJob);
    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) (wPrinterJob.getXRes() / scaleX);
    float sourceResY = (float) (wPrinterJob.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(-wPrinterJob.getPhysicalPrintableX(pageFormat.getPaper()) / wPrinterJob.getXRes() * DEFAULT_USER_RES, -wPrinterJob.getPhysicalPrintableY(pageFormat.getPaper()) / wPrinterJob.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();
    /* We need to set the device clip using saved information.
         * savedClip intersects the user clip with a clip that restricts
         * the GDI rendered area of our BufferedImage to that which
         * may correspond to a rotate or shear.
         * The saved device transform is needed as the current transform
         * is not likely to be the same.
         */
    if (savedClip != null) {
        deviceClip(savedClip.getPathIterator(savedTransform));
    }
    /* Scale the bounding rectangle by the scale transform.
         * Because the scaling transform has only x and y
         * scaling components it is equivalent to multiplying
         * the x components of the bounding rectangle by
         * the x scaling factor and to multiplying 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 GDI.
         */
    ByteComponentRaster tile = (ByteComponentRaster) deepImage.getRaster();
    wPrinterJob.drawImage3ByteBGR(tile.getDataStorage(), scaledBounds.x, scaledBounds.y, scaledBounds.width, scaledBounds.height, 0f, 0f, deepImage.getWidth(), deepImage.getHeight());
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ProxyGraphics2D(sun.print.ProxyGraphics2D) PageFormat(java.awt.print.PageFormat) ByteComponentRaster(sun.awt.image.ByteComponentRaster) AffineTransform(java.awt.geom.AffineTransform) Printable(java.awt.print.Printable) ProxyGraphics2D(sun.print.ProxyGraphics2D)

Aggregations

Graphics2D (java.awt.Graphics2D)1 AffineTransform (java.awt.geom.AffineTransform)1 Rectangle2D (java.awt.geom.Rectangle2D)1 BufferedImage (java.awt.image.BufferedImage)1 PageFormat (java.awt.print.PageFormat)1 Printable (java.awt.print.Printable)1 ByteComponentRaster (sun.awt.image.ByteComponentRaster)1 ProxyGraphics2D (sun.print.ProxyGraphics2D)1