[Java] Printing an internally generated BufferedImage object

Discussion in 'Mac Programming' started by wrldwzrd89, Nov 25, 2010.

  1. wrldwzrd89, Nov 25, 2010
    Last edited: Nov 25, 2010

    wrldwzrd89 macrumors G5


    Jun 6, 2003
    Solon, OH
    The title says it all... I'm having trouble getting printing to work. Here's the printing code I'm using:

     * To change this template, choose Tools | Templates
     * and open the template in the editor.
    package net.worldwizard.lasertank;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.image.BufferedImage;
    import java.awt.print.PrinterJob;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.SimpleDoc;
    import javax.print.attribute.DocAttributeSet;
    import javax.print.attribute.HashDocAttributeSet;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.swing.JFrame;
     * @author wrldwzrd89
    public class BoardPrinter {
        private BoardPrinter() {
            // Do nothing
        public static void printBoard(JFrame j) {
            try {
                Container c = j.getContentPane();
                Dimension d = c.getPreferredSize();
                BufferedImage bi = new BufferedImage(d.width, d.height,
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                ImageIO.write(bi, "PNG", baos);
                byte[] data = baos.toByteArray();
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
                PrinterJob pj = PrinterJob.getPrinterJob();
                boolean okay = pj.printDialog(pras);
                if (okay) {
                    PrintService service = pj.getPrintService();
                    DocPrintJob job = service.createPrintJob();
                    DocAttributeSet das = new HashDocAttributeSet();
                    Doc doc = new SimpleDoc(bais, flavor, das);
                    job.print(doc, pras);
            } catch (IOException ioe) {
                CommonDialogs.showErrorDialog("Printing failed!", "Print GameBoard");
            } catch (PrintException pe) {
                CommonDialogs.showErrorDialog("Printing failed!", "Print GameBoard");
            } catch (NullPointerException npe) {
                CommonDialogs.showErrorDialog("Printing failed!", "Print GameBoard");
    The CommonDialogs class that's being invoked here is just a convenience class for displaying frequently used dialog boxes. Printing works on the Java end, but when it sends the data to the printer I get an error:
    pstopdffilter/pstocupsraster failed with err number -31000
    I'm not sure what's going on. :confused:

    EDIT 2: Just tried the same printing code on my Windows 7 machine. It works there.

Share This Page