pzkpfw
pzkpfw

Reputation: 605

Programatically getting a screenshot of a Java applet

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?

Upvotes: 1

Views: 683

Answers (3)

JazzTeam
JazzTeam

Reputation: 59

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

Upvotes: -1

OlgaMaciaszek
OlgaMaciaszek

Reputation: 3912

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);

Upvotes: 3

Lai
Lai

Reputation: 482

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);

Upvotes: 0

Related Questions