开发者

java awt print called twice problem

I'm new to printing with java. First I start to print one page data and all was ok. Now I start to test how to paginate my data. I have a class that implements the printable interface and I have an int variable (curLine) to keep the number of rows I have already print and an variable lines to keep the total number I want to print finally. In public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) method when I use the graphics.drawString(...); method to draw a row I increase the value of the metric(curLine++) but in the printer I didn't get the results I want.I think that the print methold called twice for a specific indexPage, in that two calls the data printed only one, but the curLine variable increase in the two calls too with result to "lost" rows from the output.

More specific my test class is

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) {
      开发者_JAVA百科  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;

}

}

and you can see the output here https://docs.google.com/viewer?a=v&pid=explorer&chrome=true&srcid=0ByN6KrI39kzuMTRmZDA5NTMtOGJjZi00ZmRhLThlYWYtMzUwNzE0NTdkMjcz&hl=en&authkey=CLa-svAG

You can see that it wasn't start from row 0


Note: this solution was originally provided by the question 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;
    }
}


Now I understand why.. the question is, that every pageIndex in print will be used twice, even you make it print in one side of the paper. I have several lines of data, and my program increments the pageBreak (line indexes where we start to print every page) every time we enter print(), so the content printed on every page is wrong (pageBreak incremented twice). That's why in the tutorial example, it uses an array to contains these indexes and use pageIndex as index, because it ensures the program to change the same element in the array every time you enter the loop, because pageIndex is the same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜