Applet use in nested classes

Hello all. As an exercise in becoming more proficient at Processing, I’ve been trying to make my own GUI instead of using one of the great already-made libraries like CP5. I’ve been doing well enough, but am getting hung up on calling nested functions which revolve around calling PApplet. What I’m aiming to do is create a button class which has registerMethod mouse events, instead of using the much easier mousePressed() calls in the main sketch. What I’d like is to be able to place the buttons inside another class, and call that class from the main.
I know this makes things hard for me, but this is an exercise to push my boundaries a bit.
Here is some very sanitized code for what I’m trying to get. If it helps, the actual application will be to be able to have a servo controller package class (one slider, 2 buttons) that I can call any number of times and manipulate each.

Sorry about the format, I cannot for the life of me find the code format button
Also: I also posted this on the old forums, hope that’s OK.

PackServ tryMe;
void setup(){
  size(200,150);
  tryMe = new PackServ();
}

void draw(){
  background (250);
  fill(25,200,10);
  tryMe.Add(this);
}
public class PackServ{
  PApplet SlApp;
  Butt Butt1, Butt2;

Boolean moveUnit=false;
String theName;
int pkX, pkY, pkW;

void Add(PApplet pappy) {
  SlApp = pappy;
  theName = "The Slider";
  pkX = 20; pkY = 20; pkW = 100;  
  Butt1 = new Butt (pappy);
  Butt2 = new Butt (pappy);
  drawIt();
}

void drawIt() {
  fill(200); strokeWeight(1);
  rect(pkX,pkY,pkW,60,5);
  Butt1.Add("but1", pkX+25, pkY+25, 25);
  Butt2.Add("but2", pkX+75, pkY+25, 25);
 }
} // Pau Class

public class Butt
{
  PApplet butApplet;
  int theX, theY, theW, theH, bRad;
  boolean togStat = false, pressed = false;
  boolean onClick = false;
  color n;                             
    color h = color(240, 210, 10);         
    color f = color (40, 90, 190);          
    color a = color (240, 20, 20);           
    color c = color (250,150,50);          
 int oBb, li;
 String theLabel;

 Butt (PApplet kid) {
   butApplet = kid;
   butApplet.registerMethod("mouseEvent", this);
    n = f;
  }
 void Add(String _myName, int x, int y, int siz) {
   theLabel = _myName;
   theX = x;   theY = y; bRad = siz;
   fill(75); textSize(9);
   buttonLook();
    }
   
 boolean overB(int mx, int my) {
    if (dist (mx, my, theX, theY) <= bRad){
      return true;
    } else {
      return false;      }
   }
void buttonLook(){
   if (overB(mouseX,mouseY) && pressed) {
    n = c; oBb = 3; li = 100;
      }
   else if (overB(mouseX,mouseY)) {
         text(theLabel, theX-10, theY+25);   
         n = h;
      }
   else {n = f; oBb=1; li=25;  }
   strokeWeight(oBb); stroke(li); fill(n);
       ellipse(theX, theY, bRad, bRad);
  }
void mouseEvent(MouseEvent e) {
  pressed = false;
  int mx = e.getX(), my = e.getY();   
  switch (e.getAction()) {
    case MouseEvent.PRESS:
      pressed = overB(mx,my);
      break;
    }
  }
}        // Pau class
1 Like

Notice:

void drawIt() {
  SlApp.fill(200); 
  SlApp.strokeWeight(1);
  SlApp.rect(pkX,pkY,pkW,60,5);
  Butt1.Add("but1", pkX+25, pkY+25, 25);
  Butt2.Add("but2", pkX+75, pkY+25, 25);
 }

For this exrcise, you should read this: https://github.com/processing/processing/wiki/Library-Basics as it should help.

Kf

1 Like

Thanks for the fast reply. I’ve looked the link over a few times -that’s where I got most of the mouseEvent coding ideas. Should I be playing most attention to the section towards the bottom about Adding Your Own Library Events? That is, do I need to format everything in my object class in this manner? Or is it more of a naming issue akin to the top basic model? I’ve tried copying that, but to no avail. Additionally, I added a direct Button to the main sketch to see functionality, and it works as intended (button changes color when pressed):

PackServ tryMe;
Butt noClass;

void setup(){
  size(200,150);
  tryMe = new PackServ();
  noClass = new Butt(this);
}

void draw(){
  background (250);
  fill(25,200,10);
  tryMe.Add(this);
  noClass.Add("NoClass", 40, 100,20);
}

I played around with it a bit, and after some trial and error and help, I got this working. It’s not my end product, but a good first step. I thought I’d share in case someone struggles with this down the line. I’m sure I’ll be back later for more clarification. Thanks kfrajer.

PackServ p1;
Butt b1;

public void setup() {
  size(200, 150);
  b1 = new Butt(this);
  p1 = new PackServ(this);
}

public void draw() {
  background (250);
  p1.Add(20, 10, 50);
    line(100,10,100,100);
    fill(130,255,170);
    rect(120,10,50,60,5);
 
  b1.Add("B2", 145,35,25);
    stroke(20); textSize(14); fill(25); textAlign(CENTER);
    text("single", 145,100);   text("package", 45,100);
  }

public class PackServ{
  PApplet SerApp;
  Butt Butt1;
  int pkX, pkY, pkW;

  PackServ(PApplet papp) {
   this.SerApp = papp;
    pkX = 20; pkY = 20; pkW = 100;
   Butt1 = new Butt(SerApp);
  }

  void Add(int _x, int _y, int _w) {
    pkX = _x; pkY = _y; pkW = _w;  
    drawIt();
  }
  
  void drawIt() {
    SerApp.fill(220); 
    SerApp.strokeWeight(1);
    SerApp.rect(pkX, pkY, pkW, 60, 5);
     Butt1.Add("B1", pkX+25, pkY+25, 25);
  }
} 

public class Butt {
  PApplet butApp;
  int theX, theY, theW, theH, bRad;
  boolean pressed = false;
  color n;                             
  color h = color(240, 210, 10);         
  color f = color (40, 90, 190);          
  color a = color (240, 20, 20);           
  int oBb, li;
  String theLabel;

  Butt (PApplet butp) {
    this.butApp = butp;
    butApp.registerMethod("mouseEvent",this);
    n = f;
  }
  void Add(String _myName, int x, int y, int siz) {
    theLabel = _myName;
    theX = x;   theY = y;  bRad = siz;
    fill(75);   textSize(9); 
    buttonLook();
  }
boolean overB(int mx, int my) {
    if (dist (mx, my, theX, theY) <= bRad) {
      return true;
    } else {
      return false;
    }
  }
  void buttonLook() {
    if (overB(mouseX, mouseY) && pressed) {
      n = a; 
      oBb = 3; li = 100;
    } else if (overB(mouseX, mouseY)) {
      text(theLabel, theX-10, theY+25);   
      n = h;
    } else {
      n = f; oBb=1; li=25;
    }
    strokeWeight(oBb); 
    stroke(li); 
    fill(n);
    ellipse(theX, theY, bRad, bRad);
    strokeWeight(1);
  }
  void mouseEvent(MouseEvent e) {
    pressed = false;
    int mx = e.getX(), my = e.getY();   
    switch (e.getAction()) {
    case MouseEvent.PRESS:
      pressed = overB(mx, my);
      break;
    }
  }
}