mbunkows
asked on
Printing in Threads
I need the user to be able to manuever within a GUI while the user is printing (from Java).
Im trying to get Java2D printing to run in a separate thread.
Java 2D printing sometimes takes 2-3 minutes to run, thats alot of time for a user to be staring at the screen.
I have a print method (in the GUI class) that has this code
fragment:
try {
PrinterJob printerjob= PrinterJob.getPrinterJob() ;
PageFormat pf= printerjob.defaultPage();
printerjob.setPrintable(ne w PrintGraphPainter(linegrap h.getImage s()),pf);
if (printerjob.printDialog()) {
printerjob.print();
}
}
catch (PrinterException pe) {
System.out.println("Print Error");
pe.printStackTrace();
}
Heres the PrintGraphPainter class:
//PrintGraphPainter.java
import java.awt.*;
import java.awt.print.*;
class PrintGraphPainter implements Printable {
Image[] images;
public PrintGraphPainter(Image[] images) {
this.images=images;
}
public synchronized int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex < 1) {
Dimension f= new Dimension((int)pf.getImage ableWidth( ),(int)pf. getImageab leHeight() );
int XIMG=(int) (f.width-10); int YIMG=(int)(XIMG * .5042);
int XIMG2=(int)(f.width-10); int YIMG2=(int)(XIMG2 * .1433);
int XIMG3=(int)(f.width-10); int YIMG3=(int)(XIMG3 * .07163);
g.drawImage(images[0],(int )pf.getIma geableX(), (int)pf.ge tImageable Y(),XIMG3, YIMG3,null );
g.drawImage(images[1],(int )pf.getIma geableX(), (int)pf.ge tImageable Y()+YIMG3, XIMG,YIMG, null);
g.drawImage(images[2],(int )pf.getIma geableX(), (int)pf.ge tImageable Y()+YIMG3+ YIMG,XIMG2 ,YIMG2,nul l);
g.drawRect((int)pf.getImag eableX(),( int)pf.get ImageableY (),XIMG,YI MG+YIMG2+Y IMG3);
return Printable.PAGE_EXISTS;
}
else
return Printable.NO_SUCH_PAGE;
}
}
Do I make the PrintGraphPainter class implement the Runnable interface?
Im using JDK1.2 on Win95.
Im trying to get Java2D printing to run in a separate thread.
Java 2D printing sometimes takes 2-3 minutes to run, thats alot of time for a user to be staring at the screen.
I have a print method (in the GUI class) that has this code
fragment:
try {
PrinterJob printerjob= PrinterJob.getPrinterJob()
PageFormat pf= printerjob.defaultPage();
printerjob.setPrintable(ne
if (printerjob.printDialog())
printerjob.print();
}
}
catch (PrinterException pe) {
System.out.println("Print Error");
pe.printStackTrace();
}
Heres the PrintGraphPainter class:
//PrintGraphPainter.java
import java.awt.*;
import java.awt.print.*;
class PrintGraphPainter implements Printable {
Image[] images;
public PrintGraphPainter(Image[] images) {
this.images=images;
}
public synchronized int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
if (pageIndex < 1) {
Dimension f= new Dimension((int)pf.getImage
int XIMG=(int) (f.width-10); int YIMG=(int)(XIMG * .5042);
int XIMG2=(int)(f.width-10); int YIMG2=(int)(XIMG2 * .1433);
int XIMG3=(int)(f.width-10); int YIMG3=(int)(XIMG3 * .07163);
g.drawImage(images[0],(int
g.drawImage(images[1],(int
g.drawImage(images[2],(int
g.drawRect((int)pf.getImag
return Printable.PAGE_EXISTS;
}
else
return Printable.NO_SUCH_PAGE;
}
}
Do I make the PrintGraphPainter class implement the Runnable interface?
Im using JDK1.2 on Win95.
ASKER
Good thing I asked I thought this was actually going to take some work!
Post an answer and I'll give you an A
Thanks!
Post an answer and I'll give you an A
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
like this :
Thread t = new Thread () {
public void run() {
try {
PrinterJob printerjob= PrinterJob.getPrinterJob()
PageFormat pf= printerjob.defaultPage();
printerjob.setPrintable(ne
if (printerjob.printDialog())
printerjob.print();
}
}
catch (PrinterException pe) {
System.out.println("Print Error");
pe.printStackTrace();
}
}
};
t.start();