How to use my own button in a second window ?

edited November 2014 in Questions about Code

Hello,

I know how to build a second window, how to build button with his own draw() … But how to do all in the same sketch ? Here is my code :

import java.awt.*;

PFrame f;
AutreApplet s;

void setup() {
  size(200, 200);
  s = new AutreApplet();
  f = new PFrame(s);
}

void draw() {
  background(255, 0, 0);
}

public class PFrame extends Frame {
  public PFrame(AutreApplet autre) {
    setBounds(100, 100, 200, 200);
    add(autre);
    autre.init();
    setVisible(true);
  }
}

public class AutreApplet extends PApplet {
  boolean temoin;
  Bouton b;

  void setup() {
    size(200, 200);
    b = new Bouton(this, 100, 100);
  }

  void draw() {
    background(0);
  }
}

public class Bouton {
  PApplet parent;
  PVector position;

  Bouton (PApplet _parent, int _x, int _y) {
    parent = _parent;
    position = new PVector(_x, _y);
    parent.registerMethod("draw", this);
  }

  void draw() {
    fill(255);
    rect(position.x, position.y, 40, 40);
  }
}

It seems to be a PApplet problem. The white rectangle appears sometime int the main red window. If you have got an idea to solve my problem it will be great.

Best regards.

Answers

  • Not exactly a solution, but a better way to get PApplet multi-instances: =:)
    http://forum.processing.org/two/discussion/7202/controlp5-controlwindow-and-controlp5frame

  • You do a great job, but I understood that my problem is the nested class. I will search some explanations later on the web cause my code works : import java.awt.*;

    PFrame f;
    AutreApplet s;
    
    void setup() {
      size(200, 200);
      s = new AutreApplet();
      f = new PFrame(s);
    }
    
    void draw() {
      background(255, 0, 0);
    }
    
    public class PFrame extends Frame {
      public PFrame(AutreApplet autre) {
        setBounds(100, 100, 200, 200);
        add(autre);
        autre.init();
        setVisible(true);
      }
    }
    
    public class AutreApplet extends PApplet {
      boolean temoin;
      Bouton b;
    
      void setup() {
        size(200, 200);
        b = new Bouton(this, 100, 100);
      }
    
      void draw() {
        background(0);
      }
    
      public class Bouton {
        PApplet parent;
        PVector position;
    
        Bouton (PApplet _parent, int _x, int _y) {
          parent = _parent;
          position = new PVector(_x, _y);
          parent.registerMethod("draw", this);
        }
    
        void draw() {
          fill(255);
          rect(position.x, position.y, 40, 40);
        }
      }
    }
    
  • edited November 2014

    Well, in my style we don't need to create a Frame. ;;)
    Anyways, no need to pass the AutreApplet's reference to Bouton. After all, Bouton is a nested inner class of it!

    public class Bouton {
      static final int DIAM = 40;
      static final color COLOUR = -1;
    
      final PVector pos = new PVector();
    
      Bouton (int x, int y) {
        pos.set(x, y);
        registerMethod("draw", this);
      }
    
      void draw() {
        fill(COLOUR);
        rect(pos.x, pos.y, DIAM, DIAM);
      }
    }
    
Sign In or Register to comment.