--Create screenshot and put it into the clipboard
1 post
• Page 1 of 1
--Create screenshot and put it into the clipboard
This article is outdated - please use our new system at
https://doc.sibvisions.com
JVx has a simple mechanism to create "screenshots" from components or the entire application. But if want to save the screenshot, you need a file/directory chooser and some application logic.
Here's a simple solution for using the system clipboard for your screenshots. This example is based on JVx' SwingUI.
- Code: Select all
public void doScreenshot() throws Throwable
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IDimension dim = getSize();
IImage image = capture(dim.getWidth(), dim.getHeight());
image.saveAs(baos, ImageType.PNG);
try
{
TransferableImage trans = new TransferableImage(baos.toByteArray());
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
c.setContents(trans, trans);
}
catch (Exception e)
{
//fallback to file chooser
getApplication().getLauncher().saveFileHandle(
new FileHandle("screenshot.png", baos.toByteArray()));
}
}
- Code: Select all
public class TransferableImage implements Transferable,
ClipboardOwner
{
private static final DataFlavor[] DATAFLAVORS = new DataFlavor[]
{DataFlavor.imageFlavor};
private Image image;
public TransferableImage(byte[] pData) throws IOException
{
image = ImageIO.read(new ByteArrayInputStream(pData));
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException,
IOException
{
if (flavor.equals(DataFlavor.imageFlavor) && image != null)
{
return image;
}
else
{
throw new UnsupportedFlavorException(flavor);
}
}
public DataFlavor[] getTransferDataFlavors()
{
return DATAFLAVORS;
}
public boolean isDataFlavorSupported(DataFlavor flavor)
{
return ArrayUtil.contains(getTransferDataFlavors(), flavor);
}
public void lostOwnership(Clipboard clipboard, Transferable contents)
{
}
}
Simply Paste the image in another application e.g. a Text document.
-
Development@SIB - Posts: 325
- Joined: Mon Sep 28, 2009 1:54 pm
1 post
• Page 1 of 1