请查看处理按钮示例。
它没有封装成一个类,这取决于您的实现,
但它确实演示了如何使用Processing内置的draw()
和mousePressed()
方法来处理事件。
如果有帮助的话,这是我在一些我的草图中使用的按钮类:
int numButtons = 3;
Button[] buttons = new Button[numButtons];
void setup(){
size(180,200);
int buttonWidth = 150;
int buttonHeight = 50;
int buttonSpacing = 15;
for(int i = 0 ; i < numButtons; i++){
buttons[i] = new Button("button "+(i+1),buttonSpacing,(buttonHeight+buttonSpacing) * i,buttonWidth,buttonHeight);
}
}
void draw(){
background(255);
for(int i = 0 ; i < numButtons; i++){
buttons[i].update(mouseX, mouseY, mousePressed);
buttons[i].draw();
}
}
void onButtonClicked(Button b){
println(b.label,"was pressed at",new java.util.Date());
}
class Button{
float w,h,x,y;
color bg = color(200);
color fg = color(0);
String label;
boolean isOver,wasPressed;
int pw = 10;
Button(String label,float x,float y,float w,float h){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
}
void update(int mx,int my,boolean md){
isOver = ((mx >= x && mx <= (x+w))&&(my >= y && my <= (y+h)));
if(isOver && md){
if(!wasPressed){
onButtonClicked(this);
wasPressed = true;
}
}else wasPressed = false;
}
void draw(){
pushStyle();
noStroke();
fill(isOver ? fg : bg);
rect(x,y,w,h);
fill(isOver ? bg : fg);
text(label,x+pw,y+h*.75);
popStyle();
}
}
你可能需要以不同的方式实现它,但希望这能给你一些想法。
更新
如果感觉回调是你主要关注的事情。
如果是这样,一个选项是使用Java反射来调用方法。
这是上面Button类的修改版本,它接受包含回调方法和回调方法名称的类作为参数:
import java.lang.reflect.*;
int numButtons = 3;
Button[] buttons = new Button[numButtons];
void setup(){
size(180,200);
int buttonWidth = 150;
int buttonHeight = 50;
int buttonSpacing = 15;
for(int i = 0 ; i < numButtons; i++){
buttons[i] = new Button(this,"onButton"+(i+1)+"Clicked","button "+(i+1),buttonSpacing,(buttonHeight+buttonSpacing) * i,buttonWidth,buttonHeight);
}
}
void draw(){
background(255);
for(int i = 0 ; i < numButtons; i++){
buttons[i].update(mouseX, mouseY, mousePressed);
buttons[i].draw();
}
}
public void onButton1Clicked(){
println("button 1 clicked");
}
public void onButton2Clicked(){
println("button 2 clicked");
}
public void onButton3Clicked(){
println("button 3 clicked");
}
class Button{
float w,h,x,y;
color bg = color(200);
color fg = color(0);
String label;
boolean isOver,wasPressed;
int pw = 10;
Object parent;
Method callback;
Button(Object parent,String callbackName,String label,float x,float y,float w,float h){
this.parent = parent;
try{
callback = parent.getClass().getMethod(callbackName);
}catch(Exception e){
e.printStackTrace();
}
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.label = label;
}
void update(int mx,int my,boolean md){
isOver = ((mx >= x && mx <= (x+w))&&(my >= y && my <= (y+h)));
if(isOver && md){
if(!wasPressed){
if(callback != null){
try{
callback.invoke(parent);
}catch(Exception e){
e.printStackTrace();
}
}
wasPressed = true;
}
}else wasPressed = false;
}
void draw(){
pushStyle();
noStroke();
fill(isOver ? fg : bg);
rect(x,y,w,h);
fill(isOver ? bg : fg);
text(label,x+pw,y+h*.75);
popStyle();
}
}
这是解决问题的一种方式。如果您需要为这些回调函数提供参数,请务必深入了解Java反射和Method类。