Java 类java.awt.print.Printable 实例源码

项目:OpenDA    文件:PlotBox.java   
/**
 * Print the plot to a printer, represented by the specified graphics
 * object.
 *
 * @param graphics The context into which the page is drawn.
 * @param format The size and orientation of the page being drawn.
 * @param index The zero based index of the page to be drawn.
 * @return PAGE_EXISTS if the page is rendered successfully, or
 * NO_SUCH_PAGE if pageIndex specifies a non-existent page.
 * @exception PrinterException If the print job is terminated.
 */

public synchronized int print(Graphics graphics, PageFormat format,
        int index) throws PrinterException {
    if (graphics == null) return Printable.NO_SUCH_PAGE;
    // We only print on one page.
    if (index >= 1) {
        return Printable.NO_SUCH_PAGE;
    }
    Graphics2D graphics2D = (Graphics2D) graphics;
    // Scale the printout to fit the pages.
    // Contributed by Laurent ETUR, Schlumberger Riboud Product Center
    double scalex = format.getImageableWidth() / (double) getWidth();
    double scaley = format.getImageableHeight() / (double) getHeight();
    double scale = Math.min(scalex, scaley);
    graphics2D.translate((int)format.getImageableX(),
            (int)format.getImageableY());
    graphics2D.scale(scale, scale);
    _drawPlot(graphics, true);
    return Printable.PAGE_EXISTS;
}
项目:OpenJSharp    文件:ImagePrinter.java   
public int print(Graphics g, PageFormat pf, int index) {

        if (index > 0 || image == null) {
            return Printable.NO_SUCH_PAGE;
        }

        ((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        int iw = (int)pf.getImageableWidth();
        int ih = (int)pf.getImageableHeight();

        // ensure image will fit
        int dw = w;
        int dh = h;
        if (dw > iw) {
            dh = (int)(dh * ( (float) iw / (float) dw)) ;
            dw = iw;
        }
        if (dh > ih) {
            dw = (int)(dw * ( (float) ih / (float) dh)) ;
            dh = ih;
        }
        // centre on page
        int dx = (iw - dw) / 2;
        int dy = (ih - dh) / 2;

        g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
        return Printable.PAGE_EXISTS;
    }
项目:OpenJSharp    文件:PSPrinterJob.java   
public EPSPrinter(Printable printable, String title,
                  PrintStream stream,
                  int x, int y, int wid, int hgt) {

    this.printable = printable;
    this.epsTitle = title;
    this.stream = stream;
    llx = x;
    lly = y;
    urx = llx+wid;
    ury = lly+hgt;
    // construct a PageFormat with zero margins representing the
    // exact bounds of the applet. ie construct a theoretical
    // paper which happens to exactly match applet panel size.
    Paper p = new Paper();
    p.setSize((double)wid, (double)hgt);
    p.setImageableArea(0.0,0.0, (double)wid, (double)hgt);
    pf = new PageFormat();
    pf.setPaper(p);
}
项目:OpenJSharp    文件:TextComponentPrintable.java   
/**
 * Returns {@code TextComponentPrintable} to print {@code textComponent}.
 *
 * @param textComponent {@code JTextComponent} to print
 * @param headerFormat the page header, or {@code null} for none
 * @param footerFormat the page footer, or {@code null} for none
 * @return {@code TextComponentPrintable} to print {@code textComponent}
 */
public static Printable getPrintable(final JTextComponent textComponent,
        final MessageFormat headerFormat,
        final MessageFormat footerFormat) {

    if (textComponent instanceof JEditorPane
            && isFrameSetDocument(textComponent.getDocument())) {
        //for document with frames we create one printable per
        //frame and merge them with the CompoundPrintable.
        List<JEditorPane> frames = getFrames((JEditorPane) textComponent);
        List<CountingPrintable> printables =
            new ArrayList<CountingPrintable>();
        for (JEditorPane frame : frames) {
            printables.add((CountingPrintable)
                           getPrintable(frame, headerFormat, footerFormat));
        }
        return new CompoundPrintable(printables);
    } else {
        return new TextComponentPrintable(textComponent,
           headerFormat, footerFormat);
    }
}
项目:openjdk-jdk10    文件:ImageableAreaTest.java   
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(50);
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}"));

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
项目:jdk8u-jdk    文件:PSPrinterJob.java   
public EPSPrinter(Printable printable, String title,
                  PrintStream stream,
                  int x, int y, int wid, int hgt) {

    this.printable = printable;
    this.epsTitle = title;
    this.stream = stream;
    llx = x;
    lly = y;
    urx = llx+wid;
    ury = lly+hgt;
    // construct a PageFormat with zero margins representing the
    // exact bounds of the applet. ie construct a theoretical
    // paper which happens to exactly match applet panel size.
    Paper p = new Paper();
    p.setSize((double)wid, (double)hgt);
    p.setImageableArea(0.0,0.0, (double)wid, (double)hgt);
    pf = new PageFormat();
    pf.setPaper(p);
}
项目:openjdk-jdk10    文件:PrintDlgSelectionAttribTest.java   
public static void print() {

        // Set working printable to print pages
        printJob.setPrintable(new Printable() {
            public int print(Graphics graphics, PageFormat pageFormat,
                    int pageIndex) throws PrinterException {
                return NO_SUCH_PAGE;
            }
        });

        // Display Print dialog
        if (!printJob.printDialog()) {
            System.out.println("\tPrinting canceled by user");
            return;
        }

        try {
            printJob.print();
        } catch (PrinterException e) {
        }
    }
项目:cuttlefish    文件:GraphEditorDemo.java   
public int print(java.awt.Graphics graphics,
        java.awt.print.PageFormat pageFormat, int pageIndex)
        throws java.awt.print.PrinterException {
    if (pageIndex > 0) {
        return (Printable.NO_SUCH_PAGE);
    } else {
        java.awt.Graphics2D g2d = (java.awt.Graphics2D) graphics;
        vv.setDoubleBuffered(false);
        g2d.translate(pageFormat.getImageableX(), pageFormat
                .getImageableY());

        vv.paint(g2d);
        vv.setDoubleBuffered(true);

        return (Printable.PAGE_EXISTS);
    }
}
项目:jdk8u-jdk    文件:WPrinterJob.java   
/**
 * End a page.
 */
@Override
protected void endPage(PageFormat format, Printable painter,
                       int index) {

    deviceEndPage(format, painter, index);
}
项目:jdk8u-jdk    文件:ImageableAreaTest.java   
private static void printWithJavaPrintDialog() {
    final JTable table = createAuthorTable(42);
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}"));

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }
}
项目:jdk8u-jdk    文件:PrintLatinCJKTest.java   
public int print(Graphics g, PageFormat pf, int pageIndex)
                     throws PrinterException {

    if (pageIndex > 0) {
        return Printable.NO_SUCH_PAGE;
    }
    g.translate((int) pf.getImageableX(), (int) pf.getImageableY());
    g.setFont(new Font("Dialog", Font.PLAIN, 36));
    g.drawString("\u4e00\u4e01\u4e02\u4e03\u4e04English", 20, 100);
    return Printable.PAGE_EXISTS;
}
项目:openjdk-jdk10    文件:PathGraphics.java   
protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,
                       Printable painter, PageFormat pageFormat,
                       int pageIndex, boolean canRedraw) {
    super(graphics, printerJob);

    mPainter = painter;
    mPageFormat = pageFormat;
    mPageIndex = pageIndex;
    mCanRedraw = canRedraw;
}
项目:openjdk-jdk10    文件:ImagePrinter.java   
public int print(Graphics g, PageFormat pf, int index) {

        if (index > 0 || image == null) {
            return Printable.NO_SUCH_PAGE;
        }

        ((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        int iw = (int)pf.getImageableWidth();
        int ih = (int)pf.getImageableHeight();

        // ensure image will fit
        int dw = w;
        int dh = h;
        if (dw > iw) {
            dh = (int)(dh * ( (float) iw / (float) dw)) ;
            dw = iw;
        }
        if (dh > ih) {
            dw = (int)(dw * ( (float) ih / (float) dh)) ;
            dh = ih;
        }
        // centre on page
        int dx = (iw - dw) / 2;
        int dy = (ih - dh) / 2;

        g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
        return Printable.PAGE_EXISTS;
    }
项目:openjdk-jdk10    文件:ImageableAreaTest.java   
private static void printDifferentRowHeight() {
    final JTable table = createAuthorTable(50);
    table.setRowHeight(15, table.getRowHeight(15)+10);
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}"));

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog();
    if (printAccepted) {
        try {
            job.print();
            closeFrame();
        } catch (PrinterException e) {
            throw new RuntimeException(e);
        }
    }

}
项目:openjdk-jdk10    文件:TextComponentPrintable.java   
/**
 * Returns {@code TextComponentPrintable} to print {@code textComponent}.
 *
 * @param textComponent {@code JTextComponent} to print
 * @param headerFormat the page header, or {@code null} for none
 * @param footerFormat the page footer, or {@code null} for none
 * @return {@code TextComponentPrintable} to print {@code textComponent}
 */
public static Printable getPrintable(final JTextComponent textComponent,
        final MessageFormat headerFormat,
        final MessageFormat footerFormat) {

    if (textComponent instanceof JEditorPane
            && isFrameSetDocument(textComponent.getDocument())) {
        //for document with frames we create one printable per
        //frame and merge them with the CompoundPrintable.
        List<JEditorPane> frames = getFrames((JEditorPane) textComponent);
        List<CountingPrintable> printables =
            new ArrayList<CountingPrintable>();
        for (JEditorPane frame : frames) {
            printables.add((CountingPrintable)
                           getPrintable(frame, headerFormat, footerFormat));
        }
        return new CompoundPrintable(printables);
    } else {
        return new TextComponentPrintable(textComponent,
           headerFormat, footerFormat);
    }
}
项目:incubator-netbeans    文件:PageableScene.java   
/**
 * Return this class, which is the Printable object.
 * @param pageIndex the page number of the page to be printed.
 * @return this class, which is the Printable object.
 * @throws java.lang.IndexOutOfBoundsException
 */
public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
    if (pageIndex >= mNumPages) {
        throw new IndexOutOfBoundsException();
    }

    return this;
}
项目:rapidminer    文件:PrintingTools.java   
/**
 * @return <code>true</code> on success. <code>false</code> if user aborts printing.
 */
public static boolean print(Printable printable) throws PrinterException {
    getPrinterJob().setPrintable(printable);
    if (getPrinterJob().printDialog()) {
        PrintingTools.getPrinterJob().print(printSettings);
        return true;
    } else {
        return false;
    }
}
项目:openjdk-jdk10    文件:PrintLatinCJKTest.java   
public int print(Graphics g, PageFormat pf, int pageIndex)
                     throws PrinterException {

    if (pageIndex > 0) {
        return Printable.NO_SUCH_PAGE;
    }
    g.translate((int) pf.getImageableX(), (int) pf.getImageableY());
    g.setFont(new Font("Dialog", Font.PLAIN, 36));
    g.drawString("\u4e00\u4e01\u4e02\u4e03\u4e04English", 20, 100);
    return Printable.PAGE_EXISTS;
}
项目:onprom    文件:UMLDiagramPanel.java   
public void print() {
  PrinterJob printJob = PrinterJob.getPrinterJob();
  printJob.setPrintable((g, format, page) -> {
    if (page > 0) {
      return Printable.NO_SUCH_PAGE;
    }
    // get the bounds of the component
    Rectangle drawingArea = getDrawingArea();
    double cHeight = drawingArea.getSize().getHeight();
    double cWidth = drawingArea.getSize().getWidth();
    // get the bounds of the printable area
    double pHeight = format.getImageableHeight();
    double pWidth = format.getImageableWidth();
    double pXStart = format.getImageableX();
    double pYStart = format.getImageableY();
    //find ratio
    double xRatio = pWidth / cWidth;
    double yRatio = pHeight / cHeight;
    Graphics2D g2d = (Graphics2D) g;
    //translate and scale accordingly
    g2d.translate(pXStart, pYStart);
    g2d.scale(xRatio, yRatio);
    paintDrawing(g2d, drawingArea.x, drawingArea.y);
    return Printable.PAGE_EXISTS;
  });
  if (printJob.printDialog()) {
    try {
      printJob.print();
    } catch (PrinterException e) {
      UIUtility.error(e.getMessage());
    }
  }
}
项目:smile_1.5.0_java7    文件:Printer.java   
/**
 * Prints a document that implements Printable interface.
 * @param painter the Printable that renders each page of the document.
 */
public void print(Printable painter) {
    printer.setPrintable(painter);
    if (printer.printDialog(printAttributes)) {
        try {
            printer.print(printAttributes);
        } catch (PrinterException ex) {
            logger.error("Failed to print", ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
}
项目:ramus    文件:AbstractRamusPrintable.java   
@Override
public void print(GUIFramework framework) throws PrinterException {
    final PrinterJob pj = framework.getPrinterJob(getJobKey());
    final Printable printable = createPrintable();
    pj.setPrintable(printable, getPageFormat());

    if (pj.printDialog()) {
        pj.setJobName(getJobName());
        pj.print();
        setPageFormat(getPageFormat());
    }
}
项目:openjdk-jdk10    文件:WrongPaperPrintingTest.java   
@Override
public int print(Graphics g, PageFormat pf, int pageIndex)
    throws PrinterException {
    if (pageIndex == 0) {
        g.setColor(Color.RED);
        g.drawRect((int)pf.getImageableX(), (int)pf.getImageableY(),
            (int)pf.getImageableWidth(), (int)pf.getImageableHeight());
        return Printable.PAGE_EXISTS;
    } else {
        return Printable.NO_SUCH_PAGE;
    }
}
项目:openjdk-jdk10    文件:UnixPrintJob.java   
public void printableJob(Printable printable) throws PrintException {
    try {
        synchronized(this) {
            if (job != null) { // shouldn't happen
                throw new PrintException("already printing");
            } else {
                job = new PSPrinterJob();
            }
        }
        job.setPrintService(getPrintService());
        job.setCopies(copies);
        job.setJobName(jobName);
        PageFormat pf = new PageFormat();
        if (mediaSize != null) {
            Paper p = new Paper();
            p.setSize(mediaSize.getX(MediaSize.INCH)*72.0,
                      mediaSize.getY(MediaSize.INCH)*72.0);
            p.setImageableArea(72.0, 72.0, p.getWidth()-144.0,
                               p.getHeight()-144.0);
            pf.setPaper(p);
        }
        if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
            pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
        } else if (orient == OrientationRequested.LANDSCAPE) {
            pf.setOrientation(PageFormat.LANDSCAPE);
        }
        job.setPrintable(printable, pf);
        job.print(reqAttrSet);
        notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
        return;
    } catch (PrinterException pe) {
        notifyEvent(PrintJobEvent.JOB_FAILED);
        throw new PrintException(pe);
    } finally {
        printReturned = true;
        notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    }
}
项目:openjdk-jdk10    文件:ImageableAreaTest.java   
private static void printWithCustomImageareaSize() {
    final JTable table = createAuthorTable(18);
    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    printAttributes.add(DialogTypeSelection.NATIVE);
    printAttributes.add(new Copies(1));
    printAttributes.add(new MediaPrintableArea(
            0.25f, 0.25f, 8.0f, 5.0f, MediaPrintableArea.INCH));
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}")
    );

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog(printAttributes);
    if (printAccepted) {
        try {
            job.print(printAttributes);
            closeFrame();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("User cancels the printer job!");
    }
}
项目:OpenJSharp    文件:PSPrinterJob.java   
/**
 * Examine the metrics captured by the
 * <code>PeekGraphics</code> instance and
 * if capable of directly converting this
 * print job to the printer's control language
 * or the native OS's graphics primitives, then
 * return a <code>PSPathGraphics</code> to perform
 * that conversion. If there is not an object
 * capable of the conversion then return
 * <code>null</code>. Returning <code>null</code>
 * causes the print job to be rasterized.
 */

protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
                                        PrinterJob printerJob,
                                        Printable painter,
                                        PageFormat pageFormat,
                                        int pageIndex) {

    PSPathGraphics pathGraphics;
    PeekMetrics metrics = peekGraphics.getMetrics();

    /* If the application has drawn anything that
     * out PathGraphics class can not handle then
     * return a null PathGraphics.
     */
    if (forcePDL == false && (forceRaster == true
                    || metrics.hasNonSolidColors()
                    || metrics.hasCompositing())) {

        pathGraphics = null;
    } else {

        BufferedImage bufferedImage = new BufferedImage(8, 8,
                                        BufferedImage.TYPE_INT_RGB);
        Graphics2D bufferedGraphics = bufferedImage.createGraphics();
        boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;

        pathGraphics =  new PSPathGraphics(bufferedGraphics, printerJob,
                                           painter, pageFormat, pageIndex,
                                           canRedraw);
    }

    return pathGraphics;
}
项目:OpenJSharp    文件:PSPrinterJob.java   
public int print(Graphics g, PageFormat pf, int pgIndex) {
    if (pgIndex > 0) {
        return Printable.NO_SUCH_PAGE;
    } else {
        // "aware" client code can detect that its been passed a
        // PrinterGraphics and could theoretically print
        // differently. I think this is more likely useful than
        // a problem.
        applet.printAll(g);
        return Printable.PAGE_EXISTS;
    }
}
项目:OpenJSharp    文件:PSPrinterJob.java   
public Printable getPrintable(int pgIndex) {
    if (pgIndex > 0) {
        throw new IndexOutOfBoundsException("pgIndex");
    } else {
    return printable;
    }
}
项目:OpenJSharp    文件:WPrinterJob.java   
/**
 * Examine the metrics captured by the
 * <code>PeekGraphics</code> instance and
 * if capable of directly converting this
 * print job to the printer's control language
 * or the native OS's graphics primitives, then
 * return a <code>PathGraphics</code> to perform
 * that conversion. If there is not an object
 * capable of the conversion then return
 * <code>null</code>. Returning <code>null</code>
 * causes the print job to be rasterized.
 */

@Override
protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
                                        PrinterJob printerJob,
                                        Printable painter,
                                        PageFormat pageFormat,
                                        int pageIndex) {

    WPathGraphics pathGraphics;
    PeekMetrics metrics = peekGraphics.getMetrics();

    /* If the application has drawn anything that
     * out PathGraphics class can not handle then
     * return a null PathGraphics. If the property
     * to force the raster pipeline has been set then
     * we also want to avoid the path (pdl) pipeline
     * and return null.
     */
   if (forcePDL == false && (forceRaster == true
                              || metrics.hasNonSolidColors()
                              || metrics.hasCompositing()
                              )) {
        pathGraphics = null;
    } else {
        BufferedImage bufferedImage = new BufferedImage(8, 8,
                                        BufferedImage.TYPE_INT_RGB);
        Graphics2D bufferedGraphics = bufferedImage.createGraphics();

        boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;
        pathGraphics =  new WPathGraphics(bufferedGraphics, printerJob,
                                          painter, pageFormat, pageIndex,
                                          canRedraw);
    }

    return pathGraphics;
}
项目:OpenJSharp    文件:WPrinterJob.java   
/**
 * Begin a new page.
 */
@Override
protected void startPage(PageFormat format, Printable painter,
                         int index, boolean paperChanged) {

    /* Invalidate any device state caches we are
     * maintaining. Win95/98 resets the device
     * context attributes to default values at
     * the start of each page.
     */
    invalidateCachedState();

    deviceStartPage(format, painter, index, paperChanged);
}
项目:OpenJSharp    文件:WPrinterJob.java   
/**
 * End a page.
 */
@Override
protected void endPage(PageFormat format, Printable painter,
                       int index) {

    deviceEndPage(format, painter, index);
}
项目:openjdk-jdk10    文件:WPrinterJob.java   
/**
 * Examine the metrics captured by the
 * {@code PeekGraphics} instance and
 * if capable of directly converting this
 * print job to the printer's control language
 * or the native OS's graphics primitives, then
 * return a {@code PathGraphics} to perform
 * that conversion. If there is not an object
 * capable of the conversion then return
 * {@code null}. Returning {@code null}
 * causes the print job to be rasterized.
 */

@Override
protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
                                        PrinterJob printerJob,
                                        Printable painter,
                                        PageFormat pageFormat,
                                        int pageIndex) {

    WPathGraphics pathGraphics;
    PeekMetrics metrics = peekGraphics.getMetrics();

    /* If the application has drawn anything that
     * out PathGraphics class can not handle then
     * return a null PathGraphics. If the property
     * to force the raster pipeline has been set then
     * we also want to avoid the path (pdl) pipeline
     * and return null.
     */
   if (forcePDL == false && (forceRaster == true
                              || metrics.hasNonSolidColors()
                              || metrics.hasCompositing()
                              )) {
        pathGraphics = null;
    } else {
        BufferedImage bufferedImage = new BufferedImage(8, 8,
                                        BufferedImage.TYPE_INT_RGB);
        Graphics2D bufferedGraphics = bufferedImage.createGraphics();

        boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;
        pathGraphics =  new WPathGraphics(bufferedGraphics, printerJob,
                                          painter, pageFormat, pageIndex,
                                          canRedraw);
    }

    return pathGraphics;
}
项目:jdk8u-jdk    文件:PathGraphics.java   
protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,
                       Printable painter, PageFormat pageFormat,
                       int pageIndex, boolean canRedraw) {
    super(graphics, printerJob);

    mPainter = painter;
    mPageFormat = pageFormat;
    mPageIndex = pageIndex;
    mCanRedraw = canRedraw;
}
项目:jdk8u-jdk    文件:ImagePrinter.java   
public int print(Graphics g, PageFormat pf, int index) {

        if (index > 0 || image == null) {
            return Printable.NO_SUCH_PAGE;
        }

        ((Graphics2D)g).translate(pf.getImageableX(), pf.getImageableY());
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        int iw = (int)pf.getImageableWidth();
        int ih = (int)pf.getImageableHeight();

        // ensure image will fit
        int dw = w;
        int dh = h;
        if (dw > iw) {
            dh = (int)(dh * ( (float) iw / (float) dw)) ;
            dw = iw;
        }
        if (dh > ih) {
            dw = (int)(dw * ( (float) ih / (float) dh)) ;
            dh = ih;
        }
        // centre on page
        int dx = (iw - dw) / 2;
        int dy = (ih - dh) / 2;

        g.drawImage(image, dx, dy, dx+dw, dy+dh, 0, 0, w, h, null);
        return Printable.PAGE_EXISTS;
    }
项目:jdk8u-jdk    文件:PSPrinterJob.java   
/**
 * The RastePrintJob super class calls this method
 * at the end of each page.
 */
protected void endPage(PageFormat format, Printable painter,
                       int index)
    throws PrinterException
{
    mPSStream.println(PAGE_RESTORE);
    mPSStream.println(SHOWPAGE);
}
项目:jdk8u-jdk    文件:PSPrinterJob.java   
/**
 * Examine the metrics captured by the
 * <code>PeekGraphics</code> instance and
 * if capable of directly converting this
 * print job to the printer's control language
 * or the native OS's graphics primitives, then
 * return a <code>PSPathGraphics</code> to perform
 * that conversion. If there is not an object
 * capable of the conversion then return
 * <code>null</code>. Returning <code>null</code>
 * causes the print job to be rasterized.
 */

protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
                                        PrinterJob printerJob,
                                        Printable painter,
                                        PageFormat pageFormat,
                                        int pageIndex) {

    PSPathGraphics pathGraphics;
    PeekMetrics metrics = peekGraphics.getMetrics();

    /* If the application has drawn anything that
     * out PathGraphics class can not handle then
     * return a null PathGraphics.
     */
    if (forcePDL == false && (forceRaster == true
                    || metrics.hasNonSolidColors()
                    || metrics.hasCompositing())) {

        pathGraphics = null;
    } else {

        BufferedImage bufferedImage = new BufferedImage(8, 8,
                                        BufferedImage.TYPE_INT_RGB);
        Graphics2D bufferedGraphics = bufferedImage.createGraphics();
        boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;

        pathGraphics =  new PSPathGraphics(bufferedGraphics, printerJob,
                                           painter, pageFormat, pageIndex,
                                           canRedraw);
    }

    return pathGraphics;
}
项目:jdk8u-jdk    文件:PSPrinterJob.java   
public int print(Graphics g, PageFormat pf, int pgIndex) {
    if (pgIndex > 0) {
        return Printable.NO_SUCH_PAGE;
    } else {
        // "aware" client code can detect that its been passed a
        // PrinterGraphics and could theoretically print
        // differently. I think this is more likely useful than
        // a problem.
        applet.printAll(g);
        return Printable.PAGE_EXISTS;
    }
}
项目:jdk8u-jdk    文件:PSPrinterJob.java   
public Printable getPrintable(int pgIndex) {
    if (pgIndex > 0) {
        throw new IndexOutOfBoundsException("pgIndex");
    } else {
    return printable;
    }
}
项目:openjdk-jdk10    文件:bug8023392.java   
public int print(Graphics graphics,
                 PageFormat pageFormat,
                 int pageIndex)
        throws PrinterException {
    if (pageIndex >= 1) {
        return Printable.NO_SUCH_PAGE;
    }

    this.paint(graphics);
    return Printable.PAGE_EXISTS;
}
项目:jdk8u-jdk    文件:WPrinterJob.java   
/**
 * Examine the metrics captured by the
 * <code>PeekGraphics</code> instance and
 * if capable of directly converting this
 * print job to the printer's control language
 * or the native OS's graphics primitives, then
 * return a <code>PathGraphics</code> to perform
 * that conversion. If there is not an object
 * capable of the conversion then return
 * <code>null</code>. Returning <code>null</code>
 * causes the print job to be rasterized.
 */

@Override
protected Graphics2D createPathGraphics(PeekGraphics peekGraphics,
                                        PrinterJob printerJob,
                                        Printable painter,
                                        PageFormat pageFormat,
                                        int pageIndex) {

    WPathGraphics pathGraphics;
    PeekMetrics metrics = peekGraphics.getMetrics();

    /* If the application has drawn anything that
     * out PathGraphics class can not handle then
     * return a null PathGraphics. If the property
     * to force the raster pipeline has been set then
     * we also want to avoid the path (pdl) pipeline
     * and return null.
     */
   if (forcePDL == false && (forceRaster == true
                              || metrics.hasNonSolidColors()
                              || metrics.hasCompositing()
                              )) {
        pathGraphics = null;
    } else {
        BufferedImage bufferedImage = new BufferedImage(8, 8,
                                        BufferedImage.TYPE_INT_RGB);
        Graphics2D bufferedGraphics = bufferedImage.createGraphics();

        boolean canRedraw = peekGraphics.getAWTDrawingOnly() == false;
        pathGraphics =  new WPathGraphics(bufferedGraphics, printerJob,
                                          painter, pageFormat, pageIndex,
                                          canRedraw);
    }

    return pathGraphics;
}
项目:jdk8u-jdk    文件:ImageableAreaTest.java   
private static void printWithCustomImageareaSize() {
    final JTable table = createAuthorTable(18);
    PrintRequestAttributeSet printAttributes = new HashPrintRequestAttributeSet();
    printAttributes.add(DialogTypeSelection.NATIVE);
    printAttributes.add(new Copies(1));
    printAttributes.add(new MediaPrintableArea(
            0.25f, 0.25f, 8.0f, 5.0f, MediaPrintableArea.INCH));
    Printable printable = table.getPrintable(
            JTable.PrintMode.NORMAL,
            new MessageFormat("Author Table"),
            new MessageFormat("Page - {0}")
    );

    PrinterJob job = PrinterJob.getPrinterJob();
    job.setPrintable(printable);

    boolean printAccepted = job.printDialog(printAttributes);
    if (printAccepted) {
        try {
            job.print(printAttributes);
            closeFrame();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } else {
        throw new RuntimeException("User cancels the printer job!");
    }
}