Errors of type "this.g" is null when accessing pure java sketch that extends PApplet as a BufferedImage, output only on 100x100 pixels, other PGraphics objects like size() cannot be used

After spending hours unsuccessfully trying to change the cell size of a raster, I decided to try a different approach. The following demo is written only for Processing and does not utilize java components. It will create a resizable window with an image grid of variable size using unique images created with a noise generator and saved to the sketch folder. Extreme settings may exceed the pixel buffer and require an increase in size in the source code. It is also possible to overrun the Processing apps memory allocation and require a change of size in Preferences (untested). I have created grids up to 54 rows x 54 cols with a 40,000 buffer size which allows for reasonably fast rendering according to grid size. The noise generator was taken from your example.

/*
 Pixel buffer is second dimension of rgb[][][] array
 Num pixels = image length x width ; If pixel buffer is exceeded, an exception is thrown. 
 For single image, an increase of pixel buffer up to 2,000,000 will accommodate screen size of 1900x1000
 Pixel buffer of 40,000 will normally accommodate up to 54 rows x 54 cols (2916 images)
 */

final int _cols = 16;
final int _rows = 16;
final int _hg = 1; //horizontal gutter (space between rows)
final int _vg = 1; //vertical gutter (space between cols)

PImage[] image = new PImage[_cols*_rows+1];

int[][] ca = new int[_cols*_rows][255];
int[][] ca2 = new int[_cols*_rows][255];

float nx, ny, nz;
float theta = 0;
float phi = 0;
float R = random(5, 10);
float S = random(1, 2);

float r, g, b;
// rgb[image id][pixel buffer][3] => r,g,b pixel colors for image
int[][][] rgb = new int[_rows*_cols][40000][3]; // pixels for image size < 200x200
int id = 0;
int pixelCount = 0;

void loadColorArrays() {
  for (int j = 0; j < _cols*_rows; j++) {
    for (int i = 0; i < 255; i++) {
      ca[j][i]= color(random(0, 255), random(0, 255), random(0, 255), random(0, 255));
      ca2[j][i]= color(random(0, 255), random(0, 255), random(0, 255), random(0, 255));
    }
  }
}

void createImageArray() {
  for (int j = 0; j < image.length; j++) {
    image[j] = createImage( width/_rows, height/_cols, RGB);
  }
}

void createImagesFromPixels() {
  for (int k = 0; k < _cols*_rows; k++) {
    for (int x = 0; x < image[k].width; x++) {
      for (int y = 0; y < image[k].height; y++) {
        int loc = x + y * image[k].width;
        image[k].pixels[loc] = color(rgb[k][loc][0], rgb[k][loc][1], rgb[k][loc][2]);
      }
    }
  }
}

void createNoiseGenerators() {
  for (int a = 0; a < _cols*_rows; a++) {
    noiseGenerator(a);
  }
}

void noiseGenerator(int a) {

  image[a].loadPixels();

  for (int x = 0; x < image[a].width; x++) {
    for (int y = 0; y < image[a].height; y++) {
      // map x and y to angles between 0 and TWO_PI
      theta = map(x, 0, image[a].width, 0, TWO_PI);
      phi = map(y, 0, image[a].height, 0, TWO_PI);

      nx = (R + S * cos(phi)) * cos(theta);
      ny = (R + S * cos(phi)) * sin(theta);
      nz = S * sin(phi);

      // normalize noise parameters so that pattern has homogeneous dimensions
      nx = norm(nx, 0, R + r);
      ny = norm(ny, 0, R + r);
      nz = norm(nz, 0, r);

      // apply noise twice and use the equivalent color on the palette
      int loc = x + y * image[a].width;
      image[a].pixels[loc] = ca[a][floor((float) (16 * noise(floor((float) (200 * noise(nx, ny, nz))), 0, 0)))];
      image[a].pixels[loc] = ca2[a][floor((float) (16 * noise(floor((float) (200 * noise(nx, ny, nz))), 0, 0)))];

      float r = red(image[a].pixels[loc]);
      float g = green(image[a].pixels[loc]);
      float b = blue(image[a].pixels[loc]);
      rgb[a][loc][0] = (int)r;
      rgb[a][loc][1] = (int)g;
      rgb[a][loc][2] = (int)b;

      pixelCount++;
    }
  }
  image[a].updatePixels();
  image[a].save("image["+str(a)+"].png");
  println("image :", a);
}

void createImageGrid(int left, int top, int w, int h) {
  for (int k = 0; k < _rows; k++) {
    for (int j = 0; j < _cols; j++) {
      int x = left + j*(w+_vg );
      int y = top + k*(h+_hg);
      image(image[id], x, y, width/_rows, height/_cols);
      id++;
    }
  }
}

void setup() {
  size(600, 600);
  background(0);
  surface.setLocation(300, 100);
  surface.setTitle("Pixel Madness");
  surface.setResizable(true);
  noLoop();

  loadColorArrays();
  createImageArray();
  createNoiseGenerators();
  createImagesFromPixels();
}

void draw() {
  createImageGrid(0, 0, width/_rows, height/_cols);
}

1 Like