2014-02-28 28 views
0

我對編程相對比較陌生,我對VBA和matlab有一個非常基礎的知識,但對於我的uni設計項目,我需要生成一個GUI,因爲某些原因,我必須在處理中進行操作。我嘗試編輯示例按鈕以創建兩個按鈕,當您將鼠標懸停在它們上方時,它們會改變顏色,然後在單擊時更改背景顏色,但它不會執行任何操作。 腳本如下:用戶按鈕處理

PFont f; 
int playX, playY; 
int stopX, stopY; 
int buttonsize = 90; 
color playColor, stopColor; 
color playHighlight, stopHighlight; 
boolean playOver = false; 
boolean stopOver = false; 
int n, i; 

void setup() { 
    size(800,500); 
    f = createFont("Arial",16,true); // STEP 3 Create Font 
    playColor = color(0); 
    playHighlight = color(200); 
    stopColor = color(0); 
    stopHighlight = color(200); 
    playX = (width/4)-(buttonsize/2); 
    playY = 7*(height/10); 
    stopX = (3*(width/4))-(buttonsize/2); 
    stopY = 7*(height/10); 
    n = 0; 
    i = 0; 
} 

void draw() { 
    background(250); 
    textFont(f,16);     // STEP 4 Specify font to be used 
    fill(0);      // STEP 5 Specify font color 
    text("Set BPM:",width/10,2*(height/10)); 
    text("Choose File:",width/10,height/2); 

    if (playOver) { 
    fill(playHighlight); 
    } else { 
    fill(playColor); 
    } 
    stroke(0); 
    rect(playX, playY, buttonsize, buttonsize); 

    if (stopOver) { 
    fill(stopHighlight); 
    } else { 
    fill(stopColor); 
    } 
    stroke(0); 
    rect(stopX, stopY, buttonsize, buttonsize); 
} 

void update(int x, int y) { 
    if (overplay(playX, playY, buttonsize, buttonsize)) { 
    playOver = true; 
    stopOver = false; 
    } else if (overstop(stopX, stopY, buttonsize, buttonsize)) { 
    stopOver = true; 
    playOver = false; 
    } else { 
    stopOver = playOver = false; 
    } 
} 

void mousePressed() { 

    if (playOver) { 
    if (i == 0) { 
     i = 1; 
     playColor = color(255,0,0); 
    } else { 
     i = 0; 
     playColor = color(0); 
    } 
    } 


    if (stopOver) { 
    if (n == 0) { 
     n = 1; 
     stopColor = color(255,0,0); 
    } else { 
     n = 0; 
     stopColor = color(0); 
    } 
    } 
} 



boolean overplay(int x, int y, int width, int height) { 
    if (mouseX >= x && mouseX <= x+width && 
     mouseY >= y && mouseY <= y+height) { 
    return true; 
    } else { 
    return false; 
    } 
} 

boolean overstop(int x, int y,int width, int height) { 
    if (mouseX >= x && mouseX <= x+width && 
     mouseY >= y && mouseY <= y+height) { 
    return true; 
    } else { 
    return false; 
    } 
} 
+1

這是Java的?????????????你的主要類是哪裏? Listener類在哪裏? –

+0

@AdnanAhmadKhan這是[processing](http://processing.org)一個用java編寫的框架。預處理器將使主要和一切... –

回答

0

該代碼有點困惑。一件事你永遠不會打電話update(),這是基本的缺陷。將update(0,0)加入draw()之內,事情就會開始起作用:)。

您可以在update()調用中使用任何數字而不是0,因爲這些數字從未使用過,如果它們沒有使用,則更好。如果您需要將每個按鈕的參數傳遞給他們,那麼overStop()overPlay()又有什麼意義?我認爲你應該或者做一個獨特的isOver(x,y,xw,yw)並用每個按鈕參數或者isOverStop()來調用它,它不會得到任何參數...

那麼這裏做的更好的事情就是製作一個Button類。這將使事情變得更加清晰易於維護和編寫。

另注,您使用的in的方式可能更容易使他們布爾和使用符號:i = !i將它的toogle狀態。

編輯,示例代碼:

Button play; 


void setup() { 
    size(300, 300); 
    background(255); 
    play = new Button(50, 50, 30, 30); 
} 

void draw() { 
    background(255); 

    //display it... 
    play.display(); 

    // using the button... 
    if (play.isOn()) { 
    fill(200, 30, 30); 
    textSize(40); 
    text("I'm playing!", 50, 200); 
    } 
} 


void mouseClicked() { 
    //listen for clicks 
    play.update(); 
} 



class Button { 
    //coordinates 
    int x, y, w, h; 

    // state 
    boolean on = false; 

    //colors 
    color[] colors = new color[4]; 

    Button(int _x, int _y, int _w, int _h) { 

    //init everything 
    x = _x; 
    y = _y; 
    w = _w; 
    h = _h; 

    //create some colors 
    colors[0] = color(180); //off regular 
    colors[1] = color(100); // off over 
    colors[2] = color(180, 50, 180); // on 
    colors[3] = color(100, 50, 100); // on over 
    } 


    void display() { 
    // decide wich color to use 
    fill(colorEngine()); 

    noStroke(); 

    //draw 
    rect(x, y, w, h); 
    } 

    void update() { 
    // toogle state 
    if (isOver()) 
     on = !on; 
    } 

boolean isOn(){ 
    return on; 
} 

    color colorEngine() { 
    if (!on) { // if off... 
     if (!isOver()) { //and not over 
     return colors[0]; 
     } 
     else {//if off and over 
     return colors[1]; 
     } 
    } 
    else {// if on... 
     if (!isOver()) { //... and not over 
     return colors[2]; 
     } 
     else {// on and over 
     return colors[3]; 
     } 
    } 
    } 


    boolean isOver() { 
    return mouseX >= x && mouseX <= x+w && 
     mouseY >= y && mouseY <= y+h; 
    } 
}// 
+0

我沒有真正瞭解overStop()的東西等。我只是從處理軟件中的按鈕示例中複製它。使用我對編程的理解,我會想到有一個if函數,用於詢問鼠標x和y座標在哪裏等於按鈕的座標將會更好。 你是什麼意思做一個獨特的isOver?我怎麼做? –

+0

我的意思是如果你將座標傳遞給一個函數,那麼它可以被每個按鈕重用,所以沒有必要重複代碼......但是OOP是更好的方法。我用一個基本的按鈕類的示例代碼編輯了答案。檢查出來;) –

+0

也沒有理解複製和粘貼代碼沒有意義.... –