1

I have tried to devise a way to get a screenshot of a Java applet running in a browser, but I can't seem to get it working. I managed successfully to use cutycapt to get screenshots fine from "normal" websites, but I soon found out that qtwebkit which it seems to rely on for the rendering does not support java. I also tried IEcapt thinking that it would somehow inherit the Java rendering capabilities of IE on the system, but it does not work. Flash also does not seem to be working in IEcapt, and it has no flags for enabling plugins, so I am assuming the functionality is not there either.

Does anyone have any thoughts on how you could render something like an /index.jsp to an image from a Windows or Linux command line?

pzkpfw
  • 494
  • 2
  • 16

3 Answers3

3

Selenium webdriver might be useful here:

http://docs.seleniumhq.org/projects/webdriver/

It is used primarily for test automation but it might be helpful.

It could be used, for example, like this:

import org.openqa.selenium.*;

WebDriver driver = new FirefoxDriver();  // create a Firefox webdriver instance
driver.get("http://www.google.com/");  // navigate to page
File screenshotFile = ((Screenshot)driver).getScreenshotAs(file); // capture screenshot
                                                                  // and save to a file

// here you can trigger any necessary actions on the website:
Webelement element = driver.findElement(By.name("q")).sendKeys("xxxxx");
element.click();
new WebDriverWait(driver, 10)).until(ExpectedConditions.titleContains("xxxxx"));

// and capture a new screenshot once the content has changed
File xxxScreenshotFile = ((Screenshot)driver).getScreenshotAs(file);
OlgaMaciaszek
  • 2,741
  • 1
  • 23
  • 26
  • I'm somewhat confused about the range of different Selenium products. Is this simply the WebDriver? Would it work on from a CLI on a Linux server? Could you be so kind as to elaborate a little bit on how you would set that up? I appreciate the pointer but as I know nothing of Selenium I can't really tell if this will do the trick or not – pzkpfw Aug 05 '13 at 09:24
  • Selenium Webdriver is a more recent approach. There is also Selenium Remote Control which was created before, but it is now considered deprecated. In order to use Webdriver with Java, you should download and run Selenium Standalone Server and import the library. You can find a lot of helpful documentation here: http://www.seleniumhq.org/. You can run webdriver from console in Linux. – OlgaMaciaszek Feb 07 '15 at 22:30
0

Have you tried using java.awt.Robot?

    Rectangle rect = yourGragphicsConfiguration.getBounds();
    BufferedImage image = new Robot().createScreenCapture(rect);

If you know the position of your applet you might be able to get it with

    BufferedImage dest = image.getSubimage(appletX, appletY, appletHeight, appletWidth);
Lai
  • 462
  • 4
  • 22
  • Sorry, I should have specified that modifying the app in any way is not an option, I am looking for software to run the java applet and give me the graphical output (and I'm not looking to build the software myself) – pzkpfw Aug 02 '13 at 09:52
-1

You can take a screenshot of Swing/AWT component.

This can be done in 2 ways. In both cases the component must be visible.

Without the robot use:

BufferedImage image = new BufferedImage(component.getWidth(), 
component.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    component.paint(g);

With the robot use:

In this case the screenshot of area in which this component is situated will be made. That is, if the component overlaps another application window then the screenshot will contain an area of this another window.

Point point = new Point(0, 0);
    SwingUtilities.convertPointToScreen(point, component);
    Rectangle region = component.getBounds();
    region.x = point.x;
    region.y = point.y;
    BufferedImage image= new Robot().createScreenCapture(region);

This information is taken from the article: Frequently Asked Questions during Java applet development

JazzTeam
  • 49
  • 3