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

项目:incubator-netbeans    文件:PrintPreferences.java   
/**
 * Get an instance of {@link java.awt.print.PageFormat}.
 * @param pj {@link java.awt.print.PrinterJob} which is 
 * associated with the default printer.
 * @return an instance of <code>PageFormat</code> that describes the size and
 * orientation of a page to be printed.
 */
public static PageFormat getPageFormat(PrinterJob pj) {
    PageFormat pageFormat = null;
    pageFormat = pj.defaultPage();
    Paper p = pageFormat.getPaper();
    int pageOrientation = getPreferences().getInt(PROP_PAGE_ORIENTATION, pageFormat.getOrientation());
    double paperWidth = getPreferences().getDouble(PROP_PAGE_WIDTH, p.getWidth());
    double paperHeight = getPreferences().getDouble(PROP_PAGE_HEIGHT, p.getHeight());

    double iaWidth = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, p.getImageableWidth());
    double iaHeight = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, p.getImageableHeight());
    double iaX = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_X, p.getImageableX());
    double iaY = getPreferences().getDouble(PROP_PAGE_IMAGEABLEAREA_Y, p.getImageableY());

    pageFormat.setOrientation(pageOrientation);
    p.setSize(paperWidth, paperHeight);
    p.setImageableArea(iaX, iaY, iaWidth, iaHeight);
    pageFormat.setPaper(p);
    return pageFormat;
}
项目:Tarski    文件:EditorActions.java   
/**
 * 
 */
public void actionPerformed(ActionEvent e) {
  if (e.getSource() instanceof mxGraphComponent) {
    mxGraphComponent graphComponent = (mxGraphComponent) e.getSource();
    PrinterJob pj = PrinterJob.getPrinterJob();

    if (pj.printDialog()) {
      PageFormat pf = graphComponent.getPageFormat();
      Paper paper = new Paper();
      double margin = 36;
      paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2,
          paper.getHeight() - margin * 2);
      pf.setPaper(paper);
      pj.setPrintable(graphComponent, pf);

      try {
        pj.print();
      } catch (PrinterException e2) {
        System.out.println(e2);
      }
    }
  }
}
项目:incubator-netbeans    文件:PageableScene.java   
/**
 * Set the print size to fit a page in the verticle direction.
 * The horizontal is scaled equally but no garuntees are made on the page fit.
 */
private void scaleToFitY() {
    PageFormat format = getPageFormat();
    Rectangle componentBounds = scene.getBounds();

    if (componentBounds.height == 0) {
        return;
    }

    double scaleY = format.getImageableHeight() / componentBounds.height;
    double scaleX = scaleY;
    if (scaleY < 1) {
        setSize((float) (componentBounds.width * scaleX), (float) format.getImageableHeight());
        setScaledSize(scaleX, scaleY);
    }
}
项目:incubator-netbeans    文件:Config.java   
public PageFormat getPageFormat() {
    PrinterJob job = PrinterJob.getPrinterJob();

    if (myPageFormat == null) {
        myPageFormat = job.defaultPage();

        // restore
        myPageFormat.setOrientation(round(get(PAGE_ORIENTATION, PageFormat.PORTRAIT)));
        Paper paper = myPageFormat.getPaper();

        if (get(PAPER_WIDTH, null) != null && get(PAPER_HEIGHT, null) != null) {
            paper.setSize(get(PAPER_WIDTH, INCH), get(PAPER_HEIGHT, INCH));
        }
        if (get(AREA_X, null) != null && get(AREA_Y, null) != null && get(AREA_WIDTH, null) != null && get(AREA_HEIGHT, null) != null) {
            paper.setImageableArea(get(AREA_X, INCH), get(AREA_Y, INCH), get(AREA_WIDTH, INCH), get(AREA_HEIGHT, INCH));
        }
        myPageFormat.setPaper(paper);
    }
    return myPageFormat;
}
项目:rapidminer    文件:PrintPreviewPanel.java   
/**
 * Creates a preview panel for the specified {@link PrintableComponent}.
 *
 * @param comp
 *            the {@link PrintableComponent} the preview panel should be created for.
 */
public PrintPreviewPanel(PrintableComponent comp, PageFormat pageFormat) {
    this.printer = new ComponentPrinter(comp);
    this.cardLayout = new CardLayout();
    this.pageFormat = pageFormat;
    setLayout(cardLayout);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.CENTER;

    landscapePanel = new JPanel(new GridBagLayout());
    landscapePreview = new ComponentPreviewPanel(Orientation.LANDSCAPE);
    landscapePanel.add(landscapePreview, gbc);
    add(landscapePanel, Orientation.LANDSCAPE.toString());

    portraitPanel = new JPanel(new GridBagLayout());
    portraitPreview = new ComponentPreviewPanel(Orientation.PORTRAIT);
    portraitPanel.add(portraitPreview, gbc);
    add(portraitPanel, Orientation.PORTRAIT.toString());

    // set page format
    setPageFormat(pageFormat);
}
项目:rapidminer    文件:PrintPreviewPanel.java   
public void setPageFormat(PageFormat pageFormat) {
    this.pageFormat = pageFormat;
    remove(landscapePanel);
    remove(portraitPanel);
    add(landscapePanel, Orientation.LANDSCAPE.toString());
    add(portraitPanel, Orientation.PORTRAIT.toString());
    if (pageFormat.getOrientation() == PageFormat.LANDSCAPE) {
        cardLayout.show(this, Orientation.LANDSCAPE.toString());
    } else {
        cardLayout.show(this, Orientation.PORTRAIT.toString());
    }
    repaint();
}
项目:jdk8u-jdk    文件:PrintingStatus.java   
public int print(final Graphics graphics,
                 final PageFormat pageFormat, final int pageIndex)
        throws PrinterException {

    final int retVal =
        printDelegatee.print(graphics, pageFormat, pageIndex);
    if (retVal != NO_SUCH_PAGE && !isAborted()) {
        if (SwingUtilities.isEventDispatchThread()) {
            updateStatusOnEDT(pageIndex);
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    updateStatusOnEDT(pageIndex);
                }
            });
        }
    }
    return retVal;
}
项目: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;
}
项目:parabuild-ci    文件:ChartPanel.java   
/**
 * Prints the chart on a single page.
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing 
 *                   gets print.
 *
 * @return The result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor, 
            null);
    return PAGE_EXISTS;

}
项目:openjdk-jdk10    文件:TestUnsupportedResolution.java   
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException
{
    if (pageIndex>0)
    {
        return NO_SUCH_PAGE;
    }

    StringBuffer s=new StringBuffer();
    for (int i=0;i<10;i++)
    {
        s.append("1234567890ABCDEFGHIJ");
    }

    int x=(int) pageFormat.getImageableX();
    int y=(int) (pageFormat.getImageableY()+50);
    graphics.drawString(s.toString(), x, y);

    return PAGE_EXISTS;
}
项目:openjdk-jdk10    文件:RasterPrinterJob.java   
protected MediaPrintableArea getDefaultPrintableArea(PageFormat page,
        double w, double h) {
    double ix, iw, iy, ih;
    if (w >= 72.0 * 6.0) {
        ix = 72.0;
        iw = w - 2 * 72.0;
    } else {
        ix = w / 6.0;
        iw = w * 0.75;
    }
    if (h >= 72.0 * 6.0) {
        iy = 72.0;
        ih = h - 2 * 72.0;
    } else {
        iy = h / 6.0;
        ih = h * 0.75;
    }

    return new MediaPrintableArea((float) (ix / DPI), (float) (iy / DPI),
            (float) (iw / DPI), (float) (ih / DPI), MediaPrintableArea.INCH);
}
项目:Tarski    文件:EditorActions.java   
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() instanceof mxGraphComponent)
    {
        mxGraphComponent graphComponent = (mxGraphComponent) e
                .getSource();
        PrinterJob pj = PrinterJob.getPrinterJob();
        PageFormat format = pj.pageDialog(graphComponent
                .getPageFormat());

        if (format != null)
        {
            graphComponent.setPageFormat(format);
            graphComponent.zoomAndCenter();
        }
    }
}
项目:jdk8u-jdk    文件:RasterPrinterJob.java   
protected MediaPrintableArea getDefaultPrintableArea(PageFormat page,
        double w, double h) {
    double ix, iw, iy, ih;
        if (w >= 72.0 * 6.0) {
            ix = 72.0;
            iw = w - 2 * 72.0;
        } else {
            ix = w / 6.0;
            iw = w * 0.75;
        }
        if (h >= 72.0 * 6.0) {
            iy = 72.0;
            ih = h - 2 * 72.0;
        } else {
            iy = h / 6.0;
            ih = h * 0.75;
        }

    return new MediaPrintableArea((float) (ix / DPI), (float) (iy / DPI),
            (float) (iw / DPI), (float) (ih / DPI), MediaPrintableArea.INCH);
}
项目:openjdk-jdk10    文件:PrintTestLexmarkIQ.java   
private static void printTest() {
    PrinterJob pj = PrinterJob.getPrinterJob();

    PageFormat pf = pj.defaultPage();
    Paper paper = new Paper();
    double margin = 36; // half inch
    paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2,
            paper.getHeight() - margin * 2);
    pf.setPaper(paper);

    pj.setPrintable(new PrintTestLexmarkIQ(), pf);
    if (pj.printDialog()) {
        try {
            pj.print();
        } catch (PrinterException e) {
            System.out.println(e);
        }
    }
}
项目:incubator-netbeans    文件:PageableScene.java   
/**
 * Adjusts the scaling factors in both the horizontal and vertical directions
 * to garuntee that the Scene prints onto a single page.
 * @param useSymmetricScaling if true, the horizontal and vertical scaling
 * factors will be the same whereby preserving the current aspect ration. The
 * smallest of the two (horizontal and vertical) scaling factors is used for 
 * both.
 */
private void scaleToFit(boolean useSymmetricScaling) {
    PageFormat format = getPageFormat();

    Rectangle componentBounds = scene.getView().getBounds();

    if (componentBounds.width * componentBounds.height == 0) {
        return;
    }
    double scaleX = format.getImageableWidth() / componentBounds.width;
    double scaleY = format.getImageableHeight() / componentBounds.height;

    if (scaleX < 1 || scaleY < 1) {

        if (useSymmetricScaling) {
            if (scaleX < scaleY) {
                scaleY = scaleX;
            } else {
                scaleX = scaleY;
            }
        }

        setSize((float) (componentBounds.width * scaleX), (float) (componentBounds.height * scaleY));
        setScaledSize(scaleX, scaleY);

    }
}
项目:parabuild-ci    文件:ChartPanel.java   
/**
 * Prints the chart on a single page.
 *
 * @param g  the graphics context.
 * @param pf  the page format to use.
 * @param pageIndex  the index of the page. If not <code>0</code>, nothing gets print.
 *
 * @return the result of printing.
 */
public int print(Graphics g, PageFormat pf, int pageIndex) {

    if (pageIndex != 0) {
        return NO_SUCH_PAGE;
    }
    Graphics2D g2 = (Graphics2D) g;
    double x = pf.getImageableX();
    double y = pf.getImageableY();
    double w = pf.getImageableWidth();
    double h = pf.getImageableHeight();
    this.chart.draw(g2, new Rectangle2D.Double(x, y, w, h), this.anchor, null);
    return PAGE_EXISTS;

}
项目:jdk8u-jdk    文件:RasterPrinterJob.java   
protected void updatePageAttributes(PrintService service,
                                    PageFormat page) {
    if (this.attributes == null) {
        this.attributes = new HashPrintRequestAttributeSet();
    }

    updateAttributesWithPageFormat(service, page, this.attributes);
}
项目:incubator-netbeans    文件:PrintPreferences.java   
/**
 * @param pf <code>PageFormat</code> that describes the size and
 * orientation of a page to be printed
 */
public static void setPageFormat(PageFormat pf) {
    getPreferences().putInt(PROP_PAGE_ORIENTATION, pf.getOrientation());
    getPreferences().putDouble(PROP_PAGE_WIDTH, pf.getPaper().getWidth());
    getPreferences().putDouble(PROP_PAGE_HEIGHT, pf.getPaper().getHeight());

    getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_WIDTH, pf.getPaper().getImageableWidth());
    getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_HEIGHT, pf.getPaper().getImageableHeight());
    getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_X, pf.getPaper().getImageableX());
    getPreferences().putDouble(PROP_PAGE_IMAGEABLEAREA_Y, pf.getPaper().getImageableY());
}
项目: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;
}
项目:incubator-netbeans    文件:PrintSettings.java   
/**
* @return <tt>null</tt> Shows pageDialog, however.
*/
public java.awt.Component getCustomEditor() {
    PageFormat pf = (PageFormat) getValue();
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat npf = pj.pageDialog(pf);

    //setValue(npf);
    ((PrintSettings)PrintSettings.findObject(PrintSettings.class)).setPageFormat((PageFormat) npf.clone());
    pj.cancel();

    return null;
}
项目:openjdk-jdk10    文件: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);
}
项目:geomapapp    文件:XYGraph.java   
public int print(Graphics g, PageFormat fmt, int pageNo) {
    printing = true;
    if( pageNo>1 ) {
        printing = false;
        return NO_SUCH_PAGE;
    }
    Rectangle r = getVisibleRect();
    double w = fmt.getImageableWidth();
    double h = fmt.getImageableHeight();
    double x = fmt.getImageableX();
    double y = fmt.getImageableY();
    Insets ins = axes.getInsets();
    double ww = w - ins.left - ins.right;
    double hh = h - ins.top - ins.bottom;
    double rw = (double)(r.width - ins.left - ins.right);
    double rh = (double)(r.height - ins.top - ins.bottom);
    double scale = Math.min( rh/rw, rw/rh);
    double newH = ww * scale;
    double newW = hh * scale;
    if( !tracksWidth || !tracksHeight ) {
        int dpi = Toolkit.getDefaultToolkit().getScreenResolution();
        scale = 72./dpi;
    }
    newW = rw*scale;
    newH = rh*scale;
    x -= (newW-ww)/2;
    y -= (newH-hh)/2;
    w = newW + ins.left + ins.right;
    h = newH +ins.top + ins.bottom;
    printRect = new Rectangle( (int)x,
                (int)y,
                (int)w,
                (int)h );
    paintComponent(g);
    printing = false;
    return PAGE_EXISTS;
}
项目:smile_1.5.0_java7    文件:PlotCanvas.java   
@Override
public int print(java.awt.Graphics g, PageFormat pf, int page) throws PrinterException {
    if (page > 0) {
        // We have only one page, and 'page' is zero-based
        return NO_SUCH_PAGE;
    }

    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
    g2d.translate(pf.getImageableX(), pf.getImageableY());

    // Scale plots to paper size.
    double scaleX = pf.getImageableWidth() / canvas.getWidth();
    double scaleY = pf.getImageableHeight() / canvas.getHeight();
    g2d.scale(scaleX, scaleY);

    // Disable double buffering
    canvas.setDoubleBuffered(false);

    // Now we perform our rendering
    canvas.print(g);

    // Enable double buffering
    canvas.setDoubleBuffered(true);

    // tell the caller that this page is part of the printed document
    return PAGE_EXISTS;
}
项目:JDigitalSimulator    文件:Simulation.java   
@Override public int print(Graphics default_graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
    List<Image> images = getPrintPages(pageFormat);
    if(pageIndex>=images.size())
        return NO_SUCH_PAGE;
    Graphics2D graphics = (Graphics2D)default_graphics.create();
    graphics.translate(pageFormat.getImageableX(),
            pageFormat.getImageableY());
    graphics.drawImage(images.get(pageIndex), 0, 0, null);
    Thread.yield(); //yield shortly, so that the image is painted
    return PAGE_EXISTS;
}
项目:Tarski    文件:EditorActions.java   
/**
 * 
 */
public void actionPerformed(ActionEvent e)
{
    if (e.getSource() instanceof mxGraphComponent)
    {
        mxGraphComponent graphComponent = (mxGraphComponent) e
                .getSource();
        PrinterJob pj = PrinterJob.getPrinterJob();

        if (pj.printDialog())
        {
            PageFormat pf = graphComponent.getPageFormat();
            Paper paper = new Paper();
            double margin = 36;
            paper.setImageableArea(margin, margin, paper.getWidth()
                    - margin * 2, paper.getHeight() - margin * 2);
            pf.setPaper(paper);
            pj.setPrintable(graphComponent, pf);

            try
            {
                pj.print();
            }
            catch (PrinterException e2)
            {
                System.out.println(e2);
            }
        }
    }
}
项目:JDigitalSimulator    文件:Application.java   
@Override public int print(Graphics default_graphics, PageFormat pageFormat, int pageNumber) throws PrinterException {
    if(simulation.print(default_graphics, pageFormat, pageNumber)!=NO_SUCH_PAGE) {
        Graphics2D graphics = (Graphics2D) default_graphics.create((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY(), (int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight());
        graphics.setColor(Color.BLACK); graphics.setFont(default_graphics.getFont().deriveFont(Font.BOLD, 14));
        String title = getFileName();   FontMetrics metrics = default_graphics.getFontMetrics();
        graphics.drawString(title, (int)(pageFormat.getImageableWidth()/2-metrics.stringWidth(title)/2),
                (metrics.getAscent()));
        return PAGE_EXISTS;
    } else return NO_SUCH_PAGE;
}
项目:ramus    文件:PrintPreviewComponent.java   
public Dimension2D getPageSize() {
    int pageCount = printable.getPageCount();
    double pageWidth = 0;
    double pageHeight = 0;
    PageFormat pageFormat = new PageFormat();
    for (int i = 0; i < pageCount; i++) {
        pageFormat = printable.getPageFormat(pageFormat, i);
        double w = pageFormat.getWidth();
        double h = pageFormat.getHeight();

        if (pageWidth < w)
            pageWidth = w;
        if (pageHeight < h)
            pageHeight = h;
    }

    final double fw = pageWidth;
    final double fh = pageHeight;
    return new Dimension2D() {

        @Override
        public void setSize(double width, double height) {
        }

        @Override
        public double getWidth() {
            return fw;
        }

        @Override
        public double getHeight() {
            return fh;
        }
    };
}
项目:ramus    文件:HTMLPrintable.java   
@Override
public PageFormat getPageFormat() {
    if (pageFormat == null) {
        pageFormat = Options.getPageFormat("html-page-format.xml",
                new PageFormat());
    }
    return pageFormat;
}
项目:openjdk-jdk10    文件:TestMediaTraySelection.java   
@Override
public int print(Graphics g, PageFormat pf, int pi) {
    System.out.println("pi = " + pi);
    if (pi > 0) {
        return NO_SUCH_PAGE;
    }
    g.drawString("Testing : " , 200, 200);
    return PAGE_EXISTS;
}
项目:ramus    文件:IDEF0Printable.java   
@Override
public void setPageFormat(PageFormat pageFormat) {
    super.setPageFormat(pageFormat);
    if (!staticPageFormat.equals(pageFormat)) {
        Arrays.fill(painters, null);
        staticPageFormat = pageFormat;
        Options.setPageFormat("idef0-page-format", staticPageFormat);
    }
}
项目:openjdk-jdk10    文件:PageDlgPrnButton.java   
public void pageDialogExample() throws PrinterException
{
    PrinterJob job = PrinterJob.getPrinterJob();
    PageFormat originalPageFormat = job.defaultPage();
    PageFormat pageFormat = job.pageDialog(originalPageFormat);

    if(originalPageFormat == pageFormat) return;

    job.setPrintable(this,pageFormat);
    job.print();
}
项目:ramus    文件:ChartSetPrintable.java   
@Override
public PageFormat getPageFormat() {
    if (pageFormat == null) {
        pageFormat = Options.getPageFormat("chart-set-page-format.xml",
                new PageFormat());
    }
    return pageFormat;
}
项目:OpenJSharp    文件: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    文件: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;
}
项目: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;
    }
项目:jdk8u-jdk    文件:PrintJob2D.java   
/**
 * Prints the page at the specified index into the specified
 * {@link Graphics} context in the specified
 * format.  A <code>PrinterJob</code> calls the
 * <code>Printable</code> interface to request that a page be
 * rendered into the context specified by
 * <code>graphics</code>.  The format of the page to be drawn is
 * specified by <code>pageFormat</code>.  The zero based index
 * of the requested page is specified by <code>pageIndex</code>.
 * If the requested page does not exist then this method returns
 * NO_SUCH_PAGE; otherwise PAGE_EXISTS is returned.
 * The <code>Graphics</code> class or subclass implements the
 * {@link PrinterGraphics} interface to provide additional
 * information.  If the <code>Printable</code> object
 * aborts the print job then it throws a {@link PrinterException}.
 * @param graphics the context into which the page is drawn
 * @param pageFormat the size and orientation of the page being drawn
 * @param pageIndex the zero based index of the page to be drawn
 * @return PAGE_EXISTS if the page is rendered successfully
 *         or NO_SUCH_PAGE if <code>pageIndex</code> specifies a
 *         non-existent page.
 * @exception java.awt.print.PrinterException
 *         thrown when the print job is terminated.
 */
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
             throws PrinterException {

    int result;

    /* This method will be called by the PrinterJob on a thread other
     * that the application's thread. We hold on to the graphics
     * until we can rendevous with the application's thread and
     * hand over the graphics. The application then does all the
     * drawing. When the application is done drawing we rendevous
     * again with the PrinterJob thread and release the Graphics
     * so that it knows we are done.
     */

    /* Add the graphics to the message queue of graphics to
     * be rendered. This is really a one slot queue. The
     * application's thread will come along and remove the
     * graphics from the queue when the app asks for a graphics.
     */
    graphicsToBeDrawn.append( (Graphics2D) graphics);

    /* We now wait for the app's thread to finish drawing on
     * the Graphics. This thread will sleep until the application
     * release the graphics by placing it in the graphics drawn
     * message queue. If the application signals that it is
     * finished drawing the entire document then we'll get null
     * returned when we try and pop a finished graphic.
     */
    if (graphicsDrawn.pop() != null) {
        result = PAGE_EXISTS;
    } else {
        result = NO_SUCH_PAGE;
    }

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

    deviceEndPage(format, painter, index);
}
项目: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    文件:RasterPrinterJob.java   
protected void updatePageAttributes(PrintService service,
                                    PageFormat page) {
    if (this.attributes == null) {
        this.attributes = new HashPrintRequestAttributeSet();
    }

    updateAttributesWithPageFormat(service, page, this.attributes);
}