Java by API/java.awt.print/PrinterGraphics
Версия от 20:43, 31 мая 2010; (обсуждение)
PrinterGraphics: getPrinterJob()
<source lang="java">
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterGraphics; import java.awt.print.PrinterJob; public class MainClass {
public static void main(String[] args) { PrinterJob job = PrinterJob.getPrinterJob(); job.setPrintable(new PrintDemo1()); job.setCopies(2); job.setJobName("Printable"); if (job.printDialog()) { try { job.print(); } catch (PrinterException e) { System.out.println(e); } } System.exit(0); }
} class PrintDemo1 implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException { // pageIndex 0 corresponds to page number 1. if (pageIndex >= 1) return Printable.NO_SUCH_PAGE; PrinterGraphics p = (PrinterGraphics) g; System.out.println(p.getPrinterJob().getCopies()); System.out.println(p.getPrinterJob().getJobName()); Graphics2D g2 = (Graphics2D) g; double w = pf.getImageableWidth(); double h = pf.getImageableHeight(); int xo = (int) pf.getImageableX(); int yo = (int) pf.getImageableY(); Rectangle2D r = new Rectangle2D.Double(xo, yo, w, h); g2.setColor(Color.red); g2.draw(r); Shape s = new Ellipse2D.Double(xo + 4, yo + 4, 32, 32); g2.fill(s); return Printable.PAGE_EXISTS; } int rnd(int limit) { return ((int) (Math.random() * limit)); }
}
</source>