For whatever it’s worth, here is a short demo of changing scenes, each with animated PGraphics. It also works with a P3D renderer. The Button class code goes under a separate tab or inline, whatever you prefer.
PGraphics circles;
PGraphics squares;
int _wndW = 600;
int _wndH = 650;
Button _btn1;
Button _btn2;
color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color GREEN = color(0, 255, 0);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
boolean showCircles = false;
boolean showSquares = false;
void setup() {
size(_wndW, _wndH);
circles = createGraphics(width, height);
squares = createGraphics(width, height);
_btn1 = new Button( 30, 10, 100, 24, "Circles", YELLOW, BLACK);
_btn2 = new Button( 140, 10, 120, 24, "Squares", LTGRAY, BLACK);
}
void draw() {
background(0);
_btn1.display();
_btn2.display();
if (showCircles) {
circles.beginDraw();
circles.fill(random(255),random(255),random(255));
circles.circle(random(width),random(height),15);
circles.endDraw();
image(circles, 0, 60);
}
if (showSquares) {
squares.beginDraw();
squares.fill(random(255),random(255),random(255));
squares.rect(random(width), random(height), 15, 15);
squares.endDraw();
image(squares, 0, 60);
}
}
void mousePressed() {
if ((mouseX >= _btn1.x) && (mouseX <= _btn1.x + _btn1.w) && (mouseY >= _btn1.y) && (mouseY <= _btn1.y + _btn1.h)) {
showCircles = true;
showSquares = false;
}
if ((mouseX >= _btn2.x) && (mouseX <= _btn2.x + _btn2.w) && (mouseY >= _btn2.y) && (mouseY <= _btn2.y + _btn2.h)) {
showSquares = true;
showCircles = false;
}
}
Button class:
class Button {
float x, y, w, h;
String title;
color btnColor;
color txtColor;
// Constructor
Button(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
x = xpos;
y = ypos;
w = wt;
h = ht;
title = titleStr;
btnColor = background;
txtColor = foreground;
}
void display() {
fill(btnColor); // button color
noStroke();
rect( x, y, w, h, 15); // rounded rect
fill(txtColor); // text color
textSize(16);
textAlign(CENTER, CENTER);
text(title, x, y-1, w, h);
}
}