Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Problem about screen capture program
Hi, i have done a program which captures the screen contents to an image and stores it to one directory.
package com.example.screencapture;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.imageio.ImageIO;
import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
@SuppressWarnings("serial")
public class ScreencaptureApplication extends Application implements ClickListener
{
private static Window mainWindow = new Window("Screencapture Application");
private Button label = new Button("Capture");
String SaveTo;
@Override
public void init()
{
label.addListener((ClickListener)this);
mainWindow.addComponent(label);
setMainWindow(mainWindow);
}
public void buttonClick(ClickEvent event)
{
Button source = event.getButton();
if(source == label)
{
try
{
Robot robot = new Robot();
Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
int width = (int) d.getWidth();
int height = (int) d.getHeight();
robot.delay(3000);
Image image = robot.createScreenCapture(new Rectangle(0, 0, width, height));
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
Calendar mycalendar = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
String timeStamp = formatter.format(mycalendar.getTime());
SaveTo = "C:/temp/SparkCapture-"+ timeStamp + ".png";
try
{
BufferedImage pi = bi;
File outputfile = new File(SaveTo);
ImageIO.write(pi, "png", outputfile);
} catch (IOException e)
{
e.printStackTrace();
}
getMainWindow().showNotification("Screen Captured Successfully and Saved to:\n"+SaveTo);
}catch(Exception e)
{
}
}
}
}
And when I run this program in eclipse then it shows output file is saved and i found it is created there.
but when i deployed this program as a .war file on tomcat v6 it gives me one big black rectangle rather than an image
so what should i do?
Please help me.
Hi,
The problem is that Vaadin is (mostly) *server-side* library - so the code that you write executes on Tomcat. Therefore, when you click that button, you've just taken a screenshot of the machine tomcat machine. (Well, actually, you probably haven't : tomcat is usually running as a daemon or as a windows service, with no access to the Desktop/GUI - aka Headless , so it *can't* take a screenshot, which is why it is a black rectangle).
In short, to take a screenshot of the browser, you will need to use a Screen Capture add-on.
Cheers,
Charles.
Hi, Thank you very much for such a nice reply. I will try for Screen capture add-on. Thanks again.:D