Hi I have this code for clipboard image copy to processing. It works well for processing 2.x but not for processing 3.x. I know that java.awt.Applet is no longer the base class used by PApplet, but I can’t identify an alternative.
The error is: The function getToolkit() does not exist.
Do you have an idea? Thanks
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
boolean cbon = false;
PImage img = null;
PImage clipboardImageForProcessing = null;
void setup() {
size(1200, 800);
background(255);
noLoop();
}
void draw() {
GetImageFromClipboard();
if (cbon) {
image (img, 0, 0);
}
}
String GetTextFromClipboard ()
{
String text = (String) GetFromClipboard(DataFlavor.stringFlavor);
return text;
}
PImage GetImageFromClipboard () {
//PImage img = null;
java.awt.Image image = (java.awt.Image) GetFromClipboard(DataFlavor.imageFlavor);
if (image != null)
{
println("Clipboard.GetImageFromClipboard() >> Image transferred from clipboard.");
//img = createImage(imag);
/// img = new PImage(image);
BufferedImage bufferedImage = toBufferedImage(image);
img = new PImage(bufferedImage);
//img = loadImage( image);
}
cbon=true;
return img;
}
Object GetFromClipboard (DataFlavor flavor) {
Clipboard clipboard = getToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
Object object = null;
if (contents != null && contents.isDataFlavorSupported(flavor))
{
try
{
object = contents.getTransferData(flavor);
println("Clipboard.GetFromClipboard() >> Object transferred from clipboard.");
}
catch (UnsupportedFlavorException e1) // Unlikely but we must catch it
{
println("Clipboard.GetFromClipboard() >> Unsupported flavor: " + e1);
//~ e1.printStackTrace();
}
catch (java.io.IOException e2)
{
println("Clipboard.GetFromClipboard() >> Unavailable data: " + e2);
//~ e2.printStackTrace();
}
}
return object;
}
BufferedImage toBufferedImage(java.awt.Image src) {
int w = src.getWidth(null);
int h = src.getHeight(null);
int type = BufferedImage.TYPE_INT_RGB; // other options
BufferedImage dest = new BufferedImage(w, h, type);
Graphics2D g2 = dest.createGraphics();
g2.drawImage(src, 0, 0, null);
g2.dispose();
return dest;
}