Java Printing

Hello all,

I'm have some trouble printing multiple pages within each print loop in the print framework.

Let me first explain what I want to actually do. I have created a program that creates a basketball tournament schedule, with divisions and pools. What I want to do is print out a schedule for each division, which is cake, but the tricky part is I want each pool to be on its own page.

When the user clicks Print he can specify which divisions to print, i.e. 1-5 or all etc. Within my

print(Graphics g, PageFormat pf, int page)

method, page obviously refers to the divisions which the user wants to print, but I'm stuck on how to create a new page for each pool in a division. How do I create a new page without incrementing the page variable?

[788 byte] By [WhiteTreea] at [2007-11-27 0:12:37]
# 1
any ideas?
WhiteTreea at 2007-7-11 21:55:18 > top of Java-index,Java Essentials,Java Programming...
# 2

That is not what the page variable is meant for. When you are printing, the page number will get passed into the method, starting from 0 or 1 and incrementing upwards. You return either PAGE_EXISTS or NO_SUCH_PAGE to inform the PrinterJob if there are more pages.

If it passes in page 2, and there is no page 2 in your printout, you would return NO_SUCH_PAGE.

If you want to print out all 5 divisions, then the page variable could be used to determine which division will be printed to that page. Otherwise you would need a queue of some sort that will hold the divisions you want printed. Each time the print method is called, you would just print the next division from the queue. In this case, you don't really need the page number. You may have to be careful though, the print method is often called twice per page, I never understood why.

robtafta at 2007-7-11 21:55:18 > top of Java-index,Java Essentials,Java Programming...
# 3

Thanks for your reply. The problem is each division may have more than one page. Although I want the page variable to still refer to the division, how do I create a new page?

Just for debugging purposes, I tried just printing 1000 lines of text, however, a new page is not automatically created. Any ideas?

Thanks.

WhiteTreea at 2007-7-11 21:55:18 > top of Java-index,Java Essentials,Java Programming...
# 4

I see, your 1000 lines ends up being placed past the bottom of the paper and not on the next page. You would have to know how big the imageableArea is, and once your Y position for your text crosses that value, then return PAGE_EXISTS. Once it is called again with the next page number, the continue drawing the text. Repeat this until all of the text is completed. You will need something to store what line number you left off with, and reset that value when you get to the end.

robtafta at 2007-7-11 21:55:18 > top of Java-index,Java Essentials,Java Programming...
# 5

Yea thats what I was thinking, however returning PAGE_EXISTS will increment the page variable and thus change the division I'm referencing to. I want to stay in the current division. :( I could have a variable that holds the current division, but then the page variable will no longer be associated with the divisions. I guess what I'm trying to do is force the creation of a new page within a loop.

WhiteTreea at 2007-7-11 21:55:18 > top of Java-index,Java Essentials,Java Programming...
# 6

Rather than take this approach, you could have a class that prints a single division. If there are 5 divisions you want to print, construct 5 of these division printing classes and send them to the print queue.

This way you aren't doing both the page logic and division logic inside the print method. The page variable simply means the page # of the paper being printed. Trying to make the division number and page number line up is just not going to work, or would take a crazy amount of hacking to make it work that way. It also makes it harder to debug doing it that way.

robtafta at 2007-7-11 21:55:18 > top of Java-index,Java Essentials,Java Programming...