Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 25, 2010, 03:47 PM   #1
wrldwzrd89
macrumors G4
 
wrldwzrd89's Avatar
 
Join Date: Jun 2003
Location: Solon, OH
[Java] Printing an internally generated BufferedImage object

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.

EDIT 2: Just tried the same printing code on my Windows 7 machine. It works there.
__________________
iMac Intel (Rev H, 27"), 1TB HDD, 16GB RAM, 10.8.4

Last edited by wrldwzrd89; Nov 25, 2010 at 05:44 PM. Reason: Changed the code; still does not work
wrldwzrd89 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Can I replace my internal Dvd drive with an internal Blu-Ray? inrefrto iMac 6 Jun 17, 2011 01:34 PM
HELP! I need Java! But don't have an earlier version installed! bs2511feist OS X 10 Apr 14, 2011 07:24 PM
Is my internal hard drive dying? Jodeo iMac 8 Mar 18, 2011 01:02 PM
Automatic code generation for Objective C to C++ bridge aks86 Mac Programming 5 Apr 3, 2009 06:33 PM


All times are GMT -5. The time now is 03:57 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC