Hello folks!
This will work on W10 and Processing 4.4.10 in a Swing window:
/*
Processing + Swing Overlay Demo
-------------------------------
Goal:
Render content offscreen in Processing (PGraphics), convert it to a Java BufferedImage,
then display it in a transparent, undecorated, always-on-top Swing JFrame.
Pipeline:
PGraphics (JAVA2D) -> PImage snapshot -> BufferedImage (ARGB) -> ImageIcon/JLabel -> JFrame
Key points:
- Processing launches the app and renders offscreen only.
- Swing owns the visible window (JFrame) and presents the BufferedImage.
- Alpha transparency is preserved end-to-end (clear() + TYPE_INT_ARGB + transparent JFrame).
*/
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
//import java.net.*;
//import java.io.*;
// ==============================
// Global Variables
// ==============================
JFrame frame; // Floating Swing window
BufferedImage img; // Final Java image buffer for Swing
// Offscreen drawing surface (Processing)
PGraphics pg;
// ==============================
// Processing Setup
// ==============================
void settings() {
size(1, 1); // Minimal Processing window
}
void setup() {
surface.setVisible(false); // Hide the Processing window
createImageWindow(); // Build and show the Swing overlay
}
void draw() {
// No Processing drawing on the main surface; Swing handles the visible window.
}
// ==============================
// Build the Swing overlay window
// ==============================
void createImageWindow() {
// 1) Render offscreen to a PGraphics buffer.
// JAVA2D keeps pixels CPU-accessible for transfer into BufferedImage.
pg = createGraphics(200, 100, JAVA2D);
pg.beginDraw();
pg.clear(); // Transparent background
pg.fill(0, 255, 0);
pg.textSize(48);
pg.textAlign(CENTER, CENTER);
pg.text("TEXT", pg.width/2, pg.height/2);
//pg.save("/data/image.png"); // Optional: save the generated buffer
pg.endDraw();
// 2) Snapshot the PGraphics into a PImage (CPU-side pixel array).
PImage pi = pg.get();
// Optional: use a file image instead of generated content.
//pi = loadImage("image.png");
pi.loadPixels();
// 3) Copy Processing pixels (ARGB ints) into a Java BufferedImage (ARGB).
BufferedImage img = new BufferedImage(pi.width, pi.height, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, pi.width, pi.height, pi.pixels, 0, pi.width);
// 4) Display in Swing: ImageIcon -> JLabel -> transparent, undecorated JFrame.
ImageIcon icon = new ImageIcon(img);
JLabel label = new JLabel(icon);
frame = new JFrame();
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0)); // Transparent window background
frame.setAlwaysOnTop(true);
frame.add(label);
frame.pack();
frame.setVisible(true);
}
Reference:
:)