Can Processing Objects/Classes be used in pure Java Graphics2D environment? (with/without extending the Applet)

Hi, are there any examples that use (import) processing classes in combination with the classic pure Graphics2D environment outside Processing? I mean not just extending the Processing Applet (the new window), but extending the Graphics2D code base with Processing Objects (like a normal compatible library will do). Do we face total inconsistency or we can use them somehow in parallel considering maybe their “coordinate system” idiosyncrasies?
Can Objects like PVectors, be used outside of the Applet, to construct e.g. Point2Ds, Shapes, Path2Ds, etc, or make some i/o operations between them in order to “upgrade” somehow Graphics2D capabilities in a pure java environment? Could this be possible in practice or we consider those environments totally different? If they are different, is there any other “hacky” way to parse the Applet’s x,y image (or a state of it in time) produced from the Processing (PImage, PSurfice …) and parse this image as a Graphics2D raster object? What about vectors?
I am not an experienced Processing user, hopefully this question will make sense in order to see and clarify capabilities and possible options.
Thanks in advance.

I don’t know If thats what you’re looking for but you can easily access the jframe the processing-window would produce and pack the content-pane in a jframe of your own.
You can extend the PApplet to create a processing-window wich you can hide said window and save the contents as an image. If that interests you I can put some code together.

The following may or may not be what you are inquiring about. The demo shows how to use Java2D inside of a Processing app with a JFrame and a JPanel drawing component. Should run in Processing 4.

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

javax.swing.JFrame frame;
java.awt.Canvas canvas;

int _wndW = 400;
int _wndH = 300;

JPanel panel;
Graphics2D g2d;

class DrawPanel extends JPanel {
  public DrawPanel() {
    setBounds(0, 0, _wndW, _wndH);
    public void paintComponent(Graphics g) {
    g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    // **** Rectangle2D **** //
    Rectangle2D rect = new Rectangle2D.Double(60,100,200,100);
    // **** Ellipse2D **** //
    Ellipse2D ellipse = new Ellipse2D.Double();
    // **** Line2D **** //
    Line2D line = new Line2D.Double(60,100,260,200);
    // **** Text **** //
    Font txtFont = new Font("Monospaced", Font.BOLD, 24);

void buildWnd() {
  panel = new DrawPanel();

void setup() {
  frame = (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
  canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative();
  frame.setBounds(500, 300, _wndW, _wndH);

  javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      buildWnd(); // Builds components on EventDispatchThread


1 Like

thanks everyone, this is useful info, but svan solution is about embedding Graphics2D to processing IDE, I am describing the opposite, to embed a sketch (or a state of it in time) in an other app that resides in an other classic Java IDE. Lest say we have a native java graphics app and there we want if possible to add:

  1. Code as usual and produce graphics as (paint) components inside this app, not a processing one with GUI, use processing classes, produce graphics in any place not only in the standalone PApplet window. Maybe this is nearly impossible.

  2. If the above is not possible, find a way to parse a Processing raster (PImage, PSurfice,…) in a paint component, a raster can be rendered () something like the WritableRaster.

 WritableRaster raster = getColorModel().createCompatibleWritableRaster(w, h);
 for (int j = 0; j < h; j++) {
                       for (int i = 0; i < w; i++) {
//...processing code (?)
  1. Other intermediate ways(?) like this one: java - Adding processing 3 to a Jpanel - Stack Overflow
    If those are the final options, I guess the 3rd one is the only way embedding a PSurfice to a Native JPanel outside of processing IDE.
    copying the link code here for convenience:
import javax.swing.JFrame;

import processing.awt.PSurfaceAWT.SmoothCanvas;
import processing.core.PApplet;
import processing.core.PSurface;

public class ProcessingTest extends PApplet{

    public void settings(){
        size(200, 200);

    public void draw(){
        ellipse(mouseX, mouseY, 20, 20);

    public static void main(String... args){

        //create your JFrame
        JFrame frame = new JFrame("JFrame Test");

        //create your sketch
        ProcessingTest pt = new ProcessingTest();

        //get the PSurface from the sketch
        PSurface ps = pt.initSurface();

        //initialize the PSurface
        ps.setSize(200, 200);

        //get the SmoothCanvas that holds the PSurface
        SmoothCanvas smoothCanvas = (SmoothCanvas)ps.getNative();

        //SmoothCanvas can be used as a Component

        //make your JFrame visible
        frame.setSize(200, 200);

        //start your sketch

If anyone has alternative solutions, please share them

The following source code runs in IntelliJ. Key is to add core.jar from Processing 4 app bundle to Libraries.

import processing.core.PApplet;

public class Main extends PApplet {
    // The argument passed to main must match the class name
    public static void main(String[] args) {

    // method used only for setting the size of the window
    public void settings(){
        size(400, 400);

    // identical use to setup in Processing IDE except for size()
    public void setup(){

    // identical use to draw in Processing IDE
    public void draw(){