open a new sketch on a button click?

edited September 2016 in How To...

hello hello. I'm building a small program for my thesis. It includes several buttons and i want those buttons, when clicked, to open a new sketch. Is that possible or is it done in some other way?

I'm familiar with Java but if it's done differently in Processing, i would like to know. Also i failed in search of this on forums and google, so i'm hoping for some direction if the question or topic already exists. Thank you

All the examples i found include only visual changes when button clicked, for example:

if(button.clicked)background(#somecolor);

I was hoping it would be that simple with invoking another sketch. thank you

Answers

  • You're looking for function runSketch(), in which we pass a String[] + a PApplet subclass instance in order to get another window running concurrently w/ other windows.

    https://forum.Processing.org/two/discussions/tagged?Tag=papplet.runsketch()

    https://forum.Processing.org/two/discussions/tagged?Tag=#multiplewindows

  • Too much info, i'm looking at everything and wondering what now..

    I've found this example but i don't quite understand it.

    https://forum.processing.org/one/topic/loading-other-sketches-from-within-a-sketch.html

    This guy PhiLho says it would be best to merge all sketches. According to his examples (which you can find in his post ^^), does it mean i have to put all my code of each sketch in a separate class and then do as he did, extend them from main sketch class? Do I do it all in a completely new sketch? I'm totally confused with this one.

  • ok, different options:

    processBuilder

    when you have the sketches compiled as a exe-files they are just programs.

    You could run them

    via processBuilder

    https://forum.processing.org/two/discussion/comment/73515/#Comment_73515

    join the sketches

    then you can join the sketches on processing programming level into one.

    let's say you have those 2 simple sketches:

    int[] p = { 
      250, 50, 450
    };
    
    void setup()
    {
      size( 800, 800);
      fill(255, 2, 2);
    } // setup
    
    void draw()
    {
      background(255);
      for (int i = 0; i < p.length; i++) {
        ellipse(p[i], 100, 12, 12);
      }
    } // draw
    

    AND

    float lowerRightCornerX = 210; 
    
    void setup()
    {
      // init
      size(800, 600);
    } // func 
    
    void draw() 
    {
      // runs on and on 
      background(255);
    
      triangle(30, 75, 
        58, 20, 
        lowerRightCornerX, 75);
    } // func 
    
    void mousePressed() {
      if (lowerRightCornerX==86)
        lowerRightCornerX = 210;
      else 
      lowerRightCornerX=86 ;
    } // func
    

    only examples!!!

    joining them you need one draw to handle what's going on. Therefore the draws above to draw1 and draw2.

    Then join global vars and setup()

    Don't have double names of vars etc. ;-)

    The states tell you which sketch of the 2 old ones you are using.

    //
    // states
    final int stateGame  = 0;  // consts // find better names!!!!!! 
    final int statePause  = 1;
    int state = statePause;    // current state
    
    
    // stuff for draw1
    
    
    int i; 
    int[] p = { 
      250, 50, 450
    };
    
    // stuff for draw2
    
    float lowerRightCornerX = 210; 
    
    
    // ---------------------------------------------------------------
    
    void setup()
    {
      // init (runs only once)
      size(800, 600);
    } // func 
    
    void draw() 
    { 
      // draw() runs on and on 
    
      switch (state) {
    
      case stateGame: 
        // Game
        draw2(); 
        break; 
    
      case statePause:
        // Pause
        draw1(); 
        break;
    
      default:
        // error
        println("Error number 939; unknown state : " 
          + state 
          + ".");
        exit();
        break;
      } // switch
    
      fill(0);
      text("hit p to toggle", 77, 177); 
      //
    } // func 
    
    // ------------------------------------------------------------
    
    // functions for states - called from draw() 
    
    void draw1()
    {
      background(255);
      for (int i = 0; i < p.length; i++) {
        ellipse(p[i], 100, 12, 12);
      }
    } // draw
    
    void draw2() 
    {
      // runs on and on 
      background(255);
    
      triangle(30, 75, 
        58, 20, 
        lowerRightCornerX, 75);
    } // func 
    
    // ----------------------------------------
    // keyboard input 
    
    void keyPressed() {
    
      switch (state) {
    
      case stateGame: 
        // Game
        keyPressedForStateGame();
        break; 
    
      case statePause:
        // Pause
        keyPressedForStatePause(); 
        break;
    
      default:
        // error
        println("Error number 1039; unknown state : "
          + state 
          + ".");
        exit();
        break;
      } // switch
    } // func 
    
    // ----
    
    void keyPressedForStateGame() { 
      if (key == CODED) 
      {
        if (keyCode == UP) {
          //
        } else if (keyCode == DOWN) {
          //
        }
    
        if (keyCode == LEFT) {
          //
        } else if (keyCode == RIGHT) {
          //
        } else {
          // do nothing
        } // else
      } //  if (key == CODED) {
      else 
      {
        // not CODED ---------------------- 
        if (key == 'p') {
          // Pause 
          state = statePause;
        } else {
          // do nothing
        } // else
      } // else not CODED
    } // func
    
    void keyPressedForStatePause() { 
      if (key == CODED) 
      {
        if (keyCode == UP) {
          //
        } else if (keyCode == DOWN) {
          // none
        }
    
        if (keyCode == LEFT) {
          //
        } else if (keyCode == RIGHT) {
          //
        } else {
          // do nothing
        } // else
      } //  if (key == CODED) {
      else 
      {
        // not CODED ---------------------- 
        if (key == 'p') {
          //
          state = stateGame;
        } else {
          // do nothing
        } // else
      } // else not CODED
    } // func
    
    // -------------------------------------------------------
    // Mouse input
    
    void mousePressed() {
      //
      switch (state) {
    
      case stateGame: 
        // Game
        mousePressedForStateGame();
        break; 
    
      case statePause:
        // Pause
        mousePressedForStatePause(); 
        break;
    
      default:
        // error
        println("Error number 1139; unknown state : " + state+".");
        exit();
        break;
      } // switch
    } // func 
    
    void mousePressedForStateGame() {
      if (dist(mouseX, mouseY, 10, 100) < 30 ) { 
        println ("Hit Game.");
      }
    } // func 
    
    void mousePressedForStatePause() {
      if (dist(mouseX, mouseY, width-70, 100) < 30 ) { 
        println ("Hit Pause.");
      }
    } // func 
    // =========================================
    

    ok, one idea is that:

    float ballX;
    float ballY;
    
    public void settings() { 
      size(100, 100);
    
      String[] args = {"SecondApplet"};
      SecondApplet sa = new SecondApplet();
      PApplet.runSketch(args, sa);
    }
    
    void draw() {
      ballX = mouseX;
      ballY = mouseY;
      background(0);
      ellipse(ballX, ballY, 10, 10);
    }     
    
    public class SecondApplet extends PApplet {
    
      public void settings() {
        size(100, 100);
      }
    
      public void draw() {
        background(0);
        ellipse(ballX, ballY, 50, 10);
      }
    }
    

    these lines you could call from mousePressed on a button I think:

          String[] args = {"SecondApplet"};
          SecondApplet sa = new SecondApplet();
          PApplet.runSketch(args, sa);
    

    PM me if you need more

    Best, Chrisir ;-)

  • I'm in big rush, will let you know the end of it ;) Thank you for your answers and help!!

  • The launch() method might be your solution

  • That seems fairly easy and what i need quark, i put the right path but only the println line works, it doesn't open the app. I exported it before too.

    This would be perfect if i could make it work.

Sign In or Register to comment.