Implementing a Processing app inside another one

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);
  }
}

Thanks that is actually really useful
for example i might make more games bigger and much more complex and that will be almost impossible just via processing

but… the question is , i am at the verge of finishing the game all i have is just embed some mini games into a window do you think it’s a good point to switch to eclipse for example?

1 Like

Well, it’s really up to you.

Java code runs faster than Processing and the executable file will be smaller, if you choose to distribute this game online.

Please note that getting P2D, P3D, and FX2D renderers to work outside of the Processing IDE is difficult.

(The default renderer–Java2D–works great)