# Creating PGraphics2D/PGraphics3D without PApplet

**URL:** <https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072>\
**Category:** Coding Questions\
**Created:** [May 26, 2023, 10:00am UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072 "2023-05-26T10:00:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidGR74](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidgr74/32/18371_2.png) [@DavidGR74](https://discourse.processing.org/u/DavidGR74)\
**Post date:** [May 26, 2023, 10:00am UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/1 "2023-05-26T10:00:48Z")

</div>

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

```auto

public static void main(String[] args) {
   PGraphicsJava2D pg=new PGraphicsJava2D();
   pg.setSize(400, 400);
   pg.beginDraw();
   pg.background(255);
   pg.line(0,0,400,400);
   pg.endDraw();
   String filename=System.getProperty("user.dir")+File.separator+"picture.png";
   pg.save(filename);
}

```

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!

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 26, 2023, 5:21pm UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/2 "2023-05-26T17:21:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DavidGR74](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidgr74/32/18371_2.png) [@DavidGR74](https://discourse.processing.org/u/DavidGR74)\
**Post date:** [May 26, 2023, 6:20pm UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/3 "2023-05-26T18:20:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [May 26, 2023, 7:28pm UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/4 "2023-05-26T19:28:36Z")

</div>

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

```auto
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;
      super.paintComponent(g2);
     // setBackground(Color.GREEN);
      g2.setColor(Color.RED);
      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);
    }
    
  };
  panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  frame.add(panel); 
  frame.setVisible(true);
}

```

---

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [May 26, 2023, 10:09pm UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/5 "2023-05-26T22:09:00Z")

</div>

You can’t. Normally you would create a new `PGraphics` instace by calling [`createGraphics(...)`](https://processing.org/reference/createGraphics_.html) 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.

---

<div class="post-metadata">

**Author:** ![DavidGR74](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidgr74/32/18371_2.png) [@DavidGR74](https://discourse.processing.org/u/DavidGR74)\
**Post date:** [May 27, 2023, 9:26am UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/6 "2023-05-27T09:26:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DavidGR74](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/davidgr74/32/18371_2.png) [@DavidGR74](https://discourse.processing.org/u/DavidGR74)\
**Post date:** [May 27, 2023, 9:30am UTC](https://discourse.processing.org/t/creating-pgraphics2d-pgraphics3d-without-papplet/42072/7 "2023-05-27T09:30:53Z")

</div>

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](https://stackoverflow.com/questions/73952485/getting-the-current-instance-of-the-papplet-in-processing))
