Java Code Examples for java.awt.print.Printable

The following are top voted examples for showing how to use java.awt.print.Printable. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: OpenDA   File: PlotBox.java   View source code 7 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.
 */

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;
}
 
Example 2
Project: OpenJSharp   File: ImagePrinter.java   View source code 6 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 3
Project: OpenJSharp   File: PSPrinterJob.java   View source code 6 votes vote down vote up
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);
}
 
Example 4
Project: OpenJSharp   File: TextComponentPrintable.java   View source code 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 5
Project: openjdk-jdk10   File: ImageableAreaTest.java   View source code 6 votes vote down vote up
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);
        }
    }
}
 
Example 6
Project: jdk8u-jdk   File: PSPrinterJob.java   View source code 6 votes vote down vote up
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);
}
 
Example 7
Project: openjdk-jdk10   File: PrintDlgSelectionAttribTest.java   View source code 6 votes vote down vote up
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) {
        }
    }
 
Example 8
Project: cuttlefish   File: GraphEditorDemo.java   View source code 6 votes vote down vote up
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);
    }
}
 
Example 9
Project: jdk8u-jdk   File: WPrinterJob.java   View source code 6 votes vote down vote up
/**
 * End a page.
 */
@Override
protected void endPage(PageFormat format, Printable painter,
                       int index) {

    deviceEndPage(format, painter, index);
}
 
Example 10
Project: jdk8u-jdk   File: ImageableAreaTest.java   View source code 6 votes vote down vote up
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);
        }
    }
}
 
Example 11
Project: jdk8u-jdk   File: PrintLatinCJKTest.java   View source code 6 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
Project: openjdk-jdk10   File: PathGraphics.java   View source code 6 votes vote down vote up
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;
}
 
Example 13
Project: openjdk-jdk10   File: ImageableAreaTest.java   View source code 6 votes vote down vote up
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);
        }
    }

}
 
Example 14
Project: openjdk-jdk10   File: TextComponentPrintable.java   View source code 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 15
Project: incubator-netbeans   File: PageableScene.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 16
Project: rapidminer   File: PrintingTools.java   View source code 5 votes vote down vote up
/**
 * @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;
	}
}
 
Example 17
Project: openjdk-jdk10   File: PrintLatinCJKTest.java   View source code 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 18
Project: onprom   File: UMLDiagramPanel.java   View source code 5 votes vote down vote up
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());
    }
  }
}
 
Example 19
Project: smile_1.5.0_java7   File: Printer.java   View source code 5 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 20
Project: ramus   File: AbstractRamusPrintable.java   View source code 5 votes vote down vote up
@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());
    }
}
 
Example 21
Project: openjdk-jdk10   File: WrongPaperPrintingTest.java   View source code 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 22
Project: openjdk-jdk10   File: UnixPrintJob.java   View source code 5 votes vote down vote up
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);
    }
}
 
Example 23
Project: openjdk-jdk10   File: ImageableAreaTest.java   View source code 5 votes vote down vote up
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!");
    }
}
 
Example 24
Project: OpenJSharp   File: PSPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 25
Project: OpenJSharp   File: PSPrinterJob.java   View source code 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 26
Project: OpenJSharp   File: PSPrinterJob.java   View source code 5 votes vote down vote up
public Printable getPrintable(int pgIndex) {
    if (pgIndex > 0) {
        throw new IndexOutOfBoundsException("pgIndex");
    } else {
    return printable;
    }
}
 
Example 27
Project: OpenJSharp   File: WPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 28
Project: OpenJSharp   File: WPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 29
Project: OpenJSharp   File: WPrinterJob.java   View source code 5 votes vote down vote up
/**
 * End a page.
 */
@Override
protected void endPage(PageFormat format, Printable painter,
                       int index) {

    deviceEndPage(format, painter, index);
}
 
Example 30
Project: openjdk-jdk10   File: WPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 31
Project: jdk8u-jdk   File: PathGraphics.java   View source code 5 votes vote down vote up
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;
}
 
Example 32
Project: jdk8u-jdk   File: ImagePrinter.java   View source code 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 33
Project: jdk8u-jdk   File: PSPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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);
}
 
Example 34
Project: jdk8u-jdk   File: PSPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 35
Project: jdk8u-jdk   File: PSPrinterJob.java   View source code 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 36
Project: jdk8u-jdk   File: PSPrinterJob.java   View source code 5 votes vote down vote up
public Printable getPrintable(int pgIndex) {
    if (pgIndex > 0) {
        throw new IndexOutOfBoundsException("pgIndex");
    } else {
    return printable;
    }
}
 
Example 37
Project: openjdk-jdk10   File: bug8023392.java   View source code 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 38
Project: jdk8u-jdk   File: WPrinterJob.java   View source code 5 votes vote down vote up
/**
 * 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;
}
 
Example 39
Project: jdk8u-jdk   File: ImageableAreaTest.java   View source code 5 votes vote down vote up
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!");
    }
}
 
Example 40
Project: jdk8u-jdk   File: PrintCrashTest.java   View source code 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    PrinterJob printerJob = PrinterJob.getPrinterJob();
    printerJob.setPrintable((graphics, pageFormat, pageIndex) -> {
        if (pageIndex != 0) {
            return Printable.NO_SUCH_PAGE;
        } else {
            Shape shape = new Rectangle(110, 110, 10, 10);
            Rectangle rect = shape.getBounds();

            BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
                    .getDefaultConfiguration().createCompatibleImage(rect.width, rect.height, Transparency.BITMASK);
            graphics.drawImage(image, rect.x, rect.y, rect.width, rect.height, null);

            return Printable.PAGE_EXISTS;
        }
    });

    File file = null;
    try {
        HashPrintRequestAttributeSet hashPrintRequestAttributeSet = new HashPrintRequestAttributeSet();
        file = File.createTempFile("out", "ps");
        file.deleteOnExit();
        Destination destination = new Destination(file.toURI());
        hashPrintRequestAttributeSet.add(destination);
        printerJob.print(hashPrintRequestAttributeSet);
    } finally {
        if (file != null) {
            file.delete();
        }
    }
}