-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
beta2
-
x86
-
windows_98
Name: bsC130419 Date: 06/06/2001
java version "1.4.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)
PrinterJob.printDialog() allows you to request more than one copy collated.
This doesn't (and shouldn't) work with Printable's, so the request shouldn't be
allowed.
Note: If you request 2 copies, collated, JDK 1.4 beta produces 2 copies,
uncollated. JDK 1.3 produces 1 copy. Both JKD's have the correct behavior if
you do not request collated.
Program exhibiting this behavior follows (taken from sec. 7.3.2 of:
http://java.sun.com/j2se/1.3/docs/guide/2d/spec/j2d-print.fm3.html )
import java.awt.*;
import java.awt.print.*;
import java.io.*;
public class PrintListing {
public static void main(String[] args) {
// Get a PrinterJob
PrinterJob job = PrinterJob.getPrinterJob();
// Ask user for page format (e.g., portrait/landscape)
PageFormat pf = job.pageDialog(job.defaultPage());
// Specify the Printable is an instance of
// PrintListingPainter; also provide given PageFormat
job.setPrintable(new PrintListingPainter(args[0]), pf);
// Print 1 copy
job.setCopies(1);
// Put up the dialog box
if (job.printDialog()) {
// Print the job if the user didn't cancel printing
try { job.print(); }
catch (Exception e) { /* handle exception */ }
}
System.exit(0);
}
}
class PrintListingPainter implements Printable {
private RandomAccessFile raf;
private String fileName;
private Font fnt = new Font("Helvetica", Font.PLAIN, 10);
private int rememberedPageIndex = -1;
private long rememberedFilePointer = -1;
private boolean rememberedEOF = false;
public PrintListingPainter(String file) {
fileName = file;
try {
// Open file
raf = new RandomAccessFile(file, "r");
}
catch (Exception e) { rememberedEOF = true; }
}
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException {
try {
// For catching IOException
if (pageIndex != rememberedPageIndex) {
// First time we've visited this page
rememberedPageIndex = pageIndex;
// If encountered EOF on previous page, done
if (rememberedEOF) return Printable.NO_SUCH_PAGE;
// Save current position in input file
rememberedFilePointer = raf.getFilePointer();
}
else raf.seek(rememberedFilePointer);
g.setColor(Color.black);
g.setFont(fnt);
int x = (int) pf.getImageableX() + 10;
int y = (int) pf.getImageableY() + 12;
// Title line
g.drawString("File: " + fileName + ", page: " +
(pageIndex+1), x, y);
// Generate as many lines as will fit in imageable area
y += 36;
while (y + 12 < pf.getImageableY()+pf.getImageableHeight()){
String line = raf.readLine();
if (line == null){
rememberedEOF = true;
break;
}
g.drawString(line, x, y);
y += 12;
}
return Printable.PAGE_EXISTS;
}
catch (Exception e) { return Printable.NO_SUCH_PAGE;}
}
}
(Review ID: 125698)
======================================================================