Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2003

Tip: Looking for answers? Try searching our database.

HTMLEditorKit to Render Off-Screen Images?

Thread view: 
Lukas Bradley - 05 Dec 2003 22:31 GMT
Is it possible to create off-screen images that render HTML pages?  In other
words, can I feed an HTML page to some text.swing object, and have it save
out a JPEG for me?

Thanks for pointing me in a direction-- any direction, really.

Lukas
Chris Smith - 05 Dec 2003 23:27 GMT
> Is it possible to create off-screen images that render HTML pages?  In other
> words, can I feed an HTML page to some text.swing object, and have it save
> out a JPEG for me?

Sure it's possible.  I just copied the following code from Design-a-
Course Developer, which should give the basic idea:

   Image offscreen = createImage(width, height);
   Graphics g2 = offscreen.getGraphics();

   editorPane.paint(g2);
   g2.dispose();

   previewImage = offscreen.getScaledInstance(
       viewWidth, viewHeight, Image.SCALE_SMOOTH);
   offscreen.flush();

That creates a temporary image to be drawn onto a component.  Instead of
using Component.createImage, you'll want to create a BufferedImage, then
use ImageIO to save it as a JPEG file.

One thing I didn't show there is that I use a custom subclass of
HTMLEditorKit, which in turn uses a custom ViewFactory, which calls
setLoadsSynchronously on ImageView instances to ensure that the
resulting image doesn't exclude images in the page.  I think that's the
only major change I made, but I don't recall everything.  You may have
to dink around with things to get the whole page.  As a worst-case hack,
you could simply add a Thread.sleep to give the page time to load
completely.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Lukas Bradley - 05 Dec 2003 23:42 GMT
> One thing I didn't show there is that I use a custom subclass of
> HTMLEditorKit, which in turn uses a custom ViewFactory, which calls
[quoted text clipped - 4 lines]
> you could simply add a Thread.sleep to give the page time to load
> completely.

Would you mind sharing that code?  It would keep me from dinking, and
removes the worst-case hack scenario.

Lukas
Chris Smith - 06 Dec 2003 00:48 GMT
> Would you mind sharing that code?  It would keep me from dinking, and
> removes the worst-case hack scenario.

Unfortunately, the custom HTMLEditorKit subclass does a lot of other
things as well... and I don't have time to separate the parts that
matter from the parts that don't.  I described the basic steps to
implementing the synchronous image loading, which is the biggest deal.  
Other problems may come from background images and the like.

It would look something like this (entirely from memory, so let me know
if you can't fix the mistakes I'm sure to make):

class SyncEditorKit extends HTMLEditorKit
{
   public ViewFactory getViewFactory()
   {
       final ViewFactory superFactory = super.getViewFactory();
       return new ViewFactory() {
           public View getView(Element e)
           {
               View v = superFactory.getView(e);
               if (v instanceof ImageView)
               {
                   ((ImageView) v).setLoadsSynchronously(true);
               }

               return v;
           }
       };
   }
}

You'd then have to do the following to the JEditorPane:

   editorPane.setEditorKitForContentType(
       "text/html",
       new SyncEditorKit());

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Lukas Bradley - 06 Dec 2003 00:11 GMT
>     Image offscreen = createImage(width, height);
>     Graphics g2 = offscreen.getGraphics();
[quoted text clipped - 5 lines]
>         viewWidth, viewHeight, Image.SCALE_SMOOTH);
>     offscreen.flush();

To be more specific, you say you use a custom extended HTMLEditorKit.  So, I
assume this code is within that class.  However, createImage() is not
available there.

So what type of object is this code from?

Lukas
Chris Smith - 06 Dec 2003 00:43 GMT
> >     Image offscreen = createImage(width, height);
> >     Graphics g2 = offscreen.getGraphics();
[quoted text clipped - 11 lines]
>
> So what type of object is this code from?

Oh, no!  This is actually defined from within a Component subclass.  In
my specific application, the Component will later be drawing the image
to the screen, so that makes sense.  I thought I mentioned that you'd
want to use "new BufferedImage(width, height)" instead.

Signature

www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.