pushMatrix() cannot use push more than 32 times



/**
 * Change PGraphics Max Pushes (v1.0)
 * GoToLoop (2019/Jul/21)
 *
 * https://Discourse.Processing.org/t/
 * pushmatrix-cannot-use-push-more-than-32-times/12860/5
 */

import processing.awt.PGraphicsJava2D;
import java.lang.reflect.Field;

static final int PUSHES = 35, DIAM = 8, OFFSET_Y = 15;

void setup() {
  size(100, 550);
  noLoop();

  fill(#FFFF00);
  stroke(#FF0000);

  setMaxPushes((PGraphicsJava2D) getGraphics(), PUSHES);
}

void draw() {
  clear();

  for (int i = 0; i < PUSHES; ++i) {
    pushMatrix();
    translate(0, OFFSET_Y);
    square(0, 0, DIAM);
  }

  for (int i = 0; i < PUSHES; ++i)  popMatrix();
}

static final void setMaxPushes(final PGraphicsJava2D pg, final int pushes) {
  try {
    final Field f = PGraphicsJava2D.class.getDeclaredField("transformStack");
    f.setAccessible(true);
    f.set(pg, expand(f.get(pg), abs(pushes)));
  }
  catch (final ReflectiveOperationException ex) {
    throw new RuntimeException(ex);
  }
}
2 Likes