2016-04-07 27 views
0

JPanelを印刷しようとしていますが、作業コードがあります。しかし、問題は、それは4つの側面から広範なmaginとJPanelを印刷している... Plzの誰かが私のコードをチェックします。そして私に提案してください、どのようにして最小限のマージンでa4サイズの用紙に適したコードを変更できますか? 0.5インチ縦、横方向に1インチ:そしてactioperformedJavaコードで印刷する余白を設定します

btnNewButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     PrinterJob pjob = PrinterJob.getPrinterJob(); 
     PageFormat preformat = pjob.defaultPage(); 
     preformat.setOrientation(PageFormat.PORTRAIT); 
     PageFormat postformat = pjob.pageDialog(preformat); 

     if (preformat != postformat) { 
      RepairBill r=new RepairBill(); 
      r.setVisible(false); 
      pjob.setPrintable(new Printer(r.r), postformat); //r.r is the object for JPanel 
      if (pjob.printDialog()) { 
       try { 
        pjob.print(); 
       } catch (PrinterException e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
          } 
      } 
     } 
    }); 

コードプリンタクラス用

public static class Printer implements Printable { 
    final Component comp; 

    public Printer(Component comp) { 
        this.comp = comp; 
    } 

    @Override 
    public int print(Graphics g, PageFormat format, int page_index) throws PrinterException { 
     if (page_index > 0) { 
      return Printable.NO_SUCH_PAGE; 
     } 

     // get the bounds of the component 
     Dimension dim = comp.getSize(); 
     double cHeight = dim.getHeight(); 
     double cWidth = dim.getWidth(); 

     // get the bounds of the printable area 
     double pHeight = format.getImageableHeight(); 
     double pWidth = format.getImageableWidth(); 
     double pXStart = format.getImageableX(); 
     double pYStart = format.getImageableY(); 
     double xRatio = pWidth/cWidth; 
     double yRatio = pHeight/cHeight; 
     Graphics2D g2 = (Graphics2D) g; 
     g2.translate(pXStart, pYStart); 
     g2.scale(xRatio, yRatio); 
     comp.paint(g2); 
     return Printable.PAGE_EXISTS; 
    } 
} 

plz see the screenshot of my output

+1

コードの中心( 'translate')と' scale's(比例していません!)。単位が1/72インチの場合、絶対マージンを設定できますが、少なくともgetImageableX/Yである必要があります。 Math.min(xRatio、yRation)の両方の向きでスケールします。 –

+0

申し訳ありませんJavaでの印刷について十分なアイデアはありません。あなたは教えてください、私はこのコードで何を変えるべきですか? – user6172452

+0

私はそれをプログラムしました。あなたが見るように大きな違いはありません。 –

答えて

0

ため、このコードは、私は余白が追加されました。

マージンが印刷可能であることを確認してください(1 mmのマージンは実現できません)。

ページサイズは物理的なデバイスの問題であり、誰かがA4やUS Letterを持っているかどうかは関係ありません。 1つはPrintJobで設定し、APIはjavadocをネットで調べます。

スケーリングは比例して行われます。

// get the bounds of the component 
    Dimension dim = comp.getSize(); 
    double cHeight = dim.getHeight(); 
    double cWidth = dim.getWidth(); 

    // get the bounds of the printable area 
    double pHeight = format.getImageableHeight(); 
    double pWidth = format.getImageableWidth(); 
    double pXStart = format.getImageableX(); 
    double pYStart = format.getImageableY(); 
    double paperHeight = format.getHeight(); 
    double paperWidth = format.getWidth(); 

    final int INCH = 72; 
    double vertMargin = 0.5 * INCH; 
    double horMargin = 1 * INCH; 

    // Check whether margins are enough, are printable. 
    if (paperWidth - 2 * horMargin < pWidth) { 
     pWidth = paperWidth - 2 * horMargin; 
     pXStart = horMargin 
    } 
    if (paperHeight - 2 * vertMargin < pHeight) { 
     pHeight = paperHeight - 2 * vertMargin; 
     pYStart = vertMargin; 
    } 

    // Determine scaling 
    double xRatio = pWidth/cWidth; 
    double yRatio = pHeight/cHeight; 
    double ratio = Math.min(xRatio, yRation); // Proportional 

    Graphics2D g2 = (Graphics2D) g; 
    g2.translate(pXStart, pYStart); 
    g2.scale(ratio, ratio); 
    comp.paint(g2); 
+0

申し訳ありません。しかし、私は上記のコードをコピーしました..しかし、私に適切な出力を与えていません。 – user6172452

+0

'g2.scale(xRatio、yRatio);'を試してみてください。おそらくコンポーネントがページよりも大きくなります。申し訳ありませんがうまくいきませんでした。 –

+0

さん、私は自分の投稿を編集しました。私の出力のスクリーンショットをチェックアウトします。これはあなたのコードをコピーしてg2.scale(xRatio、yRatio)を変更した後のものと同じ出力です。 – user6172452

関連する問題