2014-06-05 107 views
0

我想打印一個JPanel是在應用程序中,應用程序內的JPanel顯示下方(注意水平和垂直滾動條):如何打印整個的JPanel Java中

The Application 當應用程序運行它的過程中,面板的高度變得非常長,但是當我打印它時,它不打印整個寬度和高度。

此外,面板在紙張中的位置與左側對齊,而不是中心。 下圖顯示了面板如何顯示在紙上,這看起來很糟糕,所以請我需要幫助。

1)如何打印整個面板?

2)如何將它定位在紙張上?

The Paper

這裏是我的代碼:

public void printWork() 
{ 
    PrinterJob pj = PrinterJob.getPrinterJob(); 
    pj.setJobName(" Opt De Solver Printing "); 

    pj.setPrintable(new Printable() 
    { 
     @Override 
     public int print(Graphics pg, PageFormat pf, int pageNum) 
     { 
      if(pageNum > 0) 
       return Printable.NO_SUCH_PAGE; 

      Graphics2D g2 = (Graphics2D)pg; 
      g2.translate(pf.getImageableX(), pf.getImageableY()); 
      MainAppPanel.this.paintAll(g2); 
      /* I've tried the following commented codes but they don't work */ 
      //MainAppPanel.this.printAll(g2); 
      //MainAppPanel.this.print(g2); 
      //MainAppPanel.this.print(g2); 
      return Printable.PAGE_EXISTS; 
     } 
    }); 
    if(pj.printDialog() == false) 
     return; 
    try 
    { 
     pj.print(); 
    } 
    catch(PrinterException xcp) 
    { 
     xcp.printStackTrace(System.err); 
    } 
} 
+0

看一看[此示例](http://stackoverflow.com/questions/12764634/printing-a-jframe和它的組件/ 12765916#12765916)和[此示例](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on -the-print/22244116#22244116)和[this example](http://stackoverflow.com/questions/22241711/setting-print-size-of-a-jlabel-and-put-a-jradiobutton-on-the -print/22242658#22242658)和[this example](http://stackoverflow.com/questions/22058738/print-jlabels-icon-in-a-printer-using-a-button/22059079#22059079) – MadProgrammer

+0

和[這個例子](http://stackoverflow.com/questions/17904518/fit-scale-jcomponent-to-page-being-printed/17961911#17961911) – MadProgrammer

+0

另外,瞭解改變「活」組件的位置和大小會影響它在屏幕上的繪製效果 – MadProgrammer

回答

0

我知道這是晚3年,但現在以供參考:

頁面上的形象定位您所定義的左邊的角落在這裏: g2.translate(pf.getImageableX(),pf.getImageableY());

所以,如果你想獲得其100分的權利和150點的下跌,那麼你可以這樣做:

g2.translate(pf.getImageableX()+ 100,pf.getImageableY()+ 150 );

另外如果你想使之適合更好地利用縮放圖像:

g2.scale(1,1);

其中1表示100%,因此0.5將意味着50%和1.5。將150%