How to close the 2nd frame and get back to the main one

edited September 2014 in How To...

Hi everyone, I write a program that has one main window which displayed letters on it, if the letters form a word a 2nd frame will be opened displaying an image. The problem that I face is when i close the 2nd frame and i want to return back to the main one it seems freeze. I can't do any thing there while the program is still working "I can know that from the console" . What I need is to clear the 1st window and continue displaying letters.

This is the setup for the main window

void setup()
{
  //size(screen.width,screen.height);

  size(640,480); 
  noStroke();
  fill(0);

  frameRate(30);
  noLoop();

  hint(ENABLE_NATIVE_FONTS);
  font = createFont("Arial", 30);
  scale_factor = height/table_size;


  // we create an instance of the TuioProcessing client
  // since we add "this" class as an argument the TuioProcessing class expects
  // an implementation of the TUIO callback methods (see below)
   tuioClient  = new TuioProcessing(this);
   wordImag = loadImage("catImage.jpg");
   wordImag2 = loadImage("actImage2.png");
   wordImag3 = loadImage("batImage.png");
   wordImag4 = loadImage("tabImage.png");
   wordImag5 = loadImage("fatImage.jpg");

   minim = new Minim(this) ;
   au_player1 = minim.loadFile("Alert.wav") ;

}

This is the 2nd frame code

public class secondApplet extends PApplet {

  public void setup() {
      size(400, 400, P3D);
      frameRate(30);
      noLoop(); 

  }

   public void draw() {
   fill(0);
   if (word==1) 
       {
       image(wordImag, 0, 0);
       noLoop();
       }

   else
    if (word ==2)
      {
      image(wordImag2, 0, 0);
      noLoop();
      }
   else
    if (word ==3)
      {
      image(wordImag3, 0, 0);
      noLoop();
      }
      else
    if (word ==4)
      {
      image(wordImag4, 0, 0);
      noLoop();
      }
       else
    if (word == 5)
      {
      image(wordImag5, 0, 0);
      noLoop();
      }

  }
}

public class PFrame extends JFrame {
  public PFrame() {

    setBounds(0, 0, 350, 380);
    s = new secondApplet();
    //s.setLocation(0,0);
    add(s);
    s.init();
    show();
    //noLoop();
     stop();

}
}

Answers

  • You have noLoop(); and no draw() method in the main applet. A sketch must have its own draw method and be looping if it is to respond to events.

  • I have a draw method but i didn't post it. Thanks GoToLoop for sharing the like but i don't like to use the G4 library. Here is the whole code of my application

    // we need to import the TUIO library
    // and declare a TuioProcessing client variable
    import TUIO.*;
    import g4p_controls.*;
    import java.util.Vector;
    import javax.swing.JFrame;
    
    // for sound
    import ddf.minim.* ;
    
    import java.awt.*;
    import java.awt.event.*;
    
    // variables declaration
    Minim minim;
    AudioPlayer au_player1 ;
    
    
    TuioProcessing tuioClient;
    
    // these are some helper variables which are used
    // to create scalable graphical feedback
    float cursor_size = 15;
    float object_size = 60;
    float table_size = 760;
    float scale_factor = 1;
    PFont font;
    PImage wordImag;
    PImage wordImag2;
    PImage wordImag3;
    PImage wordImag4;
    PImage wordImag5;
    int word =0;
    float location1 = 0, location2 = 0, location3 = 0 ,location1a = 0.0, location2a =0.0, location3a = 0.0, d = 0.02;
    float locd1p = 0.0, locd1m = 0.0;
    boolean letterC = false, letterB = false , letterF = false;
    PFrame f;
    secondApplet s;
    
    
    
    void setup()
    {
      //size(screen.width,screen.height);
    
      size(640,480); 
      noStroke();
      fill(0);
    
      frameRate(30);
      noLoop();
    
      hint(ENABLE_NATIVE_FONTS);
      font = createFont("Arial", 30);
      scale_factor = height/table_size;
    
    
      // we create an instance of the TuioProcessing client
      // since we add "this" class as an argument the TuioProcessing class expects
      // an implementation of the TUIO callback methods (see below)
       tuioClient  = new TuioProcessing(this);
       wordImag = loadImage("catImage.jpg");
       wordImag2 = loadImage("actImage2.png");
       wordImag3 = loadImage("batImage.png");
       wordImag4 = loadImage("tabImage.png");
       wordImag5 = loadImage("fatImage.jpg");
    
       minim = new Minim(this) ;
       au_player1 = minim.loadFile("Alert.wav") ;
    
    }
    
    
    // New Frame*******************
    
    public class secondApplet extends PApplet {
    
      public void setup() {
          size(400, 400, P3D);
          frameRate(30);
          noLoop(); 
    
      }
    
    
    
    
      public void draw() {
       fill(0);
       if (word==1) 
           {
           image(wordImag, 0, 0);
           noLoop();
           }
    
       else
        if (word ==2)
          {
          image(wordImag2, 0, 0);
          noLoop();
          }
       else
        if (word ==3)
          {
          image(wordImag3, 0, 0);
          noLoop();
          }
          else
        if (word ==4)
          {
          image(wordImag4, 0, 0);
          noLoop();
          }
           else
        if (word == 5)
          {
          image(wordImag5, 0, 0);
          noLoop();
          }
    
    
    
    
      }
    }
    
    public class PFrame extends JFrame {
      public PFrame() {
    
        setBounds(0, 0, 350, 380);
        s = new secondApplet();
        //s.setLocation(0,0);
        add(s);
        s.init();
        show();
        noLoop();
         stop();
          //dispose();
          //s.redraw();
    
      }
    }
    
    
    // End of new Frame ***********
    
    
    // within the draw method we retrieve a Vector (List) of TuioObject and TuioCursor (polling)
    // from the TuioProcessing client and then loop over both lists to draw the graphical feedback.
    void draw()
    {
    
      background(255);
      textFont(font,70*scale_factor);
      float obj_size = object_size*scale_factor; 
      float cur_size = cursor_size*scale_factor;
    
    
      text("_____________________________________________",0,255);
    
    
      Vector tuioObjectList = tuioClient.getTuioObjects();
      for (int i=0;i<tuioObjectList.size();i++) {
         TuioObject tobj = (TuioObject)tuioObjectList.elementAt(i);
         stroke(0);
         fill(0);
         pushMatrix();
         translate(tobj.getScreenX(width),tobj.getScreenY(height));
         rotate(tobj.getAngle());
         //rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
         popMatrix();
         //fill(255);
         fill(25);
        // text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
         if(tobj.getSymbolID() == 0)
           {
             text ("C",tobj.getScreenX(width), tobj.getScreenY(height));
    
             letterC = true;
             location1 = tobj.getX();
             location1a = tobj.getY();
             locd1p = tobj.getY() + d;
             locd1m = tobj.getY() - d;
           }
           if(tobj.getSymbolID() == 3)
           {
             text ("B",tobj.getScreenX(width), tobj.getScreenY(height));
    
             letterB = true;
             location1 = tobj.getX();
             location1a = tobj.getY();
             locd1p = tobj.getY() + d;
             locd1m = tobj.getY() - d;
           }
           if(tobj.getSymbolID() == 4)
           {
             text ("F",tobj.getScreenX(width), tobj.getScreenY(height));
    
             letterF = true;
             location1 = tobj.getX();
             location1a = tobj.getY();
             locd1p = tobj.getY() + d;
             locd1m = tobj.getY() - d;
           }
    
         if(tobj.getSymbolID() == 1)
         {
            text ("A",tobj.getScreenX(width), tobj.getScreenY(height));
            location2 = tobj.getX();
            location2a = tobj.getY();
         }
    
         if(tobj.getSymbolID() == 2)
         {
            text ("T",tobj.getScreenX(width), tobj.getScreenY(height));
             location3 = tobj.getX();
             location3a = tobj.getY();
         }
    
    
    
    
       } // end for
    
    
    
    
       Vector tuioCursorList = tuioClient.getTuioCursors();
       for (int i=0;i<tuioCursorList.size();i++) {
          TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
          Vector pointList = tcur.getPath();
    
          if (pointList.size()>0) {
            stroke(0,0,255);
            TuioPoint start_point = (TuioPoint)pointList.firstElement();;
            for (int j=0;j<pointList.size();j++) {
               TuioPoint end_point = (TuioPoint)pointList.elementAt(j);
               line(start_point.getScreenX(width),start_point.getScreenY(height),end_point.getScreenX(width),end_point.getScreenY(height));
               start_point = end_point;
            }
    
            stroke(192,192,192);
            fill(192,192,192);
            ellipse( tcur.getScreenX(width), tcur.getScreenY(height),cur_size,cur_size);
            fill(0);
            text(""+ tcur.getCursorID(),  tcur.getScreenX(width)-5,  tcur.getScreenY(height)+5);
          }
       }
    
    
    
       //*********************
    
       if(location1 != 0 && location2 != 0 && location3 !=0)
         {
    
    
         if (location1 < location2 && location2 < location3)
         {
           println("less "+locd1m);
           println("plus "+locd1p);
    
           println("1Y "+location1a);
           println("2Y "+location2a);
           println("3Y "+location3a);
    
           if ((locd1m < location2a) && (location2a < locd1p)) 
            {
             // println("if 1");
    
            if ((locd1m < location3a) && (location3a < locd1p))
             {
    
            // println("if 2");
             if(letterC)
                {
                  text ("You got the word CAT",100, 40);
                  au_player1.play() ;
                  word =1;
                  PFrame f = new PFrame();
    
                }
              else
              if( letterB)
              {
                  au_player1.play() ;
                  text ("You got the word BAT",100, 40);
                  word =3;
                  PFrame f = new PFrame();
              } 
              else
              if( letterF)
              {
                  au_player1.play() ;
                  text ("You got the word FAT",100, 40);
                  word = 5;
                  PFrame f = new PFrame();
              }
    
           }
            }
         }
         else
         if((location2 < location1 && location1 < location3) && (((location1a - d) < location2a && location2a < (location1a + d)) && ((location1a - d) < location3a && location3a < (location1a + d))))
         {     
               au_player1.play() ;
               text ("You got the word ACT",100, 40);
               word = 2;
               PFrame f = new PFrame();
    
         }
        else
        if(( location3 < location2 && location2 < location1) && (((location1a - d) < location2a && location2a < (location1a + d)) && ((location1a - d) < location3a && location3a < (location1a + d))))
         {     
               au_player1.play() ;
               text ("You got the word TAB",100, 40);
               word = 4;
               PFrame f = new PFrame();
    
         }
    
    
       //*********************
    
         }  
    
    }
    
    
    // these callback methods are called whenever a TUIO event occurs
    
    // called when an object is added to the scene
    void addTuioObject(TuioObject tobj) {
      println("add object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
    
    }
    
    // called when an object is removed from the scene
    void removeTuioObject(TuioObject tobj) {
      println("remove object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
    
    
    }
    
    // called when an object is moved
    void updateTuioObject (TuioObject tobj) {
      //println("update object "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle()
      //        +" "+tobj.getMotionSpeed()+" "+tobj.getRotationSpeed()+" "+tobj.getMotionAccel()+" "+tobj.getRotationAccel());
    }
    
    // called when a cursor is added to the scene
    void addTuioCursor(TuioCursor tcur) {
      println("add cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY());
    }
    
    // called when a cursor is moved
    void updateTuioCursor (TuioCursor tcur) {
      println("update cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+ ") " +tcur.getX()+" "+tcur.getY()
              +" "+tcur.getMotionSpeed()+" "+tcur.getMotionAccel());
    }
    
    // called when a cursor is removed from the scene
    void removeTuioCursor(TuioCursor tcur) {
      println("remove cursor "+tcur.getCursorID()+" ("+tcur.getSessionID()+")");
    }
    
    // called after each message bundle
    // representing the end of an image frame
    void refresh(TuioTime bundleTime) { 
      redraw();
    
    
    }
    

    the program works fine but the problem is the window if the 2nd window opened to view a picture the first window looks freeze so i can't continue the game unless i restart the program. PLEASE advise me on that I've been stuck on this problem for more that 2 weeks. Thanks in advance

  • ... but I don't like to use the G4 library.

    G4? Is it G4P anyways? Nevertheless, that link is about instantiating multiple sketches w/o any library import! >:/
    There's no import present in my sketch example there! [-(

Sign In or Register to comment.