This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

--Create screenshot and put it into the clipboard

Documents for the development of and with JVx.

--Create screenshot and put it into the clipboard

Postby Development@SIB » Wed Aug 10, 2016 3:03 pm



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.
User avatar
Development@SIB
 
Posts: 325
Joined: Mon Sep 28, 2009 1:54 pm

Return to Documentation