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

Printing  RSS feed

 
arun iyer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I am printing the jtable in the following method. It prints fine but I am not able to print a graph which i have plotted on the getcontentpane(). I would be grateful, if u could kindly throw some light on the matter.

if (ae.getSource()==test)
{
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(tides1.this);
pj.printDialog();
try{
pj.print();
}catch (Exception PrintException) {}
}
public int print(Graphics g, PageFormat pageFormat,
int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.black);
int fontHeight=g2.getFontMetrics().getHeight();
int fontDesent=g2.getFontMetrics().getDescent();
//leave room for page number
double pageHeight =
pageFormat.getImageableHeight()-fontHeight;
double pageWidth = pageFormat.getImageableWidth();
double tableWidth =
(double) jt.getColumnModel(
).getTotalColumnWidth();
double scale = 1;
if (tableWidth >= pageWidth) {
scale = pageWidth / tableWidth;
}
double headerHeightOnPage=
jt.getTableHeader().getHeight()*scale;
double tableWidthOnPage=tableWidth*scale;
double oneRowHeight=(jt.getRowHeight()+
jt.getRowMargin())*scale;
int numRowsOnAPage=
(int)((pageHeight-headerHeightOnPage)/oneRowHeight);
double pageHeightForTable=oneRowHeight*numRowsOnAPage;
int totalNumPages= (int)Math.ceil((
(double)jt.getRowCount())/numRowsOnAPage);
if(pageIndex>=totalNumPages) {
return NO_SUCH_PAGE;
}
g2.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
g2.drawString("Page: "+(pageIndex+1),(int)pageWidth/2-35,
(int)(pageHeight+fontHeight-fontDesent));//bottom center
g2.translate(0f,headerHeightOnPage);
g2.translate(0f,-pageIndex*pageHeightForTable);
//TODO this next line treats the last page as a full page
g2.setClip(0, (int)(pageHeightForTable*pageIndex),(int)
Math.ceil(tableWidthOnPage),
(int) Math.ceil(pageHeightForTable));
g2.scale(scale,scale);
jt.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);
jt.getTableHeader().paint(g2);//paint header at top
return Printable.PAGE_EXISTS;
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!