G4P GWindow noLoop?

edited April 2016 in Library Questions

http://www.lagers.org.uk/g4p/ref/classg4p__controls_1_1_g_window.html
After reading the reference for GWindow, it appears that there is no access to GWindow.settings() or GWindow.setup()
What is the correct approach to drawing only when required?

Answers

  • edited April 2016 Answer ✓

    This sketch demonstrates one way to do what you want. Click the mouse on the main sketch window and the second window is updated.

    import g4p_controls.*;
    
    GWindow window1;
    float x0, y0, x1, y1;
    
    public void setup() {
      size(480, 320, JAVA2D);
      createGUI();
    }
    
    public void draw() {
      background(230);
    }
    
    void mouseReleased() {
      x0 = random(window1.width);
      y0 = random(window1.height);
      x1 = random(window1.width);
      y1 = random(window1.height);
      window1.redraw();
    }
    
    public void win_draw1(PApplet appc, GWinData data) {
      appc.background(230);
      appc.fill(0);
      appc.stroke(100, 200, 100);
      appc.strokeWeight(2);
      appc.line(x0, y0, x1, y1);
      appc.text(millis(), 10, 20);
      appc.noLoop();
    }
    
    void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      surface.setTitle("Sketch Window");
      window1 = GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
      window1.noLoop();
      window1.addDrawHandler(this, "win_draw1");
      window1.loop();
    }
    
  • Interesting. That sketch you posted has raised some specific questions. This is the first time I'm using your library so my apologies if some are answered elsewhere.

    Question 1:

    void otherThreadEvent() {
      window1.redraw(); // Looks normal, and the condition I need.
      // Executes the code within draw() one time. reference/redraw_.html
    }
    
    void win_draw1(PApplet appc, ...) {
      // code.
      appc.noLoop(); // Why is this at the end of win_draw1?
    }
    
    void setup() {
      window1 = GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
      window1.noLoop();
      window1.addDrawHandler(this, "win_draw1");
      window1.loop(); // Why is this done if .redraw() & .noLoop() are used?
    }
    



    Question 2:

    void win_draw1(PApplet appc, GWinData data) { 
      appc.line(x0, y0, x1, y1); 
    }
    

    data is unused object, yet floats are updated.
    This is different than everything I've read so far for how the data is distributed, like:

     void win_draw1(PApplet appc, GWinData data) { 
          MyWinData data2 = (MyWinData)data;
          appc.line(data2.x0, data2.y0, data2.x1, data2.y1); 
        }
    


  • Answer ✓

    Ok to answer your questions.

    Q1 The noLoop / loop in the setup method were added by GUI Builder. It fixes a problem when large numbers of G4P controls are added to the GWindow. In this case these statements can be deleted.

    The noLoop must be put somewhere, by putting it at the end of the draw method it will stop the window looping from the start. On the main sketch window you would put it in setup but GWindow does not support settings or setup because a sketch only has one of each.

    Q2 the GWindow draw and event handlers can access any global variable. The data object is optional, it allows each GWindow to have its own data. The choice is yours.

  • edited April 2016

    Alright, everything makes sense to me now. Thank you very much quark! :-bd

  • So this noLoop thing is still looping once.

            int count = 0;
            void drawWinHL(PApplet a, GWinData data) {
              a.background(0);
              count += 5;
              println(count);
              a.noLoop();
            }  
    


    Prints:

    5
    10
    

    Is the easiest fix to just have a boolean runOnce if condition?
    Am I missing something?

  • edited April 2016 Answer ✓

    So this noLoop thing is still looping once.

    Yes but is normal behaviour for Processing. Try this sketch and you see it always loops once.

    void setup(){
      size(300,300);
      background(0); // black
      noLoop();
    }
    
    void draw(){
     background(255);
     fill(0);
     text(millis(), 20, 50);
    }
    

    The easiest solution is too check the frame count like this

    void setup(){
      size(300,300);
      background(0); // black
      noLoop();
    }
    
    void draw(){
     if(frameCount == 1) return; // Don't draw on the first frame
     background(255);
     fill(0);
     text(millis(), 20, 50);
     text(frameCount, 20, 70);
    }
    
    void mouseClicked(){
      redraw();
    }
    

    Applying this and my previous comments to my example above we get

    import g4p_controls.*;
    
    GWindow window1;
    float x0, y0, x1, y1;
    
    public void setup() {
      size(480, 320, JAVA2D);
      createGUI();
      window1.noLoop();
    }
    
    public void draw() {
      background(230);
    }
    
    void mouseReleased() {
      x0 = random(window1.width);
      y0 = random(window1.height);
      x1 = random(window1.width);
      y1 = random(window1.height);
      window1.redraw();
    }
    
    public void win_draw1(PApplet appc, GWinData data) {
      if (appc.frameCount <= 1) return;
      appc.background(230);
      appc.fill(0);
      appc.stroke(100, 200, 100);
      appc.strokeWeight(2);
      appc.line(x0, y0, x1, y1);
      appc.text(millis(), 10, 20);
      appc.text(appc.frameCount, 10, 40);
    }
    
    void createGUI() {
      G4P.messagesEnabled(false);
      G4P.setGlobalColorScheme(GCScheme.BLUE_SCHEME);
      G4P.setCursor(ARROW);
      surface.setTitle("Sketch Window");
      window1 = GWindow.getWindow(this, "Window title", 0, 0, 240, 120, JAVA2D);
      window1.addDrawHandler(this, "win_draw1");
    }
    
  • edited April 2016

    Oh wow that's much easier than a boolean. Derp.

    So you've completely solved my problem, but I just want to be thorough about what I'm saying what my problem here is.

    import g4p_controls.*;
    GWindow window1;
    int countA = 0;
    int countB = 0;
    
    public void setup() {
      createGUI();
      noLoop();
      window1.noLoop();
    }
    
    public void draw() {
      println("A " + countA + " " + frameCount); countA++;
    }
    
    public void win_draw1(PApplet appc, GWinData data) {
      println("B " + countB + " " + appc.frameCount); countB++;
    }
    
    void createGUI() {
      window1 = GWindow.getWindow(this, "Window", 0, 0, 100, 100, JAVA2D);
      window1.addDrawHandler(this, "win_draw1");
    }
    


    Prints:

    A 0 1
    B 0 1
    B 1 1
    


    B cycles twice using the same logic as main draw. That's confusing, that's all and nothing more.
    I am really enjoying this library. Thank you for your support and making it, Mr. quark. :)

  • What is interesting is that the frameCount is not incremented even though win_draw1() has been executed twice. I'll be honest I don't no why that is :\"> I would need to investigate further.

    It does not detract from the solution which is logically correct :)

  • Thank you for your assistance, it has been literally invaluable.

Sign In or Register to comment.