2011-04-12 165 views
2

我是新来的用java打印。首先,我开始打印一页数据,一切正常。现在我开始测试如何分页数据。我有一个实现可打印接口的类,并且我有一个int变量(curLine)来保留已经打印的行数和一个可变行以保留最终我想要打印的总数。在public int print(Graphics graphics,PageFormat pageFormat,int pageIndex)方法中,当我使用graphics.drawString(...);方法来绘制一行我增加了度量值(curLine ++),但在打印机中我没有得到我想要的结果。我认为打印方法为特定的indexPage调用了两次,在这两个调用中只打印了数据一个,但curline变量在两个调用中也增加,导致输出中的“丢失”行。java awt print被称为两次问题

更具体的我的测试类是

public class PrintTest implements Printable { 

private final int marginTop = 20; 
private final int marginLeft = 10; 
private Font mainFont = new Font("Verdana", Font.PLAIN, 10); 
private Font bigFont = new Font("Verdana", Font.PLAIN, 14); 
private int lines; 
private int curLine; 

private boolean finish = false; 

public PrintTest() { 
    curLine = 0; 
    lines = 160; 
} 

public static void main(String[] args) { 
    PrinterJob job = PrinterJob.getPrinterJob(); // Use default printer 
    job.printDialog(); 
    PrintTest t = new PrintTest(); 
    job.setPrintable(t); 
    try { 
     job.print(); 
    } catch (PrinterException e) { 
     e.printStackTrace(); 
    } 

} 

@Override 
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 
     throws PrinterException { 
    //if printed all rows then return no_such_page 
    if (finish) { 
     return NO_SUCH_PAGE; 
    } 

    Graphics2D g2 = (Graphics2D) graphics; 

    double pageH = pageFormat.getImageableHeight(); 
    g2.setFont(mainFont); 
    int t = g2.getFontMetrics().charWidth('T'); 
    int lineH = g2.getFontMetrics().getHeight(); 

    double curY; 

    g2.translate(pageFormat.getImageableX() + marginLeft, 
      curY = pageFormat.getImageableY() + marginTop); 

    while ((curY < (pageH - lineH)) && (curLine < lines)) { 
     g2.translate(0, lineH); 
     g2.drawString("Print row number #" + curLine, 0, 0); 
     curY += lineH; 
     curLine++; 
    } 

    if (curLine >= lines) { 
     finish = true; 
    } 

    return PAGE_EXISTS; 

} 

}

,你可以在这里看到的输出https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0ByN6KrI39kzuMTRmZDA5NTMtOGJjZi00ZmRhLThlYWYtMzUwNzE0NTdkMjcz&hl=en&authkey=CLa-svAG

你可以看到,它不是从排发车0

+0

您可以将您的解决方案作为答案发布并接受它。所以要摆脱标题中的[解决]。 – 2011-04-13 12:49:50

+0

为了摆脱持久状态'未解决' – 2011-04-13 13:10:49

+0

@ alberto-zaccagni这就是我想要做的,但我没有看到任何选项发表回复我自己的问题,也许我没有任何选项。所以我决定编辑我的问题,而不是将它添加到评论中。因此,如果存在更好的方式发布我的解决方案(以帮助其他用户解决同样问题),请告诉我。谢谢。 – javment 2011-04-13 13:24:58

回答

1

注意:此解决方案最初由问题OP提供。

public class PrintTest implements Printable { 

    private final int marginTop = 20; 
    private final int marginLeft = 10; 
    private Font mainFont = new Font("Verdana", Font.PLAIN, 10); 
    private Font bigFont = new Font("Verdana", Font.PLAIN, 14); 
    private int lines; 

    private int headerHeighInLines = 5; 
    private int lineH = -1; 
    private double pageH = -1; 
    private int pages = -1; 

    private boolean finish = false; 

    public PrintTest() { 

     lines = 30;// set the total number of rows we want to print 
    } 

    public static void main(String[] args) { 
     PrinterJob job = PrinterJob.getPrinterJob(); // Use default printer 
     job.printDialog(); 
     PrintTest t = new PrintTest(); 
     job.setPrintable(t); 
     try { 
      job.print(); 
     } catch (PrinterException e) { 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { 

     // if printed all pages then no_such_page 
     if (pageIndex >= getPages()) { 
      return NO_SUCH_PAGE; 
     } 

     Graphics2D g2 = (Graphics2D) graphics; 
     AffineTransform defaultTransform = g2.getTransform(); 
     if (pageH == -1) { 

      pageH = pageFormat.getImageableHeight(); 

     } 
     g2.setFont(mainFont); 
     int charW = g2.getFontMetrics().charWidth('T'); 
     if (lineH == -1) { 
      lineH = g2.getFontMetrics().getHeight(); 
     } 

     double curY; 

     g2.translate(pageFormat.getImageableX() + marginLeft, curY = pageFormat.getImageableY() + marginTop); 

     // if we have the first page we can print a title 
     // or a description 
     if (pageIndex == 0) { 
      g2.translate(0, lineH * headerHeighInLines); 
     } 

     int[] res = getNumLines(pageIndex); 

     for (int i = res[0]; i < res[1]; i++) { 
      g2.translate(0, lineH); 
      g2.drawString("Print row number #" + i, 0, 0); 
     } 

     // code to add the page number at footer 
     g2.setTransform(defaultTransform); 
     g2.drawString("page #" + (pageIndex + 1), (int) (pageFormat.getImageableWidth() - charW * 9), 
       (int) (pageFormat.getImageableHeight() - (double) lineH/2)); 

     return PAGE_EXISTS; 

    } 

    /** 
    * 
    * @param pageIndex 
    * @return an int array with contains two values, the num of row to start 
    *   printing and the num of row to finish the printing in the 
    *   specific pageindex 
    */ 
    public int[] getNumLines(int pageIndex) { 

     int[] res = new int[2]; 

     if (pageIndex == 0) { 
      res[0] = 0; 
      res[1] = firstPageLines(); 
      return res; 
     } 
     double tmpH = pageH - 2 * marginTop; 
     double numlines = (double) (tmpH/lineH); 
     res[0] = (int) (firstPageLines() + (pageIndex - 1) * numlines); 
     res[1] = (int) Math.min((res[0] + numlines), lines + 1); 

     return res; 
    } 

    public int firstPageLines() { 
     double tmpPageH = pageH - 2 * marginTop - lineH * headerHeighInLines; 
     // return the minumum of expected lines to print and the total number of 
     // lines 
     return (int) Math.min((tmpPageH/lineH), lines + 1); 
    } 

    /** 
    * 
    * @return int the total number of pages to print 
    */ 
    public int getPages() { 
     if (firstPageLines() >= lines) { 
      return 1; 
     } 
     // we substract the lines in the first page 
     // because may will be less than in the number of 
     // line in the other pages 
     // later we increase the total nubmer of pages with 1 for the first page 
     int tmpLines = lines - firstPageLines(); 

     int[] tmp = getNumLines(1); 
     int linePerPage = tmp[1] - tmp[0]; 
     pages = tmpLines/linePerPage; 
     if (tmpLines % linePerPage > 0) { 
      pages++; 
     } 
     pages++; 

     return pages; 
    } 
} 
0

现在我明白了为什么..问题是,在每一个printpageIndex两次使用,即使你把它在纸张的一面打印。我有几行数据,每当我们输入print()时,我的程序会增加pageBreak(我们开始打印每一页的行索引),因此每页上打印的内容都是错误的(pageBreak递增两次)。这就是为什么在教程示例中,它使用数组来包含这些索引,并使用pageIndex作为索引,因为它确保程序在每次输入循环时都更改数组中的同一个元素,因为pageIndex是相同的。