successful operation if printer doesn't exist ?
Stefan Sitte
stefan.sitte at comsoft.de
Fri Apr 25 07:39:52 PDT 2008
Hello,
here is a java code snippet to reproduce the "error".
While running the snippet delete the printer (the default printer is used because of services[0]).
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
public class Print2DPrinterJob implements Printable{
public static void main(String arg[]) {
final Print2DPrinterJob sp = new Print2DPrinterJob();
final PrintService[] services = PrintServiceLookup.lookupPrintServices(
DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
if (services.length > 0) {
sp.run(services[0]);
}
}
private void run(PrintService service) {
while (true) {
if (service != null) {
final PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(this);
System.out.println("selected printer " + service.getName());
try {
pj.setPrintService(service);
pj.print();
} catch (final PrinterException pe) {
String date = new SimpleDateFormat("kk:mm:ss")
.format(new Date());
System.err.println("[" + date + "] " + pe);
}
}
try {
Thread.sleep(30000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
public int print(Graphics g,PageFormat pf,int pageIndex) {
if (pageIndex == 0) {
Graphics2D g2d= (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
g2d.setColor(Color.black);
g2d.drawString("example string", 250, 250);
g2d.fillRect(0, 0, 200, 200);
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}
}
More information about the cups
mailing list