Event Mouse in a second window with G4P

Hello guys,

Can anyone help as I could control click the button of each button, a second window with G4P?

Below is the sample code ...

import g4p_controls.*;
GWindow window;
GButton btnTest, btnTest2;

public void setup() {
  size(160, 160);
  background(255);

  window = new GWindow(this, "Second Window", 130, 100, 400, 320, false, JAVA2D);
  window.addDrawHandler(this, "windowDraw");
  window.addMouseHandler(this, "windowMouse");
  window.addKeyHandler(this, "keyEvent");
  window.setActionOnClose(G4P.CLOSE_WINDOW);
  window.setLocation(250, 20);

  btnTest  = new GButton(window.papplet, 10, 50, 120, 30, "Test");
  btnTest2 = new GButton(window.papplet, 10, 90, 120, 30, "Test 2");
}

public void draw() {
  background(240);
}

public void windowDraw(GWinApplet appc, GWinData windata) {
  appc.background(255);
}

public void windowMouse(GWinApplet appc, GWinData windata, MouseEvent event) {
  if (event.getAction() == MouseEvent.CLICK) //?? 
    println("Mouse event in extra window " + frameRate);
}

public void keyEvent(GWinApplet appc, GWinData data, KeyEvent event) {
  if ((event.getAction() == KeyEvent.PRESS) && (appc.keyCode == ESC)) {
    //appc.key = 0;
    appc.keyCode = 0; //??
    println("CTRL Pressed!");
  }
}

More one detail can "cancel" the ESC key, not to close the window?

Thank you

Answers

  • To keep the window open use

    window.setActionOnClose(G4P.KEEP_OPEN);

    Can anyone help as I could control click the button of each button, a second window with G4P?

    I don't understand the question.

  • Hello Peter, thanks for the feedback ...

    Sorry the English, but thankfully we have the google translate, but could not say almost nothing ... (laughs) ...

    I need to control the Click of each separate button when not in a second window, it works like regular ...

    void handleButtonEvents(GButton button, GEvent event) {
      if ( event == GEvent.CLICKED){
        if (button == btnTest)  
          println("Click btnTest"); 
        else if (button == btnTest2)  
          println("Click btnTest 2"); 
    }
    

    But when I use a second window, I can not do the same validation as would the code, it would be possible to do the same control?

        public void windowMouse(GWinApplet appc, GWinData windata, GButton button, MouseEvent event) {
          if (event.getAction() == MouseEvent.CLICK && button == btnTest) 
            println("Click in the Button btnTest ");
          else if (event.getAction() == MouseEvent.CLICK && button == btnTest) 
            println("Click in the Button btnTest2 ");
        }
    

    This second code does not work, I would like to know how to be able to work ...

    Thank you for attention

  • In the case of the second window, I only wanted to close the current window (not the entire application), or simply "cancel" the ESC, to not close any application.

    //It works OK as well, in first window
    void keyPressed(){
      if (key == ESC) key = 0;  
    }
    

    However, this code does not seem to work in the second window.

    public void keyEvent(GWinApplet appc, GWinData data, KeyEvent event) {
      if ((event.getAction() == KeyEvent.PRESS) && (appc.keyCode == ESC)) {
        //appc.key = 0;
        appc.keyCode = 0; //??
        println("ESC Pressed!");
      }
    }
    

    Thank you again

  • You are using Processing 2.1.1 why not Processing 3?

  • I have created an example where the ESC key is ignored in the second window

    import g4p_controls.*;
    
    public void setup() {
      size(480, 320, JAVA2D);
      createGUI();
    }
    
    public void draw() {
      background(230);
    }
    
    void handleButtonEvents(GButton button, GEvent event) {
      if ( event == GEvent.CLICKED) {
        if (button == button1)  
          println("Click button 1"); 
        else if (button == button2)  
          println("Click button 2");
      }
    }
    
    //It works OK as well, in first window
    void keyPressed() {
      if (key == ESC) key = 0;
    }
    
    // Now works in the second window
    public void keyEventHandler(GWinApplet appc, GWinData data, KeyEvent kevent) {
      if (appc.key == ESC) appc.key = 0;
    }
    
    public void win_draw1(GWinApplet appc, GWinData data) {
      appc.background(230);
    }
    
    public void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      if (frame != null)
        frame.setTitle("Sketch Window");
      window1 = new GWindow(this, "Window title", 0, 0, 360, 240, false, JAVA2D);
      window1.addDrawHandler(this, "win_draw1");
      window1.addKeyHandler(this, "keyEventHandler");
      button1 = new GButton(window1.papplet, 50, 20, 80, 30);
      button1.setText("Button 1");
      button2 = new GButton(window1.papplet, 220, 20, 80, 30);
      button2.setText("Button 2");
    }
    
    // Variable declarations 
    // autogenerated do not edit
    GWindow window1;
    GButton button1; 
    GButton button2; 
    
  • Hello Peter, thanks for the help ...

    OK, so I can use the controls of buttons on different screens, just a detail I have to solve in the first screen I use GImageButton, and the second use GButton ...

    Just to complicate matters a bit does not it? ( laughs )...

    It is the first screen is a menu where the user will choose the options that open certain settings screen, registers, etc ... below is the print through a better idea.

    We are developing an application, where we put several options for animations, simple games and other activities ... This is another application to make the settings, entries, as I mentioned ...

    How could I control the events GImageButton and GButton separately?

    Code Snippet

    void handleButtonEvents( GImageButton buttonImg, GEvent event) {
      if ( event == GEvent.CLICKED) {
        if (buttonImg == btnCadastroUsuario) { 
           MsgBox(1, "Opção Não Habilitada!", "C.A.L. Anima - ADM");
          //criaObjTelaUsuario();
        } else if (buttonImg == btnCadastroPrancha) { 
          setupPrancha();
          criaObjTelaPrancha();
        } else if (buttonImg == btnConfiguracoes) { 
          MsgBox(1, "Opção Não Habilitada!", "C.A.L. Anima - ADM");
          //criaObjTelaConfiguracoes();
        } 
      }
    }
    

    Menu

    Tela001

    Thank you very much!

  • What about using the Processing 3, had some problem with some libraries, including G4P changed a few things, I can compile some programs ...

    For example, this copies a clipboard image in Processing 2, 3 is not recognizing.

    I need to see how to resolve this, Processing 3 really is with many improvements ...

    I know it has nothing to do with the subject we are seeing, but just to get a better idea of the error, the print is below ...

    Tela002

    Thank you

  • void handleButtonEvents( GImageButton buttonImg, GEvent event) {
        // For image buttons
    }
    
    public void handleButtonEvents(GButton button, GEvent event) {
        // For simple buttons
    }
    

    It is possible for every control to have its own event handler.

    For instance if we have 2 buttons

    GImageButton imgButton1; 
    GButton button1; 
    

    then after they are created you can add event handlers like this

    imgButton1.addEventHandler(this, "imgButton1_click1");
    button1.addEventHandler(this, "button1_click1");
    

    and the event handlers would look like this

    public void imgButton1_click1(GImageButton source, GEvent event) { 
      // code for image button
    } 
    
    public void button1_click1(GButton source, GEvent event) {
       // code for button
    } 
    

    I see you are using Processing 3.5.4 which is the last version compatible for Processing 2. There will be no more updates or bug fixes for this version of G4P.

  • Hi Peter,

    OK, it worked perfectly!

    I'll see if I can update to the Application for Processing 3.

    Thank you very much! :)

  • I'll see if I can update to the Application for Processing 3.

    Beware that if you go to Processing V3 you will also have to use G4P 4.0.5 which will some changes in the code.

  • OK, I've adjusted it, G4P worked usual ... now need to update SQLite, I use to load data ...

    I'll open another post, it seems that SQLite is not updated to Processing 3.

    Meanwhile'll Processing using 2 ...

    Thank you very much.

Sign In or Register to comment.