Java Printing fails on OutOfMemoryError

D

Dean

My Problem:
I am trying to print to a HP LaserJet 1200 printer using Java. My goal
is to use a GIF image on disk as a background template and then use
Graphics2D to draw onto that image (this works fine for onscreen display).
This image is large, it fills the page less a half-inch border. The printer
prints at 1200dpi. The problem is that when I print, java grows and grows
(to over 100 megs) before finally throwing an OutOfMemoryError. What is
going on here? Is there a better way to print this thing?

The code I used is below. Any insight would be greatly appreciated.

Thanks in advance,
Dean


import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;

public class barebones implements Printable{

static BufferedImage buffy;

public static void main(String args[]){
new barebones();
}//end main


barebones(){
Image i = Toolkit.getDefaultToolkit().createImage("c:/front2.gif");
buffy = new BufferedImage(2437,3209,BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2 = buffy.createGraphics();
g2.drawImage(i,0,0,null);
g2.setColor(Color.BLACK);
g2.drawString("Hello",400,400);
i=null;
printIt();
//Runtime rt = Runtime.getRuntime();
//rt.gc();
//System.out.println(rt.totalMemory()/1024);
//System.out.println(rt.freeMemory()/1024);
}//end barebones

public void printIt(){
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
Paper letter = new Paper();
letter.setSize(612d,792d);
letter.setImageableArea(.5d*72d,.5d*72d,7.5d*72d,10d*72d);
pf.setPaper(letter);

pj.setPrintable(this, pf);

try{
pj.print();
} catch(PrinterException e){
System.err.println(e);
}//end catch
}//end printit


public int print(Graphics g,PageFormat pf,int pageIndex) {
if (pageIndex == 0) {
Graphics2D g2 = (Graphics2D) g;
g.drawImage(buffy,36,36,540,720,null);
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}//end print if
}//end print method
}//end barebones class
 
R

Robert Olofsson

Dean ([email protected]) wrote:
: I am trying to print to a HP LaserJet 1200 printer using Java. My goal
: ....
: (to over 100 megs) before finally throwing an OutOfMemoryError. What is
: going on here? Is there a better way to print this thing?

: The code I used is below. Any insight would be greatly appreciated.
: ....
: public int print(Graphics g,PageFormat pf,int pageIndex) {
: if (pageIndex == 0) {
: Graphics2D g2 = (Graphics2D) g;
: g.drawImage(buffy,36,36,540,720,null);
: return PAGE_EXISTS;
: ....

How many times does print get called?
What is the clip set to for each of the calls? Have you tried to
respect the clip rect?

Other than that, grab a profiler (jmp, jprofiler, mjp, optimizeit,
jprobe...) and find out where the memory is going.

/robo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top