Why does the java Printable's print method get called multiple times with the same page number?
From sun's documentation
"The printing system might request that a page be rendered multiple times before moving to the next page."
The examples always show something like this:
Printable print(Graphics g, PageFormat pageFormat, int page) {
if (page == 0)
do...
else if(page == blah...)
}
If you follow this pattern your code typically works fine because it is explicit based on the 开发者_JS百科page number. Not following this pattern caused me great pain until I realized it was getting called multiple times with the same page number and started to cache the pages.
Why does the java Printable's print method get called multiple times with the same page number?
The Java printing system is at the mercy of the underlying OS printing system, and that system may request a single page be rendered multiple times.
One reason is banded printing -- if the printer doesn't have enough memory to render the entire page at once -- in that case, the OS will ask Java for the page again so it can print the page in strips ("bands"). This is the specific case mentioned in the Java 2D Programmer's Guide, in the section "Printing Concepts".
There may be other reasons; it's really up to the OS's printing system.
There are a number of reasons it may be doing this.
Depending on the underlying print system it may want to compute certain properties "up front" (eg: page extents, ink usage, etc.) without having to buffer the entire document.
Also, certain printing systems are "band based", rather than page-based. Inkjets, for example, will print out one a horizontal band of raster data at a time. Rather than buffering a page's worth of raster data (about 100MB for a 600dpi US letter page) the Java printing system may buffer only a few bands (or possibly even just one band) at a time.
精彩评论