|
4m
楼主 |
发表于 2011-3-10 02:15:29
|
只看该作者
color currentColor;//按鈕當前顏色
color buttonColor = color(50);//按鈕預設顏色
color highlight = color(255);//滑鼠移過去時的顏色(白)
circlebutton button1,button2,button3,button4,button5;
boolean locked = false;//預設開關一開始是關閉的
void setup(){
size(400,400);
smooth();
ellipseMode(CENTER);
buttonColor = color(30);
button1 = new circlebutton(30, 200, 20, buttonColor, highlight);
buttonColor = color(50);
button2 = new circlebutton(60, 200, 20, buttonColor, highlight);
buttonColor = color(60);
button3 = new circlebutton(90, 200, 20, buttonColor, highlight);
buttonColor = color(80);
button4 = new circlebutton(120, 200, 20, buttonColor, highlight);
buttonColor = color(200);
button5 = new circlebutton(150, 200, 20, buttonColor, highlight);
}
void draw(){
background(currentColor);
stroke(255);
update(mouseX,mouseY);//注意update 沒有在class裡面
button1.display();
button2.display();
button3.display();
button4.display();
button5.display();
}
void update(int x,int y){
if(locked == false){
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
}
else{
locked = false;
}
if(mousePressed){
if(button1.pressed()){
currentColor = button1.buttonColor;
}
else if(button2.pressed()){
currentColor = button2.buttonColor;
}
else if(button3.pressed()){
currentColor = button3.buttonColor;
}
else if(button4.pressed()){
currentColor = button4.buttonColor;
}
else if(button5.pressed()){
currentColor = button5.buttonColor;
}
}
}
class button{
int x,y;
int bsize;
color buttonColor;
color highlightcolor;
color currentcolor;
boolean over = false;
boolean passed = false;
//我是true時就update
void update(){
if(over()){
currentcolor = highlightcolor;
}
else{
currentcolor = buttonColor;
}
}
boolean pressed(){
if(over){
locked = true;
return true;
}
else{
locked = false;
return false;
}
}
boolean over(){
return true;
}
boolean overCircle(int x,int y,int diameter){
float disX = x - mouseX;
float disY = y - mouseY;
//以disX,disY為中點畫圓r位半徑
if(sqrt(sq(disX)+sq(disY)) < diameter/2){
return true;
}
else{
return false;
}
}
}
class circlebutton extends button{
circlebutton(int x,int y,int bsize,color buttonColor,color highlightcolor){
this.x = x;
this.y = y;
this.bsize = bsize;
this.buttonColor = buttonColor;
this.highlightcolor = highlightcolor;
}
boolean over(){
if(overCircle(x,y,bsize)){
over = true;
return true;
}
else{
over = false;
return false;
}
}
void display(){
stroke(0);
fill(currentcolor);
ellipse(x,y,bsize,bsize);
}
} |
|