Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

wrldwzrd89

macrumors G5
Original poster
Jun 6, 2003
12,110
77
Solon, OH
The title says it all... I'm having trouble getting printing to work. Here's the printing code I'm using:

Code:
/*
 * 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,
                    BufferedImage.TYPE_INT_ARGB);
            c.paintComponents(bi.createGraphics());
            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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.