Java Code Examples for java.awt.print.Printable#NO_SUCH_PAGE

The following examples show how to use java.awt.print.Printable#NO_SUCH_PAGE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlotBox.java    From opt4j with MIT License 6 votes vote down vote up
/**
 * 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.
 */
@Override
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() / getWidth();
	double scaley = format.getImageableHeight() / 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;
}
 
Example 2
Source File: TextHistoryPane.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {
  if (pi == 0)
    token = new StringTokenizer(ta.getText(), "\r\n");

  if (!token.hasMoreTokens())
    return Printable.NO_SUCH_PAGE;

  Graphics2D g2d = (Graphics2D) g;
  g2d.setPaint(Color.black);
  g2d.setFont(newFont);

  double xbeg = pf.getImageableX();
  double ywidth = pf.getImageableHeight() + pf.getImageableY();
  double y = pf.getImageableY() + incrY;
  while (token.hasMoreTokens() && (y < ywidth)) {
    String toke = token.nextToken();
    g2d.drawString(toke, (int) xbeg, (int) y);
    y += incrY;
  }
  return Printable.PAGE_EXISTS;
}
 
Example 3
Source File: ImagePrinter.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
 
Example 4
Source File: bug8023392.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: DummyPrintTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
        throws PrinterException {
    if (pageIndex == 0) {
        return Printable.PAGE_EXISTS;
    } else {
        return Printable.NO_SUCH_PAGE;
    }
}
 
Example 6
Source File: PSPrinterJob.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 7
Source File: ImagePrintable.java    From webapp-hardware-bridge with MIT License 5 votes vote down vote up
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
    if (pageIndex >= 1) {
        return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2d = (Graphics2D) graphics;
    g2d.translate((int) pageFormat.getImageableX(), (int) pageFormat.getImageableY());

    Double width = pageFormat.getImageableWidth();
    Double height = pageFormat.getImageableHeight();

    g2d.drawImage(image, 0, 0, width.intValue(), height.intValue(), null, null);

    return Printable.PAGE_EXISTS;
}
 
Example 8
Source File: bug8023392.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: PSPrinterJob.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 10
Source File: PrintLatinCJKTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: PrintLatinCJKTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: WrongPaperPrintingTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@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;
    }
}
 
Example 13
Source File: PrintLatinCJKTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: ImagePrinter.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
 
Example 15
Source File: TestTextPosInPrint.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public int print(Graphics pg, PageFormat pf, int pageNum)
    throws PrinterException {
    if (pageNum > 0){
        return Printable.NO_SUCH_PAGE;
    }

    Graphics2D g2 = (Graphics2D) pg;
    g2.translate(pf.getImageableX(), pf.getImageableY());
    panel.paint(g2);
    return Printable.PAGE_EXISTS;
}
 
Example 16
Source File: WrongPaperForBookPrintingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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() - 1, (int) pf.getImageableHeight() - 1);
        return Printable.PAGE_EXISTS;
    } else {
        return Printable.NO_SUCH_PAGE;
    }
}
 
Example 17
Source File: AttributesWindow.java    From megan-ce with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Print the graph associated with this viewer.
 *
 * @param gc0        the graphics context.
 * @param format     page format
 * @param pagenumber page index
 */
public int print(Graphics gc0, PageFormat format, int pagenumber) throws PrinterException {
    if (pagenumber == 0) {
        Graphics2D gc = ((Graphics2D) gc0);

        Dimension dim = frame.getContentPane().getSize();

        int image_w = dim.width;
        int image_h = dim.height;

        double paper_x = format.getImageableX() + 1;
        double paper_y = format.getImageableY() + 1;
        double paper_w = format.getImageableWidth() - 2;
        double paper_h = format.getImageableHeight() - 2;

        double scale_x = paper_w / image_w;
        double scale_y = paper_h / image_h;
        double scale = Math.min(scale_x, scale_y);

        double shift_x = paper_x + (paper_w - scale * image_w) / 2.0;
        double shift_y = paper_y + (paper_h - scale * image_h) / 2.0;

        gc.translate(shift_x, shift_y);
        gc.scale(scale, scale);

        gc.setStroke(new BasicStroke(1.0f));
        gc.setColor(Color.BLACK);

        frame.getContentPane().paint(gc);

        return Printable.PAGE_EXISTS;
    } else
        return Printable.NO_SUCH_PAGE;
}
 
Example 18
Source File: WrongPaperForBookPrintingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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() - 1, (int) pf.getImageableHeight() - 1);
        return Printable.PAGE_EXISTS;
    } else {
        return Printable.NO_SUCH_PAGE;
    }
}
 
Example 19
Source File: PSPrinterJob.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example 20
Source File: WrongPaperPrintingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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;
    }
}