screenshot of HTML pages

HI,

I want do a program that can do the screnshot of HTML pages, I had used the JEditorPane

class for parsing the HTML pages, than I print the content of JEditorPane in a Graphics 2D

than I save the Graphics2D in a file, but sometimes this doesn't work good, I paste the

code:

public void parse(String s, int n) {

// string connect fo JEditorPane

String conStr = s;

try {

// Frame test

JFrame frame = new JFrame("JFrame Screenshot");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// creo JPane

JEditorPane jep = new JEditorPane();

jep.setPreferredSize(new Dimension(1000, 800));

jep.setEditable(false);

JEditorPane.registerEditorKitForContentType("text/html",

"unibo.MyHTMLEditorKit");

JEditorPane.registerEditorKitForContentType("text/HTML",

"unibo.MyHTMLEditorKit");

// jep.registerEditorKitForContentType("text/html",

"MyHTMLEditorKit");

jep.setPage(conStr);

System.out.println(jep.getEditorKit().getClass());

System.out.println(jep.getDocument().getClass());

System.out.println(((HTMLDocument)jep.getDocument()).getAsynchronousLoadPriority());

frame.getContentPane().add(jep, BorderLayout.CENTER);

frame.pack();

frame.setVisible(true);

BufferedImage bi = new BufferedImage(1000, 800,

BufferedImage.TYPE_INT_RGB);

Graphics2D g2d = bi.createGraphics();

jep.paint(g2d);

File fl = new File(n+".jpg");

ImageIO.write(bi, "jpg", fl);

} catch (IOException ioe) {

System.out.print("IOExceptio: ");

ioe.printStackTrace();

}

}

I had modificated the EditorKit

public class MyHTMLEditorKit extends HTMLEditorKit {

private static final long serialVersionUID = 6405646186550395344L;

ViewFactory fac = new SynchHTMLFactory();

class SynchHTMLFactory extends HTMLFactory {

public View create(Element elem) {

Object o = elem.getAttributes().getAttribute(StyleConstants.NameAttribute);

if (o instanceof HTML.Tag) {

HTML.Tag kind = (HTML.Tag) o;

if (kind==HTML.Tag.IMG) {

ImageView v = new ImageView(elem);

v.setLoadsSynchronously(true);

return v;

}

}

return super.create(elem);

}

}

public Document createDefaultDocument() {

Document doc = new MyHTMLDocument();

return doc;

}

public ViewFactory getViewFactory() {

return fac;

}

}

and I modificated the HTMLDocument class for have an asynchronous behavior

public class MyHTMLDocument extends HTMLDocument {

private static final long serialVersionUID = -4377965270758306199L;

public int getAsynchronousLoadPriority() {

return -1;

}

}

My program sometimes works good and anytime works bad anyone have some suggest?

[2898 byte] By [gargiuffo@hotmail.coma] at [2007-10-2 10:49:42]
# 1

1) Use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] when posting code so the code is readable.

2) If you think the problem is that the editor pane isn't loaded completely when you attempt to capture an image of the editor pane then why don't you try adding a Timer to your code to initiate the creation of the image. You could set the Timer to fire "x" seconds after loading the html file. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html]How to Use Swing Timers[/url].

camickra at 2007-7-13 3:07:09 > top of Java-index,Desktop,Core GUI APIs...