problem trying to send to printer multi-line text file inside a while loop

Hi everybody!

i'm using graphics2D and java.awt.print* to print some formated text files. Everything is ok, I can print single lines of formated text, drawings and so on, but just can't print anything when my print statements (i.e., graphics.drawString(), graphics.drawRect(), etc) are inside a while loop. I already test and i'm sure that program do enter the loop... Does someone knows what's happening?

Below is a piece of my code... thank u all!

publicclass SinglePageimplements Printable

{

private Iterator itr;

public SinglePage(Iterator _itr)

{

itr = _itr;

}

publicint print(Graphics graphics, PageFormat pageFormat,int pageIndex)

{

Graphics2D g2d = (Graphics2D) graphics;

g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

float posX = 0;

float posY = 0;

float larguraTexto = 350;

g2d.setColor(Color.BLACK);

g2d.setFont(new Font("Arial", Font.PLAIN, 12));

//I CAN print here...

while(itr.hasNext())//I can't print inside this loop

{

if(!itr.hasNext())break;

g2d.setColor(Color.BLACK);

g2d.setFont(new Font("Arial", Font.PLAIN, 12));

String line = (String) itr.next();

g2d.drawString(line, posX, posY);

System.out.println(line);//this is printing ok to stdout

posY += 20;

}

//i CAN print here too...

return PAGE_EXISTS;

}

}

[2410 byte] By [cassio.marquesa] at [2007-10-2 20:19:54]
# 1

You cant use a loop here. The print subsytem calls a Printable objects print() method multilpe times until it returns NO_SUCH_PAGE.

For each index, you must print that page, or return NO_SUCH_PAGE.

The complication is you will get called multiple time for each page. (At leat on 1.5/1.6 Windows implementation. I posted code for printing multiple text pages a few days ago, if you search for it.

armalcolma at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks!

But.. ok, i know print() is called until it returns NO_SUCH_PAGE, but what my while() is doing is printing each single line of text inside the SAME page...

This page is inside a object extended from teh Book class, to implement Pageable. But it`s not working this way, I can send different text buffers (using ArrayList) to my printable object, but can go through all the lines in this ArrayList inside the while()... Better, I can run through the lines, but graphics.draw() is not working in the loop.

How could I do that so?

Thank u again!

cassio.marquesa at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 3

Strange. This works for me, and is quite similar:

public int print(Graphics g,PageFormat pageFormat,int pageIndex)

{

((Graphics2D)g).translate(pageFormat.getImageableX(),pageFormat.getImageableY());

g.setFont(m_font);

int charHeight=g.getFontMetrics().getDescent()+g.getFontMetrics().getAscent()+g.getFontMetrics().getLeading();

int pageHeight=(int)pageFormat.getImageableHeight();

int linesPerPage=pageHeight/charHeight;

int index=pageIndex*linesPerPage;

if(index>=m_lines.length)

{

return Printable.NO_SUCH_PAGE;

}

for(int line=0;((index<m_lines.length)&&(line><linesPerPage));line++,index++)

{

g.drawString(m_lines[index],0,charHeight*(line+1));

}

return Printable.PAGE_EXISTS;

}

>

armalcolma at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 4

Ah. I got it. As I said, you get called TWICE per page, and if you return different data the second time thats what gets printed. In your case you won't print anything second time as your iterator is already empty. You can't do anything in print which changes the state of your object (lines printed etc. otherwise this is the result. Look at my code carefully and you'll see its designed to ALWAYS print the same no matter how many times print() gets called with a given pageIndex

armalcolma at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 5
Thank u! I fixed my code!The problem was in the loop conditions i guess, don`t know... I just used your for loop, changing where needed.. and it works... Don`t know why, i`ll try to figure it out.. BUT THANK U!
cassio.marquesa at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 6

Ok, now i see!

I already deleted my code here... but I think that if i send a reference to the whole ArrayList to the Printable Object, not just the Iterator, and in the end of the print() function make Iterator point to the beggining of the ArrayList again, this should work... What do u think?

cassio.marquesa at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 7

Like I said, you can't maintain any state in the object because yo get called multiple times per page (says so in the spec., put in some print statements and prove it to yourself).

So you need to calculate the offset into your array of strings from the page index, which is what my code does. If you use your suggested method, you'll get the first page printed over and over again.

I've agonised over many implementations of this, and its a pain that print() gets called twice, but that's the way it is and you have to code round it.

A

armalcolma at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...
# 8
Ok, thank u! The problem is that i've learned to use the print() methods without knowing what was really going on behind the scenes, but know I understand it better.Bye!
cassio.marquesa at 2007-7-13 23:02:10 > top of Java-index,Java Essentials,Java Programming...