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?

