Problems with createGraphics, nullPointer, type mismatch

createGraphics on Windows10, mode Java

First problem:
using FX2D renderer results in a NullPointerException
at “beginDraw”

Second problem:
not compiling, Type mismatch, “cannot convert from processing.core.PGraphics to PGraphics”
(Pure Woodoo. If I copy the code into a new file, problem is gone, but reproduceable with the first file.
The files are bytewise identical.)

what am I doing wrong?

// first example:
final String renderer = FX2D; //FX2D; //P2D; //JAVA2D; 
PGraphics pg;

void setup() {
  size(200, 200, renderer);
  pg = createGraphics(100, 100, renderer);
}

void draw() {
  pg.beginDraw(); // --> NullPointerException
  pg.background(102);
  pg.stroke(255);
  pg.line(pg.width*0.5, pg.height*0.5, mouseX, mouseY);
  pg.endDraw();
  image(pg, 50, 50); 
}
// second example:

final String renderer = P2D; //FX2D; //P2D; //JAVA2D; 

// Defaults:
int fps = 30; // Framerate
int displayInitialWidth = 800;
int displayInitialHeight = 600;

// Globals
PGraphics overlay;

/// Screen and rendering settings
void settings()
{
  size(displayInitialWidth, displayInitialHeight, renderer);
  pixelDensity(displayDensity());  
  smooth(4);
}

/// Initialization
void setup() 
{
  frameRate(fps);
  surface.setResizable(true);
  overlay = createGraphics(width, height, renderer);
  overlay.smooth(4);
}

// Loop
void draw()
{
  overlay.beginDraw();
  overlay.background(0xFFFFFFFF);
  overlay.line(0, 0, mouseX, mouseY);
  overlay.endDraw();
  image(overlay, 0, 0);
}
1 Like

Hi @Achimoto.
You must use these directly like

size(200, 200, P2D);

You also are not permitted for instance to use

width = 100;
height = 100;
size(width, height, P2D)
1 Like

Thank you for the advice,
(Yes, I should put the variables in settings() ).

But this error stays even if I enter the parameters directly:

Error with FX2D,
renderer P2D works ok.

void setup() {
  size(200, 200, FX2D);
  pg = createGraphics(100, 100, FX2D);
}

void draw() {
  pg.beginDraw(); // --> NullPointerException
...
1 Like

Yes with this renderer this is a known issue. See here.
This renderer is still experimental

2 Likes