Creating PGraphics2D/PGraphics3D without PApplet

Hello, I am trying to use Processing as a rendering engine outside the PApplet context.
The following test code works fine:

public static void main(String[] args) {
   PGraphicsJava2D pg=new PGraphicsJava2D();
   pg.setSize(400, 400);
   String filename=System.getProperty("user.dir")+File.separator+"picture.png";;

The problem is that when I try to use the OpenGL renderers, PGraphics2D and PGraphics3D instead, I get the following error:
Exception in thread “main” java.lang.NullPointerException: Cannot call “processing.core.PApplet.getSurface()” because “this.sketch” is null`.

How do I initialise the PGraphics2D/PGraphics3D renderer correctly?
I am using the 3.3.6 version from the maven repository and the latest 4.x version from the jars in the processing editor, with the same results.
Thank you very much!

1 Like

It’s possible to run java code in the Processing editor, but I don’t think you can make Processing calls inside java code.

Yes, the source code I showed in the question compiles and runs without problems in NetBeans, provided the jar dependencies are satisfied.

If you want to run java code in the Processing editor then I think you would need to do something like this:

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

public static void main(String[] args) {
  JFrame frame = new JFrame();
  frame.setBounds(400, 200, 400, 400);
  // panel to display render results
  JPanel panel = new JPanel() { 

    void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g;
     // setBackground(Color.GREEN);
      g2.drawString("Hello", 80, 40);
      g2.fillRect(130, 30, 100, 80);
      g2.drawOval(30, 130, 50, 60);
      g2.fillOval(130, 130, 60, 60);
      g2.drawRoundRect(250, 30, 100, 80, 20, 20);
      int [] x = {45, 55, 75, 55, 63, 43, 17, 31, 12, 35, 45};
      int [] y = {41, 65, 71, 83, 108, 88, 105, 78, 61, 63, 41};
      g2.drawPolygon(x, y, 10); // 10 vertices required to draw a star
      int[] x1 = {260, 230, 290 };
      int[] y1 = {130, 190, 190 };
      g2.fillPolygon(x1, y1, 3);
      g2.drawArc(30, 210, 40, 50, 90, 60);
      g2.fillArc(100, 180, 40, 50, 180, 40);
      g2.drawLine(30, 260, 380, 260);

You can’t. Normally you would create a new PGraphics instace by calling createGraphics(...) which requires you to call it inside of a PApplet or have it as a reference.
By the way this is also the reason why almost every library wants you to input this when creating a new instance. Unfortunately, the Processing developers tied all functionality to the PApplet, to keep the API as simple as possible for beginners.
The only alternitive would be to create a new PApplet at the beginning of the program and then use surface.setVisible(false) to hide it immediately and then do everything over it. But that would cause a window to pop up briefly at the beginning of your program.

1 Like

Thank you svan, that would execute awt graphic methods into a processing program flow, but what I am looking for is exactly the opposite, that is, executing processing drawing methods in my own program flow. I developed a library to make mathematical animations and a Renderer class that uses JavaFX to perform required drawings. What I am trying to do is to replace this Renderer class by another that uses Processing methods to perform the drawings, as Processing is way more powerful in many aspects than JavaFX.

Thank yoy TimeLex! I will try this approach. I don’t mind the briefly shown pop up window. I will start the PApplet in its own thread and obtain a reference to the created object. The static method PApplet.main does not return a reference, but I saw here a solution (java - Getting the current instance of the PApplet in Processing - Stack Overflow)