Link to home
Start Free TrialLog in
Avatar of stakor
stakorFlag for United States of America

asked on

Modify a website 'screen capture' program, to remove javascript 'errors'

The program below is designed to do a 'screen capture' of a whole webpage. It uses a text area to accomplish this. The problem is that if the webpage has javascript on it, it will show up as errors. I am looking to modify this program so that the errors are not displayed. If the way to do this is to not use a text area, this is fine. If using a browser would be better, that is ok as well. In the end, I just need the image of the whole web page. The test url (CNN) is just a popular page that has javascript on it. If you could help out, it would be greatly appreciated. If you have any questions, please let me know.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.net.*;
 
public class SavingAScrollingComponent
{
    public SavingAScrollingComponent()
    {
        JEditorPane textArea = getTextArea();
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(getUIPanel(textArea), "North");
        f.getContentPane().add(new JScrollPane(textArea));
       
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
       

    }
 
    private void save(Component c)
    {
        int w = c.getWidth();
        int h = c.getHeight();
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        c.paint(g2);
        g2.dispose();
        File file = new File("scrollingComponent.png");
        try
        {
            ImageIO.write(image, "png", file);
        }
        catch(IOException ioe)
        {
            System.err.println("write: " + ioe.getMessage());
        }
    }
 
    private JPanel getUIPanel(final JEditorPane ta)
    {
        JButton save = new JButton("save");
        save.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                save(ta);
            }
        });
        JPanel panel = new JPanel();
        panel.add(save);
        return panel;
    }
 
    private JEditorPane getTextArea()
    {
        JEditorPane ta = new JEditorPane();
        ta.setMargin(new Insets(0,5,0,5));
        try
        {
       
          URL file = new URL("http://www.cnn.com/");
             ta.setPage(file);
        }
        catch(IOException ioe)
        {
            System.err.println("read: " + ioe.getMessage());
        }
        return ta;
    }
 
    public static void main(String[] args)
    {
        new SavingAScrollingComponent();
    }
}
Avatar of durgaprasad_j
durgaprasad_j

hi stakor,
  using JEditorPane, you cannot render javascript.
 
 Try using webrender. http://www.webrenderer.com/ [but it is shareware ]

  see this link http://www.jguru.com/faq/view.jsp?EID=25662 

       http://www.ii.uib.no/~alexey/jb/
       http://www.netcluesoft.com/desktop/desktop.php3?sub=deskload

Hope this helps
Avatar of stakor

ASKER

I fear that I neglected to mention how much of a newbie I am to Java. I see that the general idea here is to replace the JEditorPane with a java web browser. But, that is about as far as I get...

In the example that you provide, I see that there is a new object called SormBase, and that it is using Swing. Now keep in mind I only know that there are books writien on swing, but I know nothing about it. (Actually I think it is a method for communicating between objects? - I really don't know) After that there is a new JPannel, and a viewport is created via Storm Base's Storm Panel.

I guess my major problem here (other than not knowing much Java) is that I don't fully understand how to replace the JEditorPane in 'my' application. I am not even certain where I need to place the ICE browser's jb.class in relation to my application. Does it only need to be visable in the path, or does it need something more specific? Sorry, should have mentioned I am very new to Java.
ASKER CERTIFIED SOLUTION
Avatar of durgaprasad_j
durgaprasad_j

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial