wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Printing the applet screen Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Printing the applet screen" Watch "Printing the applet screen" New topic
Author

Printing the applet screen

priya gnana
Greenhorn

Joined: Jun 08, 2008
Posts: 14
i want to print the applet screen through the printer....Can anyone help me...?I want to know,what should be to print the applet screen?Please help me,out...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10384
    
    8

Originally posted by priya gnana:
i want to print the applet screen through the printer....Can anyone help me...?I want to know,what should be to print the applet screen?Please help me,out...


By "applet screen" I presume you mean the applet being displayed in the browser.

Using the Robot class, you can capture the screen images.
Then you save them as a temp image file.
Then you invoke the printer API to print them to the printer.

Also check out this thread where Ulf has been most helpful (and patient if I may add so)
http://www.coderanch.com/t/259383/Applets/java/Save-applet-page-as-JPG


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Applet is a subclass of Component, so you can use its print methods. Using java.awt.print, you can create a Printable object that will print your applet. A naive approach:

I say naive because it will only print the part of the applet that fits on the first page. If your applet is larger you will have to print your component multiple times, but each time a different part. You will probably have to use Graphics.translate for that, to "shift your paper under a different part of your applet".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Neeba Rebbaca
Ranch Hand

Joined: Oct 21, 2008
Posts: 159
i want to take print of my applet screen(my whole screen).i enabled the print in the popupmenu item at the right click.In My program,on right click ,according to the option the color of the rectangle changes.My problem is,
1. i could not able take the print out for my full applet screen.
2. if the color of the rectangle changes,the print out is getting only for the default color of the rectangle.


please help me....
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Originally posted by Neeba Rebbaca:
1. i could not able take the print out for my full applet screen.

Do you mean that only a small part of your applet is printed? As in, that part that fits on your screen?

If so, you must print multiple pages. For instance:

See how this code will use multiple pages? You just have to fill in the pseudo code (methods with _ in the name), using the page size, component size and current page. For instance, if your applet would need 3 pages vertically and 2 pages horizontally (for a total of 6 pages), then that's what rows and columns should become. row and column are quite easy after that, with just % and / you can easily calculate the row and column of the current page. Page 3 (starting at 0 of course) would have row == 1 and column == 1.
The translating just needs to put the current "page" on the graphics object correctly.

Also, don't create a new PrinterJob - you are already printing, with the settings specified by the user. Just use those.
Neeba Rebbaca
Ranch Hand

Joined: Oct 21, 2008
Posts: 159
can you explain what 'll the following 'll do...and can you explain what this 'll do?
*calculate_current_column()
* calculate_current_row
* translate_graphics
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

View your applet as a table of pages:

Each cell should fit exactly on one page that is printed.

The first two methods calculate the row number and column number of the page (cell) you are going to print, e.g. 1 and 0. It's most likely something like page / columns and page % columns.

translate_graphics should translate your Graphics object such that the top-left point of the current page in your "table" will be the top-left point of the Graphics object when painting / printing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing the applet screen