How to create button in Processing

How can use buttons in processing(java)

Hi

Many ways to do that … Give more details what you want to achieve

1 Like

import processing.core.PApplet;
import android.widget.Button;
import android.widget.TextView;

public class Core extends PApplet {
private Button button;
public Core() {
}

public void $CreateWindow(int width,
int height,
java.lang.String renderer,
java.lang.String path) {
}

public void $button(float x, float y, float w, float h) {

}
}
How to create button with class a Button?

There is an example of Android widgets here: https://github.com/vsquared/AndroidWidgets_Processing4

Your original post was for ‘processing(java)’ but your code example is for Android widgets. The approaches are specific to each, so you should be more clear about which one you want.

2 Likes

for ‘processing(java)’

Button[] myButtons=new Button[10]; // make a button of type class Button ( just for ref )
int cmd = -1;

// ---------------------------------------------------------------------------------------

void setup() {
  size(768, 900);

  String[] names= {
    "moving block at the top",

    "1",
    "2",
    "3",

    "4",
    "5",
    "6",

    "7",
    "8",
    "9",
  };

  strokeWeight(3);
  textSize(14);
  for (int i=0; i < 10; i++) {
    //parameters: xi, yi, wi, hi, seli, visible, text, id
    myButtons[i] = new Button(600, 20 + i * 55,
      130, 50,
      false, true,
      names[i],
      i);
  }
}

void draw() {
  background(200, 200, 0);
  for (Button btn : myButtons) {
    btn.draw();
  }
  fill(0);
  text(cmd, 28, 28);
}

// ---------------------------------------------------------------------------------------

void mousePressed() {
  for (Button btn : myButtons) {
    btn.sel=false;
    if ( btn.myMousePressed()) {
      cmd = btn.id;
      btn.sel=true;
    }
  }//for
}

// ===================================================================================

class Button {
  int x, y,
    w, h,
    id;
  boolean sel, vis;
  String text;

  // constr
  Button(int x, int y,
    int w, int h,
    boolean sel, boolean vis,
    String atext,
    int id) {
    //
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.sel = sel;
    this.vis = vis;
    this.text = atext;
    this.id = id;
  }// constr

  void  draw() {
    if (vis) {
      strokeWeight(3);

      if (sel)         fill(0, 200, 0);
      else             fill(0, 0, 200);

      if (over())      stroke(200, 0, 200);
      else             stroke(0, 200, 200);

      rect(x, y,
        w, h);
      noStroke();
      fill(200);
      textSize(14);
      textAlign(LEFT, TOP);
      text(text,
        x + 4, y + 3,
        w-10, 900);
    }
  } // method

  boolean over() {
    return
      mouseX > x &
      mouseX < x + w &
      mouseY > y &
      mouseY < y + h;
  } // method

  boolean myMousePressed() {
    //called from main mousePressed
    return
      over();
  } // method
  //
} // class
//

1 Like

Hi
processing(java) or Android widgets ?
For processing (java) here is More examples

With @svan link and this link widgets could be more explained