• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java printing  RSS feed

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I got code to print contents of JTextArea from this forum. I tried it and it works...sort of. When I run application the data is sent to the printer, but nothing gets printed out. I am not sure why. I read on one of page that java.awt.print has a bug where the default color for text is white, but this issue is taken care of here. So I am not sure why it does not work.
In the class, a page number is send to printer as a footer -- I get that, but nothing that I set using: PrintJText.printComponent(new JTextArea("test111")); in main.
What's wrong here? May be someone can run this and respond with what they got?
thanks a lot,
Alex
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, man...I got so freaked out with this printing problem that forgot to supply the code...sorry. Here goes.
============================================================
import java.awt.*;
import javax.swing.*;
import java.awt.print.*;
public class PrintJText implements Printable {
private JTextArea maJTextArea;
public static void printComponent(JTextArea maJTextArea) {
new PrintJText(maJTextArea).print();
}
public PrintJText(JTextArea maJTextArea) {
this.maJTextArea = maJTextArea;
}
public void print() {
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(PrintJText.this);
try{
if (pj.printDialog())
pj.print();
}
catch (Exception PrintException) {}
}
public int print(Graphics g, PageFormat pageFormat, int pageIndex) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight = g2.getFontMetrics().getHeight();
int fontDesent = g2.getFontMetrics().getDescent();
double pageHeight = pageFormat.getImageableHeight() - fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double headerHeightOnPage =16.5;
double oneRowHeight = fontHeight;
int numRowsOnAPage = (int) ((pageHeight - headerHeightOnPage)/oneRowHeight);
int totalNumPages=(int)Math.ceil(((double)(maJTextArea.getLineCount())/numRowsOnAPage));
System.out.println(totalNumPages);
double pageHeightForTable=oneRowHeight*numRowsOnAPage;
double tableWidth = 468;
double scale=1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double tableWidthOnPage = tableWidth*scale;
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
g2.drawString("Printing Page: "+(pageIndex+1), (int)pageWidth/2-35, (int)(pageHeight +fontHeight-fontDesent));
g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable);
if (pageIndex + 1 == totalNumPages) {
int lastRowPrinted = numRowsOnAPage * pageIndex;
int numRowsLeft = totalNumPages*numRowsOnAPage - lastRowPrinted;
g2.setClip(0,(int)(pageHeightForTable * pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(oneRowHeight * numRowsLeft));
}
else{
g2.setClip(0,(int)(pageHeightForTable*pageIndex),(int) Math.ceil(tableWidthOnPage),(int) Math.ceil(pageHeightForTable));
}
g2.scale(scale,scale);
maJTextArea.paint(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForTable);
g2.translate(0f, -headerHeightOnPage);
g2.setClip(0, 0,
(int) Math.ceil(tableWidthOnPage),
(int)Math.ceil(headerHeightOnPage));
g2.scale(scale,scale);
return Printable.PAGE_EXISTS;
}
public static void main(String[] args){
PrintJText.printComponent(new JTextArea("test111"));
}
}
============================================================
thanks again,
Alex
 
Michael Szul
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just bought a book called Core JFC from Prentice Hall Press (I believe the author's name is Kim Topley), and he covers printing in the book. He covers awt, which is the original and he covers the printing API's that are included in Java 2D.
I haven't read these parts yet (I just heard about them in the introduction) so I can't tell you much more, but you might want to pick this book up (he claims to give the code for a useful printing method that will work with most applications), or you might want to check out a Java 2D book.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, I've seen these codes. I even got the one from Marty Hall's Java book. All of them have the same problem: they send something to the printer but not the component or component's functions. The interesting thing is that if I send output to a file, the contents of the file are the calls for the printer like the following:
%-12345X@PJL COMMENT HP5SI b=3713
@PJL JOB NAME="JAVA"
@PJL RDYMSG DISPLAY="JAVA"
@PJL SET RET=ON
@PJL SET PAGEPROTECT=AUTO
@PJL SET RESOLUTION=600
@PJL SET ECONOMODE=OFF
@PJL SET OUTBIN=UPPER
@PJL ENTER LANGUAGE=PCL
E&u600D&l7ho2a4d1E*pxyR&l1XE%-12345X@PJL EOJ
@PJL RDYMSG DISPLAY=""
%-12345X
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!