• Post Reply Bookmark Topic Watch Topic
  • New Topic

jpanel printing

 
marko stone
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used this code for printing JPanel contents
all labels and tables in it printing WHITE COLOR. WHY ???
thanks

class JPanelMy extends JPanel implements Printable
{
JPanelMy(MainWindow src)
{
super();
this.src=src;
DateWork dw=new DateWork();
daydate=new JLabel(dw.getDayDate());
titlename=new JLabel();
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
setBackground(Color.white);
}
...
public void printData()
{
PrinterJob pj=PrinterJob.getPrinterJob();
pj.setPrintable(this);
pj.printDialog();
try
{
pj.print();
}
catch (PrinterException pe)
{
System.out.println("Printer Exception");
}
}
...
public int print(Graphics g,PageFormat pageFormat,int pageIndex) throws PrinterException
{
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.black);

int fontHeight=g2.getFontMetrics().getHeight();
System.out.println("fontHeight="+fontHeight);
().getDescent();
//System.out.println("fontDescent="+fontDescent);
pageFormat.setOrientation(PageFormat.LANDSCAPE);
double pageHeight=pageFormat.getImageableHeight()-fontHeight;
System.out.println("pageHeight="+pageHeight);
double pageWidth=pageFormat.getImageableWidth();
System.out.println("pageWidth="+pageWidth);
Dimension d0;
d0=getSize();
double panelWidth=(double)d0.width;
System.out.println("panelWidth="+panelWidth);
double panelHeight=(double)d0.height;
System.out.println("panelHeight="+panelHeight);
double scale=1;
System.out.println("scale="+scale);
if (panelWidth>=pageWidth)
{
scale=pageWidth/panelWidth;
System.out.println("scale="+scale);
}
double panelWidthOnPage=panelWidth*scale;
System.out.println("panelWidthOnPage="+panelWidthOnPage);
double oneRowHeight=fontHeight;
System.out.println("oneRowHeight="+oneRowHeight);
int numRowsOnAPage=(int)(pageHeight/oneRowHeight);
System.out.println("numRowsOnAPage="+numRowsOnAPage);
double pageHeightForPanel=oneRowHeight*numRowsOnAPage;
System.out.println("pageHeightForPanel="+pageHeightForPanel);
int totalNumPages=(int)Math.ceil((double)panelHeight/pageHeight);
System.out.println("totalNumPages="+totalNumPages);
totalNumPages=1;
if (pageIndex>=totalNumPages)
{
return (NO_SUCH_PAGE);
}
g2.translate(pageFormat.getImageableX(),pageFormat.getImageableY());
g2.translate(0f,-pageIndex*pageHeightForPanel);

g2.setClip(0,(int)pageHeightForPanel*pageIndex,(int)Math.ceil(panelWidthOnPage),(int)Math.ceil(pageHeightForPanel));

g2.scale(scale,scale);
paint(g2);
g2.scale(1/scale,1/scale);
g2.translate(0f,pageIndex*pageHeightForPanel);
return (PAGE_EXISTS);
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!