can anyone tell me how here the count of st is incrementing like this 2,4,6,8,10 and not like 1,2,3,4,5

how could i get the count of st to increment as 1,2,3,4,5

PrinterJob printJob = PrinterJob.getPrinterJob();
        Book book = new Book();
        for(int i=0;i<5;i++)
        {
          book.append(new IntroPage(), printJob.defaultPage());  
        }

IntroPage class is given below

private final static int POINTS_PER_INCH = 72;

private class IntroPage implements Printable 
{
     @Override
     public int print(Graphics g, PageFormat pageFormat, int page) 
  {
      [B]st++;[/B]
      //--- Create the Graphics2D object
      Graphics2D g2d = (Graphics2D) g;

      //--- Translate the origin to 0,0 for the top left corner
      g2d.translate(pageFormat.getImageableX(), pageFormat.getImageableY());

      //--- Set the default drawing color to black
      g2d.setPaint(Color.black);

      //--- Draw a border arround the page
      Rectangle2D.Double border = new Rectangle2D.Double(0, 0, pageFormat.getImageableWidth(), pageFormat.getImageableHeight());
      g2d.draw(border);
     
      //--- Print the title
      [B]String titleText = "Printing in Java Part "+st+", Example 4";[/B]
      Font titleFont = new Font("helvetica", Font.BOLD, 18);
      g2d.setFont(titleFont);

      //--- Compute the horizontal center of the page
      FontMetrics fontMetrics = g2d.getFontMetrics();
      double titleX = (pageFormat.getImageableWidth() / 2)- (fontMetrics.stringWidth(titleText) / 2);
      double titleY = 3 * POINTS_PER_INCH;
      g2d.drawString(titleText, (int) titleX, (int) titleY);

      return (PAGE_EXISTS);
    }
}

for a loop,

for (int i=0; i<5; i=i+2)

still i'm not getting the increment as 1,2,3,4

Where in your code do you see the value of the variable as it is incremented?
Add a println to show its value every time it is incremented.
Is the method being called two times when you think it is being called once?

ya 2 times the method is calling.........i don't know where that is calling

may be the this code that i use may creates the problem

PrinterJob printJob = PrinterJob.getPrinterJob();
        Book book = new Book();
        for(int i=0;i<5;i++)
        {
          book.append(new IntroPage(), printJob.defaultPage());  
        }
if (printJob.printDialog()) {
       try {
        printJob.print();
      } catch (Exception e) {e.printStackTrace();
      }
    }

What is the value of the page variable every time it is called?

The API doc for Printable includes this:

For correct printing behaviour, the following points should be observed:
The printing system may request a page index more than once...
... the Printable should expect multiple calls for a page index and that page indexes may be skipped, when page ranges are specified by the client, or by a user through a print dialog.

So the fact that it's called twice may be just how it works.

This all means that the approach of incrementing the page number when print is called is definitely wrong.
You could possibly pass the page number to the constructor of IntroPage and keep that as an attribute of the page, so the page number is created when the page is created and then doesn't change.

page value is 0,0,1,1,2,2,3,3,4,4

so if then how will i do the increment within the function

public int print(Graphics g, PageFormat pageFormat, int page)
{
}

What do you want to increment?
What does the page variable contain?

Since print can be called any number of times for any page you can't just do the increment in the print method. I would decide the page numbers when creating the pages, as per my previous post.

This might work right............

public int print(Graphics g, PageFormat pageFormat, int page)
 {
            
       k1=page;
       if(k2==k1)
       {
           k2++;
       }
}

Can you explain the logic of you post. What is it supposed to be doing?

I wondered that too.

Can you explain the logic of you post. What is it supposed to be doing?

Since that i want the increment of st for four pages of the book should be like 1,2,3,4,i use this code mentioned

public int print(Graphics g, PageFormat pageFormat, int page)
 {
            
       k1=page;
       if(k2==k1)
       {
           k2++;
           st=k2;
       }
       :
       :
}

otherwise i will get the increment as 1,2,3,4,5,6,7,8 as JamesCherrill said that

The printing system may request a page index more than once...
... the Printable should expect multiple calls for a page index and that page indexes may be skipped, when page ranges are specified by the client, or by a user through a print dialog.

This is probably a dumb question, but here goes anyway...
Why not just use the page number (index+1) that's passed into print as its third parameter? Isn't that the number that you want to print?
(If not, what EXACTLY is "st" intended to be?)

k1=page;
       if(k2==k1)
       {
           k2++;
           st=k2;
       }

What is the purpose of this code?
What values are the variables k1 and k2 supposed to hold?

k1=page;
       if(k2==k1)
       {
           k2++;
           st=k2;
       }

What is the purpose of this code?
What values are the variables k1 and k2 supposed to hold?

variable k1 holds the value of page,since the print function is been called twice.....the page value is like this 0,0,1,1,2,2,3,3

so in the above code when the page value is 0 it is been stored in variable k1,where k2 at declaration is initialized as 0,

hence when page=0 ,k1=0 we have k2=0,since k1==k2 therefore k2=1 st=1
when page=0 ,k1=0 we have k2=1,since k1!=k2 therefore k2=1 st=1

when page=1 ,k1=1 we have k2=1,since k1==k2 therefore k2=2 st=2
when page=1 ,k1=1 we have k2=2,since k1!=k2 therefore k2=2 st=2

What happens if the user requests 3 copies of pages 4,5? Your algorithm only works for one specific sequence.

This is my last shot in this topic:

You don't need st or k1 or k2, and anything that increments inside the print method is certain to fail for most print sequences.
All you need for your titleText is page+1;

What happens if the user requests 3 copies of pages 4,5? Your algorithm only works for one specific sequence.

This is my last shot in this topic:

You don't need st or k1 or k2, and anything that increments inside the print method is certain to fail for most print sequences.
All you need for your titleText is page+1;

understood, thanks for your support

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.