Colors 如何使用处理语言更改鼠标按下时的颜色
您好,对于这个程序,我必须创建3个按钮,当鼠标按下每个按钮时,颜色必须改变,我非常困惑于如何在使用处理语言按下鼠标时将按钮切换到不同的颜色,有什么提示吗??这是到目前为止我的代码 小部件主程序Colors 如何使用处理语言更改鼠标按下时的颜色,colors,widget,switch-statement,processing,Colors,Widget,Switch Statement,Processing,您好,对于这个程序,我必须创建3个按钮,当鼠标按下每个按钮时,颜色必须改变,我非常困惑于如何在使用处理语言按下鼠标时将按钮切换到不同的颜色,有什么提示吗??这是到目前为止我的代码 小部件主程序 PFont stdFont; final int EVENT_BUTTON1=1; final int EVENT_BUTTON2=2; final int EVENT_BUTTON3=3; final int EVENT_NULL=0; Widget widg
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;
}
}
PFont stdFont;
最终int事件1=1;
最终int事件按钮2=2;
最终int事件按钮3=3;
最终int事件_NULL=0;
Widget widget1、widget2、widget3;
ArrayList widgetList;
无效设置(){
stdFont=loadFont(“AbadiMT-constratedlight-48.vlw”);
文本字体(stdFont);
widget1=新的小部件(10010018040,
“红色”,颜色(100),
标准字体,事件按钮1);
widget2=新的小部件(10020018040,
“绿色”,颜色(100),
标准字体,事件按钮2);
尺寸(400400);
widget3=新的小部件(10030018040,
“蓝色”,颜色(100),
标准字体,事件按钮2);
尺寸(400400);
widgetList=newarraylist();
widgetList.add(widget1);widgetList.add(widget2);widgetList.add(widget3);
}
作废提款(){
for(int i=0;iy&&mY听起来像是家庭作业。一般答案:
class RectangularThing {
float x, y, w, h;
color boxColor = 0;
[...]
boolean over(float mx, float my) {
// return "true" if mx/my is inside this rectangle,
// otherwise return "false"
}
void setColor(color c) {
boxColor = c;
}
void draw() {
stroke(boxColor);
fill(boxColor);
rect(x,y,w,h);
}
}
[...]
ArrayList<RectangularThing> allmythings;
void setup() {
...
allmythings = new ArrayList<RectangularThing>();
...
}
void draw() {
// this kind of looping is the same as
// "for(int i=0, last=allmythings;i<last;i++) {
// RectangularThing t = allmythings[i];
// ..." except that it's less code and easier
// to read. If you're just iterating without
// needing the iteration counter, this is nicer.
for(RectangularThing t: allmythings) {
t.draw();
}
}
void mouseClicked() {
for(RectangularThing t: allmythings) {
if(t.over(mouseX,mouseY)) {
t.setColor(...);
}
}
redraw();
}
类矩形化{
浮动x,y,w,h;
color-boxColor=0;
[...]
布尔覆盖(浮点mx,浮点my){
//如果mx/my位于该矩形内,则返回“true”,
//否则返回“false”
}
无效设置颜色(颜色c){
boxColor=c;
}
作废提款(){
笔画(boxColor);
填充(boxColor);
rect(x,y,w,h);
}
}
[...]
ArrayList所有神话;
无效设置(){
...
allmythings=新的ArrayList();
...
}
作废提款(){
//这种循环与
//“对于(inti=0,last=allmythings;i来说,这听起来像是一个家庭作业。一般答案是:
class RectangularThing {
float x, y, w, h;
color boxColor = 0;
[...]
boolean over(float mx, float my) {
// return "true" if mx/my is inside this rectangle,
// otherwise return "false"
}
void setColor(color c) {
boxColor = c;
}
void draw() {
stroke(boxColor);
fill(boxColor);
rect(x,y,w,h);
}
}
[...]
ArrayList<RectangularThing> allmythings;
void setup() {
...
allmythings = new ArrayList<RectangularThing>();
...
}
void draw() {
// this kind of looping is the same as
// "for(int i=0, last=allmythings;i<last;i++) {
// RectangularThing t = allmythings[i];
// ..." except that it's less code and easier
// to read. If you're just iterating without
// needing the iteration counter, this is nicer.
for(RectangularThing t: allmythings) {
t.draw();
}
}
void mouseClicked() {
for(RectangularThing t: allmythings) {
if(t.over(mouseX,mouseY)) {
t.setColor(...);
}
}
redraw();
}
类矩形化{
浮动x,y,w,h;
color-boxColor=0;
[...]
布尔覆盖(浮点mx,浮点my){
//如果mx/my位于该矩形内,则返回“true”,
//否则返回“false”
}
无效设置颜色(颜色c){
boxColor=c;
}
作废提款(){
笔画(boxColor);
填充(boxColor);
rect(x,y,w,h);
}
}
[...]
ArrayList所有神话;
无效设置(){
...
allmythings=新的ArrayList();
...
}
作废提款(){
//这种循环与
//对于(int i=0,last=allmythings;i