We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,680 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Invoke class within same class

Why the method of the following class is getting invoked several times even if the condition is false.Please help me with my code...

public class PRDDayBook {

    int remX;

    public PRDDayBook(int x) {
        remX = x;



        PrinterJob pj = PrinterJob.getPrinterJob();

        PageFormat pf = pj.defaultPage();
        Paper paper = new Paper();
        double margin = 9;
        paper.setImageableArea(margin, margin, paper.getWidth() - margin * 2, paper.getHeight() - margin * 2);
        pf.setPaper(paper);

        pj.setPrintable(new MyPrintable(), pf);
        if (pj.printDialog()) {
            try {
                pj.print();
            } catch (PrinterException e) {
                System.out.println(e);
            }
        }
    }

    class MyPrintable implements Printable {

        public int print(Graphics g, PageFormat pf, int pageIndex) {
            if (pageIndex != 0) {
                return NO_SUCH_PAGE;

            }

            int x = 100;
            int y = 50;

            Graphics2D g2 = (Graphics2D) g;
            g2.setFont(new Font("Serif", Font.PLAIN, 10));
            g2.setPaint(Color.black);

            g2.drawString("S.No", x, 30);
            g2.drawString("Date", x + 30, 30);
            g2.drawString("Particulars", x + 100, 30);
            g2.drawString("Amount Rs", x + 250, 30);
            g2.drawString("Voucher No", x + 300, 30);


            // for printing the items with teir rates and cost
            int hor = DayBookRecords.data.size();
            int ver = DayBookRecords.data.get(0).size();


            double ph = pf.getImageableHeight();
            double pw = pf.getImageableWidth();
            int m=0;

            System.out.println("HOR:" + hor + " VER " + ver);

            for (int i = remX + 1; i < hor; i++) {
                for (int j = 0; j < ver; j++) {

                    g2.drawString("" + DayBookRecords.data.get(i).get(j), x, y);


                    if (j == 0) {
                        x = x + 30;
                    }
                    if (j == 1) {
                        x = x + 70;
                    }
                    if (j == 2) {
                        x = x + 150;
                    }
                    if (j == 3 || j == 4) {
                        x = x + 50;
                    }
                }
                x = 100;
                y = y + 15;
                if (y > ph) {
                    m = i;
                    new PRDDayBook(m);
                    break;
                }
            }


            Rectangle2D outline = new Rectangle2D.Double(pf.getImageableX(), pf.getImageableY(), pf.getImageableWidth(), pf.getImageableHeight());
            g2.draw(outline);
            return PAGE_EXISTS;
        }
    }
}

Here i'm trying to print data to multiple pages in another way... everything works fine but the last page is printed more than one times. I cannot figure out where am i making mistake .Please Help!!

4
Contributors
10
Replies
2 Days
Discussion Span
9 Months Ago
Last Updated
11
Views
47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

Could you include the import statements so the code will compile?

Also I don't see a main() method for testing the code.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

actually its a part of printing module... there is another main method in MainFrame class i don't know how to make it compilable for u

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

that is a class which is called from another main class...i dun know how to show main class

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

The recursive call on line 84 is ringing an alarm bell, but the variables names are so totally useless (what's m supposed to be?) that I can't tell any more than that.

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

what method is invoked several times? how can we answer this if we don't get to see the code where the method (might be/)is called?

stultuske
Industrious Poster
4,489 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 627
Skill Endorsements: 25

I supposed to make that recursive call just to print the remaining data(line no. 84). Everthing is working fine.. i can get as many pages of data as i want to print out.. but the problem is that for multiple pages of data i'm getting their PRINT_OUT more than one time.
I tried to figure out the no. of copies of data in this pattern:

For 1 page data :page no 1 -> 1 time
For 2 page data :page no 1 -> 1 time page no 2 -> 2 times
For 3 page data :page no 1 -> 1 time page no 2 -> 2 times page no 3 -> 4 times

..................................................
this is how i am getting the printouts..how can i solve this problem plz help...

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

Which loop is the break on line 85 intended to exit? Without comments or useful variable names your code is more or less incomprehensible, but at at guess its trying to print one page then recurse to print the next?
Because you are only breaking out of the inner loop maybe the outer loop is continuing to print pages while the recursive call is also printing them, thus giving you more and more copies of the later pages?

JamesCherrill
... trying to help
Moderator
8,667 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,476
Skill Endorsements: 33

on line 85 that break is to avoid the further prompt for the new PRDDayBook(i)...which wud have been called for everypossible value of i from the 1st loop.... I'm still stucked with the same problem i am still getting multiple copies of final pages...wud u please suggest me the alternative ways. Plzzz help

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

Can you post code for testing? The posted code does not have a main() method.
Something small that will compile, execute and show the problem.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

thanx for all i solve the problem :) cheers

47pirates
Junior Poster
129 posts since Dec 2009
Reputation Points: 19
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0936 seconds using 2.74MB