• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Printing - Fit to Page

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there. I have some applications that I want to enable users to do print screens on. I have been able to get the screen to print, however part of the screen gets cut off when printing on 8-1/2 x 11. Is there any way that I can automatically tell it to "Fit to Page" when printing? This is currently what I'm using to print:



Another thing.... I got this code from the web, but I don't think I really understand it. At what point does the paint(Grahpics g,PageFormat pf,int pageIndex) method ever get called? I had put a System.out.println in this method and it never showed up when I ran the program.
Thanks for any help!
[ June 27, 2008: Message edited by: Jennifer Sohl ]
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jennifer Sohl:
Hi there. I have some applications that I want to enable users to do print screens on. I have been able to get the screen to print, however part of the screen gets cut off when printing on 8-1/2 x 11. Is there any way that I can automatically tell it to "Fit to Page" when printing? This is currently what I'm using to print:

...

Another thing.... I got this code from the web, but I don't think I really understand it. At what point does the paint(Grahpics g,PageFormat pf,int pageIndex) method ever get called? I had put a System.out.println in this method and it never showed up when I ran the program.
Thanks for any help!

[ June 27, 2008: Message edited by: Jennifer Sohl ]



You need to apply a scale transform on the Graphics. After you call translate, call scale, which takes doubles for x,y that represent the scale factor. You can think of it as a percentage. 1.0 means no change, 2.0 means twice as big, .5 means half size, etc.

You'll have to calculate how much you need to scale. Get the size of you component, compare to the size of your imageable area to compute a ratio, and that will tell you the scale factor. You'll want to keep the same aspect ratio though, so you want to pass the same values for x,y to scale, so just use the smaller of the ratios of the width,height of your component.

The printer job calls your paint() method whenever it needs it to paint a page. It can call it multiple times per page, so don't change any object state in your paint() method.

Printing is done internally on a background thread, so that's why you don't see any println output. It's a pain in the you-know-what to debug printing problems. I've taken to actually using drawString commands, so I can just paint debug messages to the Graphics when I'm having trouble. When debugging I always just do a print-preview from my OS print dialog so I don't waste paper.

Hope this helps.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!