zoukankan      html  css  js  c++  java
  • e638. 向剪切板获取和粘贴图像

     // If an image is on the system clipboard, this method returns it;
        // otherwise it returns null.
        public static Image getClipboard() {
            Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        
            try {
                if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    Image text = (Image)t.getTransferData(DataFlavor.imageFlavor);
                    return text;
                }
            } catch (UnsupportedFlavorException e) {
            } catch (IOException e) {
            }
            return null;
        }
    

    Setting an image on the system clipboard requires a custom Transferable object to hold the image while on the clipboard.

        // This method writes a image to the system clipboard.
        // otherwise it returns null.
        public static void setClipboard(Image image) {
            ImageSelection imgSel = new ImageSelection(image);
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
        }
        
        // This class is used to hold an image while on the clipboard.
        public static class ImageSelection implements Transferable {
            private Image image;
        
            public ImageSelection(Image image) {
                this.image = image;
            }
        
            // Returns supported flavors
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[]{DataFlavor.imageFlavor};
            }
        
            // Returns true if flavor is supported
            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return DataFlavor.imageFlavor.equals(flavor);
            }
        
            // Returns image
            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
                if (!DataFlavor.imageFlavor.equals(flavor)) {
                    throw new UnsupportedFlavorException(flavor);
                }
                return image;
            }
        }
    
    Related Examples
  • 相关阅读:
    ecshop 调用指定分类的推荐,热卖,新品
    ecshop 首页调用指定类产品
    html常用笔记
    ecshop 修改flash图片大小
    ecshop 删除随机版权
    Java Web(一) Servlet详解!!
    Git使用总结
    git clone命令使用
    Lucene学习总结之四:Lucene索引过程分析
    Lucene学习总结之二:Lucene的总体架构
  • 原文地址:https://www.cnblogs.com/borter/p/9575342.html
Copyright © 2011-2022 走看看