Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Nov 2004 14:45:46 -0500
From:      "Lukman Jaji" <myloveforyou@loveable.com>
To:        freebsd-java@freebsd.org
Subject:   JAVA PRINTING
Message-ID:  <20041129194546.734D04BDAA@ws1-1.us4.outblaze.com>

next in thread | raw e-mail | index | archive | help
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 p=
rint, I lose all the info that doesn't fit in the paper (to the right of th=
e paper).

Here's my code implementing the command:

public void printCommand() {
PrintJob pjob =3D getToolkit().getPrintJob(FileViewer.this, "FilePrinter", =
printprefs);
if (pjob !=3D null)
{
Graphics pg =3D pjob.getGraphics();
if (pg !=3D null)
{
pg.translate(75, 75);
Dimension size =3D 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 =3D 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 =3D 1;
int linesForThisPage =3D 0;
int linesForThisJob =3D 0;
// Note: String is immutable so won't change while printing.
if (!(pg instanceof PrintGraphics)) {
throw new IllegalArgumentException ("Graphics context not PrintGraphics");
}
StringReader sr =3D new StringReader (s);
LineNumberReader lnr =3D new LineNumberReader (sr);
String nextLine;
int pageHeight =3D pjob.getPageDimension().height;
Font myfont =3D new Font("Tahoma", Font.PLAIN, 9);
//have to set the font to get any output
pg.setFont (myfont);
FontMetrics fm =3D pg.getFontMetrics(myfont);
int fontHeight =3D fm.getHeight();
int fontDescent =3D fm.getDescent();
int curHeight =3D 0;
try {
do {
nextLine =3D lnr.readLine();
if (nextLine !=3D null) {=20
if ((curHeight + fontHeight) > pageHeight) {
// New Page=20
System.out.println ("" + linesForThisPage + " lines printed for page " + pa=
geNum);
pageNum++;
linesForThisPage =3D 0;
pg.dispose();
pg =3D pjob.getGraphics();
if (pg !=3D null) {
pg.setFont (myfont);
}
curHeight =3D 0;
}
curHeight +=3D fontHeight;
if (pg !=3D null) {
pg.drawString (nextLine, 0, curHeight - fontDescent);
linesForThisPage++;
linesForThisJob++;
} else {
System.out.println ("pg null");
}
}
} while (nextLine !=3D null);
} catch (EOFException eof) {
// Fine, ignore
} catch (Throwable t) { // Anything else
t.printStackTrace();
}
System.out.println ("" + linesForThisPage + " lines printed for page " + pa=
geNum);
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!!!=20


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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20041129194546.734D04BDAA>