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
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