import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class WebCapture {
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.err.println(
"Usage: java " +
WebCapture.class.getName() +
" url file");
System.exit(1);
}
new WebCapture().go(
new URL(args[0]),
new File(args[1]));
}
public void go(URL url, File file) throws IOException {
JEditorPane editor = new JEditorPane();
EditorKit ek = new HTMLEditorKit() {
public ViewFactory getViewFactory() {
return new HTMLEditorKit.HTMLFactory() {
public View create(Element element) {
View view = super.create(element);
if (view instanceof ImageView) {
((ImageView)view).setLoadsSynchronously(true);
}
return view;
}
};
}
public Document createDefaultDocument() {
AbstractDocument document =
(AbstractDocument)super.createDefaultDocument();
document.setAsynchronousLoadPriority(-1);
return document;
}
};
editor.setEditorKit(ek);
editor.setPage(url);
editor.setSize(editor.getPreferredSize());
javax.imageio.ImageIO.write(
getImage(editor),
getFormatName(file.getName()),
file);
}
private RenderedImage getImage(Component component) {
Dimension dim = component.getSize();
BufferedImage bi = new BufferedImage(
dim.width, dim.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
component.paint(g2);
g2.dispose();
return bi;
}
private String getFormatName(String fileName) {
int n = fileName.lastIndexOf(".");
if (n == -1) {
return null;
}
return fileName.substring(n+1);
}
}