Posted By:
Andreas_Hess
Posted On:
Friday, January 17, 2003 08:32 AM
Hello, I'm having touble printing simple text in jdk1.3. I created a class implementing the "Printable" interface. (see below) If I try to print a short document (1 page) it is allways empty. Longer printouts (more than 2 pages) work, but the first page is missing and the last page is empty? Any idea what's wrong. Thanks Andreas CODE: public class TextPrinterV2 implements Printable { private Font oFont = new Font("Monospaced", Font.PLAIN, 9); private LinkedList oText = new LinkedList(); public TextPrinter(String text) throws IOException { StringReader oStringReader = ne
More>>
Hello,
I'm having touble printing simple text in jdk1.3.
I created a class implementing the "Printable" interface.
(see below)
If I try to print a short document (1 page) it is allways
empty. Longer printouts (more than 2 pages) work, but the
first page is missing and the last page is empty?
Any idea what's wrong.
Thanks
Andreas
CODE:
public class TextPrinterV2 implements Printable
{
private Font oFont = new Font("Monospaced", Font.PLAIN, 9);
private LinkedList oText = new LinkedList();
public TextPrinter(String text)
throws IOException
{
StringReader oStringReader = new StringReader(text);
LineNumberReader oLineReader = new LineNumberReader(oStringReader);
String sLine;
while((sLine = oLineReader.readLine()) != null)
{
oText.addLast(sLine);
}
}
public int print(Graphics g, PageFormat format, int page_index)
throws PrinterException
{
// text all text printed
if(oText.size() == 0)
{
return(Printable.NO_SUCH_PAGE);
}
Graphics2D g2d = (Graphics2D)g;
g2d.setFont(oFont);
g2d.setColor(Color.black);
FontMetrics oFontMetric = g2d.getFontMetrics(oFont);
int fontHeight = oFontMetric.getHeight() + oFontMetric.getLeading();
int fontDesent = oFontMetric.getDescent();
double pageHeight = format.getImageableHeight();
int offsetX = (int)format.getImageableX();
int offsetY = (int)format.getImageableY();
int numRowsOnAPage = (int)(pageHeight / (fontHeight - fontDesent));
for(int iLoop = 1; iLoop
<= numRowsOnAPage && oText.size() > 0; iLoop++)
{
int y_coodinate = offsetY + iLoop * fontHeight - fontDesent;
g2d.drawString((String)oText.removeFirst(), offsetX, y_coodinate);
}
return(Printable.PAGE_EXISTS);
}
<<Less