Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't print the content to printer  RSS feed

 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello JGuru,
I am using Printable to print a preloaded text from a vector. I wonder why I could not print the content of text in the my loop to the page. I could print this content to awt panel. Any idea is appreciated.

public int print(Graphics g, PageFormat pf, int pageIndex) throws Exception
{
int wPage = (int)pf.getImageableWidth();
int hPage = (int)pf.getImageableHeight();

if (lines == null)
lines = textLoader.getPrintText(g, wPage);

g.translate((int)pf.getImageableX(), (int)pf.getImageableY());
// this statement was printed on the page (paper)
g.drawString("File: " + fileName + ", page: " + (pageIndex+1), 50, 50);

if (lineIndex >= lines.size())
{
System.out.println("I terminate it at " + lineIndex);
return Printable.NO_SUCH_PAGE;
}

int y = 0;

while (y < hPage)
{
Line line = (Line)lines.elementAt(lineIndex);
y += line.getHeight();
// the following line is not printed. ???
g.drawString("File: " + fileName + ", page: " + (pageIndex+1), 0, y);
printLine(line, g, y);
++lineIndex; // was initialied to Zero in the constructor

if (lineIndex >= lines.size())
break;
}// end of while
return Printable.PAGE_EXISTS;
}
private void printLine(Line line, Graphics g, int linePosition)
{
while(line.hasMoreSegment())
{
Segment segment = line.nextSegment();
g.setFont(segment.getFont());
System.out.print("\t" + segment.getText());
g.drawString(segment.getText(), segment.getStart(), linePosition);

if (segment.isUnderline())
{
g.drawLine(segment.getStart(), linePosition + 2, segment.getEnd(), linePosition + 2);
}// end of while
}
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!