• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Neef Help with Printing

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to do some printing in my Java program....
this is what I am doing.... I pass an ArrayList to the class performing the print ... and then create a graphics object using the data in the ArrayList..... And print it.....

public int print(Graphics g, PageFormat pageFormat, int page) throws
PrinterException
{
int i;
Graphics2D g2d = (Graphics2D) g;
if (page == 0)
{
g2d.translate (pageFormat.getImageableX(), pageFormat.getImageableY());
g2d.setFont(new java.awt.Font("Microsoft Sans Serif", 0,11));
int x =0;
int y= 45;
// Printing the data from the Array list.
// The way I am formatting to print.. I can print only 16 records on one page.
So it needs to go to the next page after 16 records.
for(i=0;i<printArray.size();i++)
{
// trying to position the data into tow columns .. with all odd rows on one side and even on another.
if(checknum_odd_even(i))
{
x =0;
// trying to position the data.
Object[] prnObject = (Object[]) printArray.get(i);
g2d.drawString(prnObject[0].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[1].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[2].toString(), x, y);
y = y + 45;
}
else
{
// trying to position the data.
x=300;
y=y-75;
Object[] prnObject = (Object[]) printArray.get(i);
g2d.drawString(prnObject[0].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[1].toString(), x, y);
y = y + 15;
g2d.drawString(prnObject[2].toString(), x, y);
y = y + 45;
}
}
return (PAGE_EXISTS);
}
else return (NO_SUCH_PAGE);
}
}

This works fine.. as long as it is limited to only one page .... but i don't know how to print multiple pages ,,,
for example .. if there are 30 items in the array list that I am sending to the class ... it should print the first 16 on page one and the next 14 on page two .... .. I went through the javax.print .. but did not understand much .. will really appreciate if someone could help me .. give me an idea as per what needs to be done ..
Thanks,
Sandeep
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The print method has the following signature:

You need to consider the page variable. The printing engine will keep calling this method with ever-increasing values for page until you return NO_SUCH_PAGE. You have to figure out how many items will fit on a page (PageFormat gives you the bounds) and draw the items on the appropropriate page. Be forewarned, the print method may be called several times for the same page number.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic