Error to close the window with ControlP5

Hello guys,

I open two windows using the buttons have controlP5 library, the first is a menu where calls this second window ... I created a button in second window, instead of typing ESC, the same can press in the button to close the window. When pressed the keys ESC, this code works normal, that is, closes the window and returns to the menu normally. But however when chamos the same function as used in the ESC, with the button, the following error occurs:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 4
    at java.util.Vector.get(Unknown Source)
    at controlP5.ControllerList.get(Unknown Source)
    at controlP5.ControllerGroup.setMousePressed(Unknown Source)
    at controlP5.ControlWindow.mouseReleasedEvent(Unknown Source)
    at controlP5.ControlWindow.mouseEvent(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1236)
    at processing.core.PApplet.handleMethods(PApplet.java:1431)
    at processing.core.PApplet.handleMouseEvent(PApplet.java:2826)
    at processing.core.PApplet.dequeueEvents(PApplet.java:2725)
    at processing.core.PApplet.handleDraw(PApplet.java:2397)
    at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240)
    at processing.core.PApplet.run(PApplet.java:2256)
    at java.lang.Thread.run(Unknown Source)

The second window was closed, but an error occurs in the first window, as if he could no longer run something ... Any idea how to solve this?

Note: I will leave part of the code below, but it will not work, because it is only a part.

First Window ( Menu )

static class Video02 extends PApplet {
  atualizaVar atualizaVarVideo02 = new atualizaVar(0,0);
  ControlP5 cp5_Video02; 

  int savedTimeVideo02, totalTimeVideo02 = 5000;
  float pontoAtuVideo02 = 0,
  int bar_sizeVideo02   = 15;
  int contBtn02         = 0;
  int menu02            = 1;
  int qtdeMenus02       = 1;
  int menuAtivo02       = 1;

  PVector v1_Video02;
  String[][] botoesMenu02;

  XML xml_Video02; //Variável para carregar arquivo XML
  ArrayList targetsVideo02 = new ArrayList();

  boolean retornaMenu = false;

  //Configurações Iniciais
  void setup(){
    size(displayWidth, displayHeight, JAVA2D); //Define tamanho da Janela, e forma de renderização
    background(255);             //Limpa tela
    smooth();                    //Suaviza serrilhado nos contornos do desenho
    textFont(utilVar.fontA, 40); //Define e de fonte e Tamanho
    fill(#1273C1);               //Seta cor para o Texto

    cp5_Video02 = new ControlP5(this); //Instancia objeto ControlP5 ( Biblioteca Gráfica )
    criaObjetosVideo02();

    utilVar.tempoAutClick = true;
    savedTimeVideo02      = millis();
    v1_Video02            = new PVector(0, 0);
    rob.mouseMove(30, height-100);

    cursor(HAND);
  }

  //Loop enquanto rodar o programa
  void draw(){

    background(255);
    textFont(utilVar.fontA, 40);
    fill(#1273C1);
    text("Escolha uma Opção para selecionar o Vídeo...", 70, 40);

    if (utilVar.tempoAutClick && focused){
      if (Collections.disjoint(cp5_Video02.getMouseOverList(), targetsVideo02)==false) {
        if(cp5_Video02.getMouseOverList().size()==1) {
          controlP5.Controller controllerVideo02 = (controlP5.Controller)(cp5_Video02.getMouseOverList().get(0));

          String posicoes = controllerVideo02.getLabel().toString();
          String posBtnX  = posicoes.substring(0,4); 
          String posBtnY  = posicoes.substring(5,posicoes.length());

          desenhaBarraVideo02(int(posBtnX)-17, int(posBtnY)+150, 5, 150);
        }
      } else {
        savedTimeVideo02 = millis();
        pontoAtuVideo02  = 0;
      }
    } else {
      savedTimeVideo02 = millis();
      pontoAtuVideo02  = 0;
    }

    if (!utilVar.executando3 && utilVar.usaLeapMotion){
      atualizaVarVideo02.atualizaLeap(utilVar.opcaoLeapMotion);
      rob.mouseMove(atualizaVarVideo02.xPos, atualizaVarVideo02.yPos);
    } else {
      atualizaVarVideo02.xPos = mouseX;
      atualizaVarVideo02.yPos = mouseY;
    }  
  }

  void mouseMoved(){   

  }

  void mousePressed(){
    utilVar.pressMouse3 = true;
  }

  void keyPressed() {
    if ( key == ESC ){
      retornaMenu();
      desabilitaDisp();
    }
 }

  void retornaMenu(){
    key = 0;
    this.stop();
    this.dispose();
    frame.hide();
    frame                = null;
    cp5_Video02          = null;
    atualizaVarVideo02   = null;
    utilVar.pressMouse3  = false;
    utilVar.executando2  = false;
    desabilitaDisp();
  }

  void desenhaBarraVideo02(int pW, int pH, int perc, int pontoMax){
    color c = color(0, 126, 255, 150);

    if (pontoAtuVideo02 < pontoMax){
      pontoAtuVideo02 = pontoAtuVideo02 + utilVar.tempoAutClickMouse; //Conforme calibrar variával ( tempoAutClickMouse ), muda o tempo do Click automático
      fill (c);
      rect(pW, pH, bar_sizeVideo02, pontoAtuVideo02*-1 );
    } else { 
      pontoAtuVideo02 = 0;
      cliCkMouse();
      utilVar.pressMouse2 = true;
    }
  }

  void controlEvent(ControlEvent theControlEvent) {
    if (utilVar.pressMouse3){
      utilVar.opcaoLink = int(theControlEvent.controller().getValue());
      String nomeBotaoVideo02  = theControlEvent.controller().getName(); 

      if (nomeBotaoVideo02 == "btnProximoVideo02" || nomeBotaoVideo02 == "btnAnteriorVideo02" || nomeBotaoVideo02 == "btnVoltarVideo02"){
        if (nomeBotaoVideo02 == "btnProximoVideo02"){
          if (menuAtivo02 < menu02){ 
            menuAtivo02++;
            habilitaDesabilitaMenu02(menuAtivo02);
          }  
        } else if (nomeBotaoVideo02 == "btnAnteriorVideo02"){
          if (menuAtivo02 > 1){ 
            menuAtivo02--;
            habilitaDesabilitaMenu02(menuAtivo02);
          }
        } else if (nomeBotaoVideo02 == "btnVoltarVideo02"){ //Here is Called Idem press key ESC
          println("Volta 1");
          key = ESC;
          retornaMenu();
        } 
      } else {
        utilVar.imgPai      = int(theControlEvent.controller().getStringValue());
        utilVar.executando3 = true;    

        PApplet video03 = new Video03();  
        runSketch(new String[] { "***Vídeos***" }, video03);
      }  
    }
  }

  void criaObjetosVideo02(){
    xml_Video02 = loadXML(PATHIMGMENUVIDEOS + "/" + utilVar.idPessoa +"/pessoasMenuVideos.xml");
    XML[] children = xml_Video02.getChildren("menuVideo");

    botoesMenu02  = new String[children.length][2];

    for (int i = 0; i < children.length; i++) {
      int idButton    = children[i].getInt("id");
      String idImagem = children[i].getString("id");
      int opcLink     = children[i].getInt("opcaoLink"); 
      String urlLink  = children[i].getString("linkURL");

      contBtn02++;    
      adicionaPosBtnXY(i, opcLink, contBtn02);
      //println("Path " + PATHIMGMENUVIDEOS + "/" + idPessoa + "/" + idImagem);
      cp5_Video02.addButton("btn"+nf(idButton,6))
                 .setId(idButton)
                 .setPosition(utilVar.posBtnX, utilVar.posBtnY)
                 .setImages(loadImage(PATHIMGMENUVIDEOS+"/"+utilVar.idPessoa+"/"+idImagem+"_0.jpg"), loadImage(PATHIMGMENUVIDEOS+"/"+utilVar.idPessoa+"/"+idImagem+"_1.jpg"), loadImage(PATHIMGMENUVIDEOS+"/"+utilVar.idPessoa+"/"+idImagem+"_2.jpg"))
                 .setStringValue(idImagem)
                 .setValue(opcLink)
                 .setLabel(nf(utilVar.posBtnX,4) + "," + nf(utilVar.posBtnY,4))
                 .setVisible(false)
                 .updateSize();

      targetsVideo02.add(cp5_Video02.getController("btn"+nf(idButton,6)));

      botoesMenu02[i][0] = "btn"+nf(idButton,6);
      botoesMenu02[i][1] = Integer.toString(menu02);

    }//for

    cp5_Video02.addButton("btnVoltarVideo02")
               .setPosition(2, 2)
               .setImages(loadImage(PATHDADOS + "/voltar0.png"), loadImage(PATHDADOS + "/voltar1.png"), loadImage(PATHDADOS + "/voltar2.png"))
               .updateSize();

    targetsVideo02.add(cp5_Video02.getController("btnVoltarVideo02"));

    habilitaDesabilitaMenu02(1);
  }

  void habilitaDesabilitaMenu02(int opcaoMnu){
    for (int i = 0; i < botoesMenu02.length; i++) {
      cp5_Video02.getController(botoesMenu02[i][0]).setVisible((int(botoesMenu02[i][1]) == opcaoMnu));
    }
  }
}//função 

Only code execute on exit second window

public static final class Video03 extends PApplet {
...

void keyPressed() {
    if ( key == ESC ){
      retornaMenu();
      desabilitaDisp();
    }
}

void controlEvent(ControlEvent theControlEvent) {
    if (utilVar.pressMouse3){
      String nomeBotao03  = theControlEvent.controller().getName(); 
      if (nomeBotao03 == "btnVoltarVideo03"){
        key = ESC;
        retornaMenu();
      }
    }

void retornaMenu(){
    //if (key == ESC ){
      key = 0;
      this.stop();
      this.dispose();
      frame.hide();
      frame               = null;
      //cp5_Video03         = null;
      atualizaVarVideo03  = null;
      utilVar.pressMouse2 = false;
      utilVar.pressMouse3 = false;
      utilVar.executando3 = false;
      desabilitaDisp();
    //}
  }

Summing up, the function retornaMenu() works on the ESC key, but not when triggered by controlP5, generating the error. I hope I have managed to explain ...

Thanks for listening.

Sign In or Register to comment.