Multiple sketches

edited May 2015 in Library Questions

Howdy, i'm doing a work and I want to, by a menu with several options, click an option and a diferent sketch appears in a pop up window, i have already all the sketches done I just need to "glue" them together and i dont know how, I've tried multisketch but i don't think it lets me use more than two sketches. please help!! thanks 8)

Answers

  • Please do not post multiple copies of the same question. I've deleted your duplicate post.

    You'll have better luck if you try something out and post some example code. What have you tried with multisketch?

  • edited May 2015

    oh sorry, in this example they only use two sketches, one in a class and other in none, what i want is to from a menu sketch access other sketches. that would need multiple classes i think, but if i put the other sketch in a class it doesn't work and i dont know how to use more than two sketches in multisketch.

    import org.gicentre.utils.multisketch.*;
    class AnotherSketch extends EmbeddedSketch 
    { 
      float textScale;  
      void setup() 
      {
        size(300, 300);
        textFont(createFont("SansSerif", 24), 24);
        textAlign(CENTER, CENTER);
        fill(20, 120, 20);
        textScale = 0;
      }
      void draw() 
      { 
        super.draw();   // Should be the first line of draw(). 
        background(200, 255, 200); 
     
        pushMatrix(); 
        translate(width/2, height/2); 
        scale(0.1+sin(textScale), 1); 
        text("Hello again", 0, 0); 
        popMatrix(); 
     
        textScale += 0.02;
      }
    }
    float rotationAngle; 
    void setup() 
    { 
      size(300, 300); 
      textFont(createFont("Serif", 32), 32); 
      textAlign(CENTER, CENTER); 
      fill(120, 20, 20); 
      rotationAngle = 0; 
     
      PopupWindow win = new PopupWindow(this, new AnotherSketch()); 
      win.setVisible(true);
    } 
    void draw() 
    { 
      background(255, 200, 200); 
      pushMatrix(); 
      translate(width/2, height/2); 
      rotate(rotationAngle); 
      text("Hello world", 0, 0); 
      popMatrix(); 
     
      rotationAngle += 0.01;
    }
  • edited May 2015

    for example, using this menu i would pop up another sketch

    float c=-40;
    float r=0, x=0, y=0;
    float teta=0, ha=height/18;
    int a=60, hue=0,cl=0,sx,sy,stage;
    PImage img;
    
    void setup(){
    
     size(670,600,P3D); 
     //textSize(60);
     img = loadImage("image.jpg");
     smooth();
     stage=1;
     
    }
    
    void letras(){
       if(stage==1){
      //image(img,0,0,width,height);
      a++;
      textSize(a);
      
      colorMode(HSB,360,100,100);
      hue=mouseY/3+25;
      cl=color(hue,100,100);
      textSize(60);
      
       background(0);
      r=mouseX-width/3;
      x= r*cos(teta);
      y= r*sin(teta);
      translate(x+width/2,y+height/2,c);
      ha+=0.01;
      rotateX(ha);
      teta+=0.1;
      fill(cl);
      pushMatrix();
      text("Tripatica",-20,0,20);
      popMatrix();
      if(mousePressed)
        stage=2;}
      if(stage==2){
        int c1selet=180;
        int c2selet=180;
        int ck1=0,ck2=0,ck3=0;
        int c3selet=180;
        colorMode(RGB,360,100,100);
        background(0);
       
        
    
        //rect(50,(height/3)-30,400,200);
        
        int i=mouseY;
          if(i>=0 && i<=height/3)
            c1selet=270;
          else if (i>=height/3 && i<=height*(2/3))
            c2selet=270;
          else if(i>=450)
            c3selet=270;
            
          
          
           ck1=color(c1selet,100,100);
           ck2=color(c2selet,100,100);
           ck3=color(c3selet,100,100);
           fill(ck3);
           rect(width/3,450,300,150);
            
           fill(ck2);
           rect(width/3,250,300,150);
           fill(ck1);
           rect(width/3,50,300,150);
        }
        
        
        
      }
      
    
    /*void menu(){
      background(255,255,000);
    }
    void afterletras(){
      background();
    }*/
    void draw(){
      
      
      letras();
      
    }
  • edited May 2015 Answer ✓

    Multiple PApplet sketches w/o relying on PopupWindow: :-h

    /**
     * Multiple Nested PApplets (v1.01)
     * by GoToLoop (2015/May/21)
     *
     * forum.processing.org/two/discussion/10937/multiple-sketches
     * forum.processing.org/two/discussion/7036/multiple-screens
     */
    
    // Main PApplet Sketch:
    void setup() {
      size(300, 300, JAVA2D);
      smooth(4);
    
      textFont(createFont("Serif", 32, true));
      textAlign(CENTER, CENTER); 
      fill(120, 20, 20);
    
      // Instantiating All Nested PApplet Sketches:
      String[] sketches = getSketchNestedClassNames();
      for (String sketch : sketches)  main(sketch);
      printArray(sketches);
    }
    
    void draw() {
      background(255, 200, 200); 
      translate(width>>1, height>>1);
      rotate(frameCount*.01);
      text("Hello world", 0, 0);
    }
    
    // Util Functions for Nested PApplet Sketches:
    static final String getSketchClassName() {
      return Thread.currentThread().getStackTrace()[1].getClassName();
    }
    
    static final String[] getSketchNestedClassNames() {
      Class[] nested;
    
      try {
        nested = Class.forName(getSketchClassName()).getClasses();
      }
      catch (ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
    
      int idx = 0, len = max(0, nested.length - 2);
      String[] classes = new String[len];
    
      while (idx != len)  classes[idx] = nested[idx++].getName();
      return classes;
    }
    
    // Nested PApplet Class A:
    public static class AnotherSketch extends PApplet {
      void setup() {
        size(300, 150, JAVA2D);
        smooth(4);
    
        textFont(createFont("SansSerif", 24, true));
        textAlign(CENTER, CENTER);
        fill(20, 120, 20);
      }
    
      void draw() {
        background(200, 255, 200);
        translate(width>>1, height>>1);
        scale(.1 + sin(frameCount*.02), 1);
        text("Hello again", 0, 0);
      }
    }
    
    // Nested PApplet Class B:
    public static final class Bouncy extends PApplet {
      /** 
       * Bouncy Words (v3.04)
       * by tfguy & jtwhite14 (2013/Aug)
       * mod GoToLoop
       * 
       * forum.processing.org/topic/animated-text
       * studio.processingtogether.com/sp/pad/export/ro.9eHY5Fel1e0Jr/latest
       */
    
      static final int NUM = 3;
      final BouncyWord[] words = new BouncyWord[NUM];
    
      void setup() {
        size(300, 200, JAVA2D);
        smooth(4);
    
        fill(BouncyWord.INK);
        textAlign(LEFT, CENTER);
        textFont(createFont("Trebuchet MS Bold", BouncyWord.DIM, true));
    
        words[0] = new BouncyWord("History", -1.5, 0, 50);
        words[1] = new BouncyWord("Design", 2.3, 0, 100);
        words[2] = new BouncyWord("Studio", -3, 0, 150);
      }
    
      void draw() {
        background(-1);
        for (BouncyWord w : words)  w.bounce();
      }
    
      class BouncyWord {
        static final short DIM = 90;
        static final color INK = 0100 << 030;
    
        String word;
        float px, py, vx, vy;
    
        BouncyWord(String iword, float ivy, float ipx, float ipy) {
          word = iword;
          vy = ivy;
          px = ipx;
          py = ipy;
        }
    
        void bounce() {
          if ((py += vy) >= height - (DIM>>2) | py < (DIM>>2))  vy *= -1;
          text(word, px, py);
        }
      }
    }
    
  • Thanks i love you

  • why is

    int idx = 0, len = max(0, nested.length - 2); the lenght of the array - 2 

    the lenght-2 ? i tought it was suppose to be one because you know [3] its 0, 1, 2, 3 and so on

  • ok so the sketches are being initiated in the main sketch's setup but the setup only runs once right? now if i wanted to make for example

    keypressed({
    if (key==a) show class A
    else show class B 

    what would i do?

  • edited May 2015 Answer ✓

    The lenght-2?

    getClasses() ends up returning 2 internal PApplet classes besides the 1s we implemented ourselves!
    They seem always being the last 2 classes obtained. That's why nested.length - 2 skips them! :ar!

  • edited August 27 Answer ✓

    What would I do?

    • As you noticed, I've called main() in order to instantiate & ignite all nested PApplet sketches.
    • However, main() is void and doesn't return anything.
    • Therefore, we don't get hold of the references as a way to track those nested sketches! :-S
    • In place of main(), we'll rely on runSketch() from now on.
    • This 1 allows us to pass an already instantiated PApplet as its 2nd argument.
    • As a bonus, sketches don't need to be public nor static as main() demanded! \m/
    • Since the main PApplet sketch was responsible to directly instantiate the other nested 1s,
      it can save & track their references in variables it has control of! *-:)
    • Other key ingredients are setVisible(), noLoop() & loop() methods:
      http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#setVisible-boolean-
      https://processing.org/reference/noLoop_.html
      https://processing.org/reference/loop_.html
    • With them, we can pause/resume & hide/show any PApplet which we hold the reference!
    • Check out latest (v2.0) below. Any doubts just ask here again: O:-)

    /**
     * Multiple Nested PApplets (v2.0)
     * by GoToLoop (2015/May/21)
     *
     * Forum.Processing.org/two/discussion/10937/multiple-sketches#Item_11
     * Forum.Processing.org/two/discussion/7036/multiple-screens
     */
    
    // Main PApplet Sketch:
    static final int FPS = 60, TOGGLE = 5*FPS; // 5 seconds
    
    final PApplet another = new AnotherSketch(), bouncy = new Bouncy();
    
    void setup() {
      size(300, 300, JAVA2D);
      smooth(4);
      frameRate(FPS);
    
      textFont(createFont("Serif", 32, true));
      textAlign(CENTER, CENTER); 
      fill(120, 20, 20);
    
      // Instantiating All Nested PApplet Sketches:
      String[] sketches = getSketchNestedClassNames();
      //for (String sketch : sketches)  main(sketch);
      printArray(sketches);
    
      //runSketch(new String[] { ARGS_FULL_SCREEN, "Full Window" }, another);
      runSketch(new String[] { "My Hello Window" }, another);
      runSketch(new String[] { "My Bouncy Window" }, bouncy);
    }
    
    void draw() {
      background(255, 200, 200); 
      translate(width>>1, height>>1);
      rotate(frameCount*.01);
      text("Hello world", 0, 0);
    
      if (frameCount % TOGGLE == 0)  toggleSketch(bouncy);
    }
    
    void keyPressed() {
      int k = keyCode;
    
      if (k == ENTER | k == RETURN)  activateSketch(another);
      else if (k == ' ')             disableSketch(another);
    }
    
    // Util Functions for Hiding/Displaying/Toggling Sketches:
    static final void disableSketch(PApplet p) {
      p.frame.hide();
      p.noLoop();
    }
    
    static final void activateSketch(PApplet p) {
      p.frame.show();
      p.loop();
    }
    
    static final void toggleSketch(PApplet p) {
      boolean isActive = p.frame.isVisible();
      p.frame.setVisible(!isActive);
    
      if (isActive)  p.noLoop();
      else           p.loop();
    }
    
    // Util Functions for Nested PApplet Sketches:
    static final String getSketchClassName() {
      return Thread.currentThread().getStackTrace()[1].getClassName();
    }
    
    static final String[] getSketchNestedClassNames() {
      Class[] nested;
    
      try {
        nested = Class.forName(getSketchClassName()).getClasses();
      }
      catch (ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
    
      int idx = 0, len = max(0, nested.length - 2);
      String[] classes = new String[len];
    
      while (idx != len)  classes[idx] = nested[idx++].getName();
      return classes;
    }
    
    // Nested PApplet Class A:
    public static final class AnotherSketch extends PApplet {
      void setup() {
        size(300, 150, JAVA2D);
        smooth(4);
    
        textFont(createFont("SansSerif", 24, true));
        textAlign(CENTER, CENTER);
        fill(20, 120, 20);
      }
    
      void draw() {
        background(200, 255, 200);
        translate(width>>1, height>>1);
        scale(.1 + sin(frameCount*.02), 1);
        text("Hello again", 0, 0);
      }
    }
    
    // Nested PApplet Class B:
    public static final class Bouncy extends PApplet {
      /** 
       * Bouncy Words (v3.04)
       * by tfguy & jtwhite14 (2013/Aug)
       * mod GoToLoop
       * 
       * forum.processing.org/topic/animated-text
       * studio.processingtogether.com/sp/pad/export/ro.9eHY5Fel1e0Jr/latest
       */
    
      static final int NUM = 3;
      final BouncyWord[] words = new BouncyWord[NUM];
    
      void setup() {
        size(300, 200, JAVA2D);
        smooth(4);
    
        fill(BouncyWord.INK);
        textAlign(LEFT, CENTER);
    
        textFont(createFont("Trebuchet MS Bold", BouncyWord.DIM, true));
    
        words[0] = new BouncyWord("History", -1.5, 0, 50);
        words[1] = new BouncyWord("Design", 2.3, 0, 100);
        words[2] = new BouncyWord("Studio", -3, 0, 150);
      }
    
      void draw() {
        background(-1);
        for (BouncyWord w : words)  w.bounce();
      }
    
      class BouncyWord {
        static final short DIM = 90;
        static final color INK = 0100 << 030;
    
        String word;
        float px, py, vx, vy;
    
        BouncyWord(String iword, float ivy, float ipx, float ipy) {
          word = iword;
          vy = ivy;
          px = ipx;
          py = ipy;
        }
    
        void bounce() {
          if ((py += vy) >= height - (DIM>>2) | py < (DIM>>2))  vy *= -1;
          text(word, px, py);
        }
      }
    }
    
  • you're just awsome.

  • Yes I have a problem my beloved I just united 4 sketches to a main sketch due to your aid, but in the menu the moment I select a sketch to run the program just breaks if you could take a look it would be awsome

  • final PApplet triangulas = new Triangulas(), espiral = new Espiral(), pytagoras = new Pytagoras(), respira = new Respira();
    //MAIN SKETCH E O MENU
    float c=-40 ;
    float r=0, x=0, y=0;
    float teta=0, ha=height/18;
    int hue=0, hue2=1, cl=0, sx, sy, stage, vah=50, kah=1, n=0;
    PImage img, title;
    
    void setup() {
      size(670, 600, P3D);
      smooth();
      stage=1;
      //frameRate(FPS);
      rectMode(RADIUS);
      colorMode(HSB, 360, 100, 100);
      // Instantiating All Nested PApplet Sketches:
      String[] sketches = getSketchNestedClassNames();
      //for (String sketch : sketches)  main(sketch);
      printArray(sketches);
    
      //runSketch(new String[] { ARGS_FULL_SCREEN, "Full Window" }, another);
     /* runSketch(new String[] { 
        "Triangulas"
      }
      , triangulas);
      runSketch(new String[] { 
        "Espiral"
      }
      , espiral);
      runSketch(new String[] { 
        "Pytagoras"
      }
      , pytagoras);
      runSketch(new String[] { 
        "Expanção"
      }
      , respira);*/
    }
    public void letras() {
      if (stage==1) {
        //image(img,0,0,width,height);
        hue=mouseY/3+25;
        cl=color(hue, 100, 100);
        textSize(69);
    
        background(0);
        r=mouseX-width/3;
        x= r*cos(teta);
        y= r*sin(teta);
        translate(x+width/2, y+height/2, c);
        //ha+=0.01;
        rotateX(7);
        rotateY(6);
        teta+=0.1;
        fill(cl);
        pushMatrix();
        text("Tripatica", -20, 0, 20);
        popMatrix();
        if (mousePressed)
          stage=2;
      }
      if (stage==2) {
    
        int cselet=180;
    
        int ck1=0, ck2=0, ck3=0;
    
    
        background(0);
        float xs4=width/4, ys4=height/4, ys=ys4+height/2, xs=xs4+height/2;
        textSize(15);
        text("press 'ESC' to exit", width/2-40, height-10);
        //rect(50,(height/3)-30,400,200);
        //image(title, width/2, 10);
        int i=mouseY, a=mouseX, rot1=0, rot2=0, rot3=0, rot4=0;
        if (i>=0 && i<=height/3)
          cselet=270;
        else if (i>=height/3 && i<=height*(2/3))
          cselet=270;
        else if (i>=450)
          cselet=270;
        //fill();
        fill(255);
    
        n++;
        if (n%30==0) {
          kah*=-1;
        }
        if (kah>0) {
          vah++;
          hue2+=9;
        } else if (kah<0) {
          vah--;
          hue2-=9;
        }
        cl=color(hue2, 100, 100);
        fill(cl);
        rotate(rot1);
        text("A:", xs4-100, ys4);
        rect(xs4, ys4, vah, vah);
        rotate(rot2);
        text("C:", xs4-100, ys);
        rect(xs4, ys, vah, vah);
        rotate(rot3);
        text("D:", xs-100, ys);
        rect(xs, ys, vah, vah);
        rotate(rot4);
        text("B:", xs-100, ys4);
        rect(xs, ys4, vah, vah);
        textSize(((vah+50)*2)-200);
        text("TRIPATICA", width/2-90, 50);
        fill(0);
        textSize(20);
        text("PYTAGORAS", xs4-60, ys4);
        text("FORMAS", xs4-60, ys);
        text("ESPIRAL", xs-45, ys4);
        text("RESPIRAÇAO", xs-60, ys);
        // rotate(radians(45)); ao clicar nos quadrados
      }
    }
    
    
    
    void draw() {
      letras();
    }
    void keyPressed() {
      int k = keyCode;
      
      if (k == 'A')  activateSketch(pytagoras);
      if (k == ' ') {
        disableSketch(pytagoras);
        disableSketch(espiral);
        disableSketch(respira);
        disableSketch(triangulas);
      }
      if (k == 'B')  activateSketch(espiral);
      if (k == 'C')  activateSketch(respira);
      if (k == 'D')  activateSketch(triangulas);
    }
    //HIDE DISPLAY SKETCH
    static final void disableSketch(PApplet p) {
      p.frame.hide();
      p.noLoop();
    }
     
    static final void activateSketch(PApplet p) {
      p.frame.show();
      p.loop();
    }
    
    static final String getSketchClassName() {
      return Thread.currentThread().getStackTrace()[1].getClassName();
    }
     
    static final String[] getSketchNestedClassNames() {
      Class[] nested;
     
      try {
        nested = Class.forName(getSketchClassName()).getClasses();
      }
      catch (ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
     
      int idx = 0, len = max(0, nested.length - 2);
      String[] classes = new String[len];
     
      while (idx != len)  classes[idx] = nested[idx++].getName();
      return classes;
    }
    
    public static class Triangulas extends PApplet {
     int x, c=0, d=0;
    int y;
    float outsideRadius = 150;
    float insideRadius = 100;
    
    void setup() {
      size(640, 360);
      background(204);
      x = width/2;
      y = height/2;
    }
    
    void draw() {
      colorMode(HSB,360,100,100);
      c=color(117,100,100);
      d=color(46,100,100);
      background(230,100,100);
      
      int numPoints = int(map(mouseX, 0, width*2, 5, 60));
      float angle = 0;
      float angleStep = 180.0/numPoints;
      
      
      fill(c);
      beginShape(TRIANGLE_STRIP); 
      for (int i = 0; i <= numPoints; i++) {
        float px = x + cos(radians(angle)) * outsideRadius;
        float py = y + sin(radians(angle)) * outsideRadius;
        angle += angleStep;
        vertex(px, py);
        px = x + cos(radians(angle)) * insideRadius;
        py = y + sin(radians(angle)) * insideRadius;
        vertex(px, py); 
        angle += angleStep;
        float nume= (numPoints-2)*180;
      }
      endShape();
      int numt = (numPoints-2)*180;
      String angles = str(angle), angleSteps = str(angleStep), num=str(numPoints), numts= str(numPoints);
      fill(d);
      text("soma dos angulos: ("+ num +"-2) * 180 ="+ numts,5,10);
      text("agulo de cada vertice: "+angleSteps,5,20);
    } 
      
    }
    public static class Pytagoras extends PApplet {
      PShape triangle;
    int c1=0, c2=0, p1,p2, conta=0, a=0;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ;
    float h=0;
    
    void setup(){
      size(1200,720,P3D);
      
    }
    
     void bacalhanso(){
       
       noFill();
       background(0);
       stroke(255);
        noFill();
       
       
        p1 =mouseX+width/3;
        p2= mouseY+height/3;
        //TRIANGLE
       beginShape(TRIANGLES); 
    vertex(p1,height/3);
    vertex(width/3, p2);
    
    vertex(width/3,height/3);
    endShape(CLOSE);
    
    //1ST SQUARE
    beginShape(QUADS);
    vertex(p1,height/3);
    vertex(width/3,height/3);
    endShape(CLOSE);
    
    //2ND SQUARE
    beginShape(QUADS);
    vertex(width/3, p2);
    vertex(width/3,height/3);
    
    endShape(CLOSE);
    
    //background(0);
      //LADO QUAD 1
      
      //lado quad 2
      
      //lado quad da hipotenusean
      int x1 =mouseX+width/3;
      int y2 =mouseY+height/3;
      int y1= height/3;
      int x2= width/3;
      float dishipo=sqrt(sq(x2-x1)+sq(y2-y1));
      float quadhipo=sq(dishipo);
      String ladhipo=str(dishipo);
      String quadhipopo=str(quadhipo);
      int xyhipo=round(((dishipo)/2)+height/3);
      
      
      //cateto de cima
      int x2cat1 =mouseX+width/3;;
      float discat1=round(sqrt(sq(x2cat1-x2)+sq(y1-y1)));
      float quadcat1=sq(discat1);
      String ladcat1=str(discat1);
      String quadcato1=str(quadcat1);
      int xstrcat1= round((discat1/2)+width/3);
      text(ladcat1,xstrcat1,(height/3)-5/*escolher o valor*/);
      
       //cateto de baixo
     
      int y2cat2 =mouseY+height/3;
      float discat2=round(sqrt(sq(x2-x2)+sq(y2cat2-y1)));
      float quadcat2=sq(discat2);
      String ladcat2=str(discat2);
      String quadcato2=str(quadcat2);
      int ystrcat2= round((discat2/2)+height/3);
     
      //rotate(PI);
      
       //contas
     
      text(ladcat2,(width/3)+1,ystrcat2);
      String conta1 ="(" + ladcat1 +")^2 + (" + ladcat2 +")^2 = ("+ ladhipo +")^2";
      text(conta1, 10,15);
       String conta2 = quadcato1 +" + "+ quadcato2 +" = "+ quadhipopo;
       text(conta2, 10,30);
      text(ladhipo,xstrcat1,ystrcat2);
      //ver os angulos!!!
      //do de cima x 
      float subang1= discat1/dishipo;
      float angrad1= asin(subang1);
      float angdeg1= angrad1/PI*180;
      String sang1= str(angdeg1) + "º";
      
      text(sang1,width/3,y2+10);
      
      //do de baixo y
      float subang2= discat2/dishipo;
      float angrad2= acos(subang1);
      float angdeg2= angrad2/PI*180;
      String sang2= str(angdeg2) + "º";
      text(sang2,x1,height/3);
      //90º
      text("90º",width/3+10,height/3+10);
      text(sang2 +"º + "+ sang1 +"º + 90º = 180º", 10, 45);
      //QUADRADOS
      rect(width/3-mouseY,height/3,mouseY,mouseY);  //cato2
      int quad2textpo=round(width/3-(mouseY/2));
       text(quadcato2,quad2textpo-20,ystrcat2);
       
      rect(width/3,height/3-mouseX,mouseX,mouseX); //cato1
      int quad1textpo=round(height/3-(mouseX/2));
       text(quadcato1,xstrcat1,quad1textpo-20);
       //para o quad da hipotenusa utilizar o quad();
       
     }
    void draw(){
     
     bacalhanso();
     
    }
    
      
    }
    public static class Espiral extends PApplet {
      void setup() {
      size(720,720, P3D);
      background(0);
      smooth();
    }
    float trip=0;
    float r=0;
    int hue =1;
    int hue2=360;
    int c=0, z=720;
    float r2=180;
    int baca=0;
    void spiral(){
      noStroke();
      colorMode(HSB,360,100,100);
      
      c=color(hue,100,100);
      fill(c);
      hue++;
      
      
      float x = r * cos(trip)*cos(trip);
      float y = r * sin(trip)*sin(trip);
      //ellipse(x+width/2, y+height/2, 10, 10);
      
      trip += 2;
      r += 0.7;
      //if(r==360){
        //r=0;
        //trip =0;
      
        
      }
    
      void spiral2(){
        noStroke();
     colorMode(HSB,360,100,100);
       
      c=color(hue,100,100);
      fill(c);
      hue++;
      if(hue==360)
      hue=0;
      float x = r2 * cos(trip)*cos(trip);
      float y = r2 * sin(trip);
      
      //ellipse(x+width/2, y+height/2, 12, 12);
      translate(x+width/2, y+height/2,z);
      sphere(10);
      trip += 2;
      r2--;
      //if(r==360){
        //r=0;
          //trip =0;
        //}
      }
       void spiral3(){
        noStroke();
      colorMode(HSB,360,100,100);
      
      c=color(hue2,100,100);
      fill(c);
      
      hue2--;
      if(hue2==0)
      hue2=360;
      
      float x = r2 * cos(trip)*cos(trip);
      float y = r2 * sin(trip);
      float k = x+width/2;
      
      if(k<=370)
      z++;
      else
      z--;
      //ellipse(x+width/2, y+height/2, 12, 12);
      baca++;
      //rotateY(PI/3);
      //rotateX(PI/3);
      //rotateZ(PI/3);
      lights();
      translate(x+width/2, y+height/2,z);
      sphere(10);
      trip += 2;
      r2-=0.05;
      if(r==360){
      r=0;
      trip=0;}
    }
    void draw() {
      //spiral();
      //spiral2();
      spiral3();
      
    }
      
    }
    public static class Respira extends PApplet {
      float a=220;
    float b=500;
    int i=0, bacalhuncio=0;
    float xab=0, yab=0, xac=0, yac=0, xdb=0, ydb=0, xdc=0, ydc=0;
    
    void setup() {
      size(720, 720, P3D);
      
      smooth();
    }
    void bacalhau() {
    }
    void draw() {
      int intersected;
      background(0);
      
        a++;
        b--;
        i++;
        if (i>=280) {
          a-=2;
          b+=2;
        }
        fill(0,255,0);
        
    
      fill(0,255,0);
      intersected=intersect(/*A*/500, 220, a, 500,/*B*/500, 500, 220, a,/*C*/ 500, b, 220, 220,/*D*/b, 220, 220, 500);
      //cruzamentos AB, AC, BD, CD 
      /*ellipse(xab,yab,6,6);
      ellipse(xac,yac,6,6);
      ellipse(xdb,ydb,6,6);
      ellipse(xdc,ydc,6,6);*/
      
       //camera(mouseX, mouseY, 100.0, // eyeX, eyeY, eyeZ
         //    360.0, 360.0, -200.0, // centerX, centerY, centerZ
           //  0.0, 1.0, 0.0);
    lights();
    
      beginShape(); 
    vertex(xab,yab);//AB
    vertex(xac,yac);//AC
    
    vertex(540, 540, 100);//Z
    endShape();
    
     beginShape(); 
    vertex(xab,yab);//AB
    vertex(xac,yac);//AC
    
    vertex(50, 50, -100);//Y
    
    endShape();
    
    
     beginShape(); //YABAC
    //vertex(xab,yab);//AB
    //vertex(xac,yac);//AC
    vertex(xdb,ydb);//DB
    vertex(xdc,ydc);//DC
    //vertex(180, 180);
    vertex(540, 540, 100);
    endShape();
    
     beginShape(); 
    
    vertex(xdb,ydb);//DB
    vertex(xdc,ydc);//DC
    vertex(50, 50, -100);
    
    endShape();
    
    
    
    beginShape(); //
    vertex(xab,yab);//AB
    
    vertex(xdb,ydb);//DB
    
    
    vertex(540, 540, 100);
    endShape();
    
     beginShape(); //ZABAC
    
    vertex(xac,yac);//AC
    
    vertex(xdc,ydc);//DC
    vertex(50, 50, -100);
    endShape();
    
     beginShape(); //YABAC
    //vertex(xab,yab);//AB
    vertex(xac,yac);//AC
    //vertex(xdb,ydb);//DB
    vertex(xdc,ydc);//DC
    //vertex(180, 180);
    vertex(540, 540, 100);
    endShape();
    
      
    
    // variaveis tipo "xb2" = x ou y, letra da linha, nr do ponto
    //float xc1,float yc1,float xc2,float yc2,float xd1,float yd1,float xd2,float yd2
    //,5+00, b, 220, 220,b, 220, 220, 500
    }
      int intersect(float xa1,float ya1,float xa2,float ya2,float xb1,float yb1,float xb2,float yb2,float xc1,float yc1,float xc2,float yc2,float xd1,float yd1,float xd2,float yd2){
      float ab1,ab2,abk1,abk2,abl1,abl2, ac1,ac2,ack1,ack2,acl1,acl2,db1,db2,dbk1,dbk2,dbl1,dbl2, dc1,dc2,dck1,dck2,dcl1,dcl2;
      
      float denomab,numab, offsetab, denomac,numac, offsetac, denomdb,numdb, offsetdb, denomdc,numdc, offsetdc; 
      
      //cruzamento AB:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      
      ab1 = ya2 - ya1;
      abk1 = xa1 - xa2;
      abl1 = (xa2 * ya1) - (xa1 * ya2);
    
    
      ab2 = yb2 - yb1;
      abk2 = xb1 - xb2;
      abl2 = (xb2 * yb1) - (xb1 * yb2);
    
      
       denomab = (ab1 * abk2) - (ab2 * abk1);
       
      if (denomab < 0){ 
        offsetab = -denomab / 2; 
      } 
      else {
        offsetab = denomab / 2 ;
      }
      numab = (abk1 * abl2) - (abk2 * abl1);
      if (numab < 0){
        xab = (numab - offsetab) / denomab;
      } 
      else {
        xab = (numab + offsetab) / denomab;
      }
      numab = (ab2 * abl1) - (ab1 * abl2);
      if (numab < 0){
        yab = ( numab - offsetab) / denomab;
      } 
      else {
        yab = (numab + offsetab) / denomab;
      }
      
      //cruzamento AC::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
      
      ac1 = ya2 - ya1;
      ack1 = xa1 - xa2;
      acl1 = (xa2 * ya1) - (xa1 * ya2);
    
      
    
      
      ac2 = yc2 - yc1;
      ack2 = xc1 - xc2;
      acl2 = (xc2 * yc1) - (xc1 * yc2);
    
      
     
      
       denomac = (ac1 * ack2) - (ac2 * ack1);
       
      if (denomac < 0){ 
        offsetac = -denomac / 2; 
      } 
      else {
        offsetac = denomac / 2 ;
      }
      numac = (ack1 * acl2) - (ack2 * acl1);
      if (numac < 0){
        xac = (numac - offsetac) / denomac;
      } 
      else {
        xac = (numac + offsetac) / denomac;
      }
      numac = (ac2 * acl1) - (ac1 * acl2);
      if (numac < 0){
        yac = ( numac - offsetac) / denomac;
      } 
      else {
        yac = (numac + offsetac) / denomac;
      }
      
      //cruzamento DB::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
       db1 = yd2 - yd1;
      dbk1 = xd1 - xd2;
      dbl1 = (xd2 * yd1) - (xd1 * yd2);
    
      
      
      
      db2 = yb2 - yb1;
      dbk2 = xb1 - xb2;
      dbl2 = (xb2 * yb1) - (xb1 * yb2);
    
      
      
      
       denomdb = (db1 * dbk2) - (db2 * dbk1);
       
      if (denomdb < 0){ 
        offsetdb = -denomdb / 2; 
      } 
      else {
        offsetdb = denomdb / 2 ;
      }
      numdb = (dbk1 * dbl2) - (dbk2 * dbl1);
      if (numdb < 0){
        xdb = (numdb - offsetdb) / denomdb;
      } 
      else {
        xdb = (numdb + offsetdb) / denomdb;
      }
      numdb = (db2 * dbl1) - (db1 * dbl2);
      if (numdb < 0){
        ydb = ( numdb - offsetdb) / denomdb;
      } 
      else {
        ydb = (numdb + offsetdb) / denomdb;
      }
     
      
      //cruzamento DC:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      
      
      dc1 = yd2 - yd1;
      dck1 = xd1 - xd2;
      dcl1 = (xd2 * yd1) - (xd1 * yd2);
    
      
     
      
      dc2 = yc2 - yc1;
      dck2 = xc1 - xc2;
      dcl2 = (xc2 * yc1) - (xc1 * yc2);
    
      
     
      
       denomdc = (dc1 * dck2) - (dc2 * dck1);
       
      if (denomdc < 0){ 
        offsetdc = -denomdc / 2; 
      } 
      else {
        offsetdc = denomdc / 2 ;
      }
      numdc = (dck1 * dcl2) - (dck2 * dcl1);
      if (numdc < 0){
        xdc = (numdc - offsetdc) / denomdc;
      } 
      else {
        xdc = (numdc + offsetdc) / denomdc;
      }
      numdc = (dc2 * dcl1) - (dc1 * dcl2);
      if (numdc < 0){
        ydc = ( numdc - offsetdc) / denomdc;
      } 
      else {
        ydc = (numdc + offsetdc) / denomdc;
      }
      return bacalhuncio;
    }
    
      
    }
    
  • Sorry about the genesis testament but you can skip most of the code

  • edited June 2015 Answer ✓

    Only 1 of the sketches can use an OpenGL-based renderer: P2D, P3D, OPENGL.
    In other words, the rest gotta be JAVA2D renderer!

  • that means i have to cut down one sketch, thanks :)

  • Ok i have cut down one sketch and made the changes necessary but when i select one it breaks and says NULL POINTER EXCEPTION

  • edited June 2015

    In my own example, all of them uses JAVA2D.
    And if you change only 1 of them to something else, you'll see they'll still work.
    Only when more than 1 is diff. than JAVA2D it crashes.
    Now I dunno why you got an NPE.
    The code you posted is half green characters and hard to get my brain focused! 8-X

  • edited June 2015
    String[] sketches = getSketchNestedClassNames();
      //for (String sketch : sketches)  main(sketch);
      printArray(sketches);
    
     
    }
    public void letras() {
      if (stage==1) {
        //image(img,0,0,width,height);
        hue=mouseY/3+25;
        cl=color(hue, 100, 100);
        textSize(69);
        
        background(0);
        r=mouseX-width/3;
        x= r*cos(teta);
        y= r*sin(teta);
        
        //ha+=0.01;
       teta+=0.1;
        
        fill(cl);
        
        text("Tripatica", x+width/2, y+height/2);
       
        if (mousePressed)
          stage=2;
      }
      if (stage==2) {
    
        int cselet=180;
    
        int ck1=0, ck2=0, ck3=0;
    
    
        background(0);
        float xs4=width/4, ys4=height/4, ys=ys4+height/2, xs=xs4+height/2;
        textSize(15);
        text("press 'ESC' to exit", width/2-40, height-10);
        //rect(50,(height/3)-30,400,200);
        //image(title, width/2, 10);
        int i=mouseY, a=mouseX, rot1=0, rot2=0, rot3=0, rot4=0;
        if (i>=0 && i<=height/3)
          cselet=270;
        else if (i>=height/3 && i<=height*(2/3))
          cselet=270;
        else if (i>=450)
          cselet=270;
        //fill();
        fill(255);
    
        n++;
        if (n%30==0) {
          kah*=-1;
        }
        if (kah>0) {
          vah++;
          hue2+=9;
        } else if (kah
  • edited June 2015

    can you read all the code i updated? cus i cant, and its not green anymore xD

  • What I see is an incomplete letras() function.
    You didn't say what was wrong with it...

  • well i put all the code in the comment but only that showed up

  • edited June 2015

    I can't guess where the NPE occurred! Is it at image(title, width/2, 10);?
    If so you just need to make sure title holds some PImage reference.
    Although I might say this isn't about "multiple sketches" anymore but rather common NPE bug hunt! :-<

  • edited June 2015

    its at p.frame.show() in the activate sketch

  • well i didnt add to my code the togglesketch class maybe thats it

  • nor the runSketch,

  • I DID IT PROBLEM SOLVED

  • What would I do?

    As you noticed, I've called main() in order to instantiate & ignite all nested PApplet sketches. However, main() is void and doesn't return anything. Therefore, we don't get hold of the references as a way to track those nested sketches! In place of main(), we'll rely on runSketch() from now on. This 1 allows us to pass an already instantiated PApplet as its 2nd argument. As a bonus, sketches don't need to be public nor static as main() demanded! Since the main PApplet sketch was responsible to directly instantiate the other nested 1s, it can save & track their references in variables it has control of! Other key ingredients are setVisible(), noLoop() & loop() methods: http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#setVisible-boolean- https://processing.org/reference/noLoop_.html https://processing.org/reference/loop_.html With them, we can pause/resume & hide/show any PApplet which we hold the reference! Check out latest (v2.0) below. Any doubts just ask here again:

    /**
     * Multiple Nested PApplets (v2.0)
     * by GoToLoop (2015/May/21)
     *
     * forum.processing.org/two/discussion/10937/multiple-sketches
     * forum.processing.org/two/discussion/7036/multiple-screens
     */
     
    // Main PApplet Sketch:
    static final int FPS = 60, TOGGLE = 5*FPS; // 5 seconds
     
    final PApplet another = new AnotherSketch(), bouncy = new Bouncy();
     
    void setup() {
      size(300, 300, JAVA2D);
      smooth(4);
      frameRate(FPS);
     
      textFont(createFont("Serif", 32, true));
      textAlign(CENTER, CENTER); 
      fill(120, 20, 20);
     
      // Instantiating All Nested PApplet Sketches:
      String[] sketches = getSketchNestedClassNames();
      //for (String sketch : sketches)  main(sketch);
      printArray(sketches);
     
      //runSketch(new String[] { ARGS_FULL_SCREEN, "Full Window" }, another);
      runSketch(new String[] { "My Hello Window" }, another);
      runSketch(new String[] { "My Bouncy Window" }, bouncy);
    }
     
    void draw() {
      background(255, 200, 200); 
      translate(width>>1, height>>1);
      rotate(frameCount*.01);
      text("Hello world", 0, 0);
     
      if (frameCount % TOGGLE == 0)  toggleSketch(bouncy);
    }
     
    void keyPressed() {
      int k = keyCode;
     
      if (k == ENTER | k == RETURN)  activateSketch(another);
      else if (k == ' ')             disableSketch(another);
    }
     
    // Util Functions for Hiding/Displaying/Toggling Sketches:
    static final void disableSketch(PApplet p) {
      p.frame.hide();
      p.noLoop();
    }
     
    static final void activateSketch(PApplet p) {
      p.frame.show();
      p.loop();
    }
     
    static final void toggleSketch(PApplet p) {
      boolean isActive = p.frame.isVisible();
      p.frame.setVisible(!isActive);
     
      if (isActive)  p.noLoop();
      else           p.loop();
    }
     
    // Util Functions for Nested PApplet Sketches:
    static final String getSketchClassName() {
      return Thread.currentThread().getStackTrace()[1].getClassName();
    }
     
    static final String[] getSketchNestedClassNames() {
      Class[] nested;
     
      try {
        nested = Class.forName(getSketchClassName()).getClasses();
      }
      catch (ClassNotFoundException cause) {
        throw new RuntimeException(cause);
      }
     
      int idx = 0, len = max(0, nested.length - 2);
      String[] classes = new String[len];
     
      while (idx != len)  classes[idx] = nested[idx++].getName();
      return classes;
    }
     
    // Nested PApplet Class A:
    public static final class AnotherSketch extends PApplet {
      void setup() {
        size(300, 150, JAVA2D);
        smooth(4);
     
        textFont(createFont("SansSerif", 24, true));
        textAlign(CENTER, CENTER);
        fill(20, 120, 20);
      }
     
      void draw() {
        background(200, 255, 200);
        translate(width>>1, height>>1);
        scale(.1 + sin(frameCount*.02), 1);
        text("Hello again", 0, 0);
      }
    }
     
    // Nested PApplet Class B:
    public static final class Bouncy extends PApplet {
      /** 
       * Bouncy Words (v3.04)
       * by tfguy & jtwhite14 (2013/Aug)
       * mod GoToLoop
       * 
       * forum.processing.org/topic/animated-text
       * studio.processingtogether.com/sp/pad/export/ro.9eHY5Fel1e0Jr/latest
       */
     
      static final int NUM = 3;
      final BouncyWord[] words = new BouncyWord[NUM];
     
      void setup() {
        size(300, 200, JAVA2D);
        smooth(4);
     
        fill(BouncyWord.INK);
        textAlign(LEFT, CENTER);
     
        textFont(createFont("Trebuchet MS Bold", BouncyWord.DIM, true));
     
        words[0] = new BouncyWord("History", -1.5, 0, 50);
        words[1] = new BouncyWord("Design", 2.3, 0, 100);
        words[2] = new BouncyWord("Studio", -3, 0, 150);
      }
     
      void draw() {
        background(-1);
        for (BouncyWord w : words)  w.bounce();
      }
     
      class BouncyWord {
        static final short DIM = 90;
        static final color INK = 0100 << 030;
     
        String word;
        float px, py, vx, vy;
     
        BouncyWord(String iword, float ivy, float ipx, float ipy) {
          word = iword;
          vy = ivy;
          px = ipx;
          py = ipy;
        }
     
        void bounce() {
          if ((py += vy) >= height - (DIM>>2) | py < (DIM>>2))  vy *= -1;
          text(word, px, py);
        }
      }
    }
    

    But when one of those windows is closed, it kills the entire program... how might one handle closing any of the three windows individually without killing the program?

  • Is there any solution for this problem running in P3 resp. a version of Multiple Nested PApplets by GoToLoop for P3?

  • edited August 27

    Just move all size() & smooth() to settings() for P3:
    https://Processing.org/reference/settings_.html

      void settings() {
        size(300, 200, JAVA2D);
        smooth();
      }
    
Sign In or Register to comment.