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;
}
}
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!
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;
}
>
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