JAVA PRINTING

Lukman Jaji myloveforyou at loveable.com
Mon Nov 29 11:45:50 PST 2004


Hello,
I am trying really hard to print a textarea in a frame. It prints string to graphics via printjob but it does not deal with word wrap so that when I print, I lose all the info that doesn't fit in the paper (to the right of the paper).

Here's my code implementing the command:

public void printCommand() {
PrintJob pjob = getToolkit().getPrintJob(FileViewer.this, "FilePrinter", printprefs);
if (pjob != null)
{
Graphics pg = pjob.getGraphics();
if (pg != null)
{
pg.translate(75, 75);
Dimension size = this.getSize();
// Set a clipping region so our scribbles don't go outside the border
// On-screen this happens automatically, but not on paper.
//pg.setClip(0, 0, size.width, size.height);
String s = textarea.getText();
printLongString(pjob, pg, s);
pg.dispose();
}
pjob.end();
}
}


// Print string to graphics via printjob
// Does not deal with word wrap or tabs
void printLongString (PrintJob pjob, Graphics pg, String s) {
int pageNum = 1;
int linesForThisPage = 0;
int linesForThisJob = 0;
// Note: String is immutable so won't change while printing.
if (!(pg instanceof PrintGraphics)) {
throw new IllegalArgumentException ("Graphics context not PrintGraphics");
}
StringReader sr = new StringReader (s);
LineNumberReader lnr = new LineNumberReader (sr);
String nextLine;
int pageHeight = pjob.getPageDimension().height;
Font myfont = new Font("Tahoma", Font.PLAIN, 9);
//have to set the font to get any output
pg.setFont (myfont);
FontMetrics fm = pg.getFontMetrics(myfont);
int fontHeight = fm.getHeight();
int fontDescent = fm.getDescent();
int curHeight = 0;
try {
do {
nextLine = lnr.readLine();
if (nextLine != null) { 
if ((curHeight + fontHeight) > pageHeight) {
// New Page 
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
pageNum++;
linesForThisPage = 0;
pg.dispose();
pg = pjob.getGraphics();
if (pg != null) {
pg.setFont (myfont);
}
curHeight = 0;
}
curHeight += fontHeight;
if (pg != null) {
pg.drawString (nextLine, 0, curHeight - fontDescent);
linesForThisPage++;
linesForThisJob++;
} else {
System.out.println ("pg null");
}
}
} while (nextLine != null);
} catch (EOFException eof) {
// Fine, ignore
} catch (Throwable t) { // Anything else
t.printStackTrace();
}
System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
System.out.println ("pages printed: " + pageNum);
System.out.println ("total lines printed: " + linesForThisJob);
}

Can anyboy help me to implemenet wordwrap when printing?
Any help will be greatly appreciated and thank you so much in advance....

Thanks!!! 


-- 
___________________________________________________________
Sign-up for Ads Free at Mail.com
http://promo.mail.com/adsfreejump.htm



More information about the freebsd-java mailing list