您好,所以对于程序,我必须创建 3 个按钮,并且当鼠标按下每个按钮时,颜色必须改变,我非常纠结于如何在使用处理按下鼠标时将按钮切换为不同的颜色语言,有什么建议吗??到目前为止,这是我的代码!
小部件的主程序
PFont stdFont;
final int EVENT_BUTTON1=1;
final int EVENT_BUTTON2=2;
final int EVENT_BUTTON3=3;
final int EVENT_NULL=0;
Widget widget1, widget2,widget3;
ArrayList widgetList;
void setup(){
stdFont=loadFont("AbadiMT-CondensedLight-48.vlw");
textFont(stdFont);
widget1=new Widget(100, 100, 180, 40,
"RED", color(100),
stdFont, EVENT_BUTTON1);
widget2=new Widget(100, 200, 180, 40,
"GREEN", color(100),
stdFont, EVENT_BUTTON2);
size(400, 400);
widget3=new Widget(100, 300, 180, 40,
"BLUE", color(100),
stdFont, EVENT_BUTTON2);
size(400, 400);
widgetList = new ArrayList();
widgetList.add(widget1); widgetList.add(widget2); widgetList.add(widget3);
}
void draw(){
for(int i = 0; i<widgetList.size(); i++){
Widget aWidget = (Widget)widgetList.get(i);
aWidget.draw();
}
}
void mousePressed(){
int event;
for(int i = 0; i<widgetList.size(); i++){
Widget aWidget = (Widget) widgetList.get(i);
event = aWidget.getEvent(mouseX,mouseY);
switch(event) {
case EVENT_BUTTON1:
println("button 1!");
break;
case EVENT_BUTTON2:
println("button 2!");
break;
case EVENT_BUTTON3:
println("button 3!");
break;
}
}
}
main widget class
class Widget {
int x, y, width, height;
String label; int event;
color widgetColor, labelColor;
PFont widgetFont;
Widget(int x,int y, int width, int height, String label,
color widgetColor, PFont widgetFont, int event){
this.x=x; this.y=y; this.width = width; this.height= height;
this.label=label; this.event=event;
this.widgetColor=widgetColor; this.widgetFont=widgetFont;
labelColor= color(0);
}
void draw(){
fill(widgetColor);
rect(x,y,width,height);
fill(labelColor);
text(label, x+10, y+height-10);
}
int getEvent(int mX, int mY){
if(mX>x && mX < x+width && mY >y && mY <y+height){
return event;
}
return EVENT_NULL;
}
}