NCF参数化建筑论坛

标题: button 問題(急求解答 拜託了禮拜四之前) [打印本页]

作者: weiwei    时间: 2011-3-8 19:58
标题: button 問題(急求解答 拜託了禮拜四之前)
如圖,這次我想做個GUI介面的設計。想要在窗口上設定5個按鈕,當滑標靠近時可以按鈕變色,往下按時按鈕也可以變色。 但目前我所遇到的問題是,單一個設計按鈕是沒問題的!!但我嘗試使用 class寫出其他2個按鈕時,鼠標靠近變成沒有反應,是否有站友能幫我解決這問題(這是本次設計作業,拜託了!!!!)一併附上原始檔案 幫助修改。 [attach]15509[/attach] 概念圖 [attach]15510[/attach] 按鈕靠近時可以改變顏色 [attach]15511[/attach] 但寫出其他3個時就出現問題無法變色。 [attach]15512[/attach]
作者: claudemit    时间: 2011-3-9 05:26
改这样就可以了,不知道是不是你想要的效果 botton botton1 = new botton(150,300,20,20); botton botton2 = new botton(300,300,20,20); botton botton3 = new botton(200,300,20,20); void setup() { size(400,400); smooth(); background(255); } void draw() { botton1.drawbotton(); botton1.mousePressed(); botton1.mouseReleased(); botton1.mouseMoved(); botton2.drawbotton(); botton2.mousePressed(); botton2.mouseReleased(); botton2.mouseMoved(); botton3.drawbotton(); botton3.mousePressed(); botton3.mouseReleased(); botton3.mouseMoved(); }
作者: weiwei    时间: 2011-3-9 13:07
非常接近了,但是當鼠標按下按鈕時,應該也要變色,但目前也是沒反應。正在研究中...不知道是不是mousePressed,mouseMoved,mouseReleased這幾個bulit-in function的問題
作者: weiwei    时间: 2011-3-10 02:15
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); } }
作者: weiwei    时间: 2011-3-10 02:16
相信答案就在裡頭,但是目前是背景變色,我想要改成:::背景顏色固定,只有鼠標按下時,按鈕才變色。 請求站友幫忙
作者: holonking    时间: 2011-3-10 13:21
背景颜色固定的话把background(currentcolor)去掉嘛,否则跟着你的currentColor在变。 顺便解释一下,鼠标按下的反映叫“事件 (event)”,PApplet加载包括了鼠标和键盘等若干事件,如果想在自己的class实现事件反馈,百度一下java 里的 event。当然,如楼上利用papplet的事件调用程序也是正解




欢迎光临 NCF参数化建筑论坛 (http://bbs.ncf-china.com/) Powered by Discuz! X3.2