From bd57e1354a5b7210e86899a05447e7042de594b4 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Wed, 21 Jul 2021 01:35:55 +0100 Subject: [PATCH] Add Utility class --- .../java/me/goudham/util/ClipboardUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/main/java/me/goudham/util/ClipboardUtils.java diff --git a/src/main/java/me/goudham/util/ClipboardUtils.java b/src/main/java/me/goudham/util/ClipboardUtils.java new file mode 100644 index 0000000..71f274f --- /dev/null +++ b/src/main/java/me/goudham/util/ClipboardUtils.java @@ -0,0 +1,44 @@ +package me.goudham.util; + +import java.awt.Dimension; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.datatransfer.Clipboard; +import java.awt.datatransfer.Transferable; +import java.awt.datatransfer.UnsupportedFlavorException; +import java.awt.image.BufferedImage; +import java.io.IOException; +import me.goudham.model.MyClipboardContent; + +import static me.goudham.util.Contents.IMAGE; +import static me.goudham.util.Contents.STRING; + +public class ClipboardUtils { + + public static MyClipboardContent getClipboardContents(Transferable contents, Clipboard clipboard) { + MyClipboardContent myClipboardContent = new MyClipboardContent<>(""); + + try { + if (STRING.isAvailable(clipboard)) { + myClipboardContent.setContent(contents.getTransferData(STRING.getDataFlavor())); + } else if (IMAGE.isAvailable(clipboard)) { + BufferedImage bufferedImage = convertToBufferedImage((Image) contents.getTransferData(IMAGE.getDataFlavor())); + myClipboardContent.setContent(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight())); + } + } catch (UnsupportedFlavorException | IOException exp) { + exp.printStackTrace(); + } + + return myClipboardContent; + } + + public static BufferedImage convertToBufferedImage(Image image) { + BufferedImage newImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB); + + Graphics2D graphics = newImage.createGraphics(); + graphics.drawImage(image, 0, 0, null); + graphics.dispose(); + + return newImage; + } +}