2015-09-08 117 views
0

在我的代碼中,我試圖讓一個按鈕將背景顏色從黑色變成白色變成黑色變成白色。這是我的代碼。我包含了所有你可以看到的代碼。這不適用於學校或任何娛樂活動。如何讓一個按鈕在處理java中來回改變背景顏色?

int circleX, circleY; // Position of circle button 
int circleSize = 93; // Diameter of circle 
color circleColor, baseColor; 
color circleHighlight; 
color currentColor; 
boolean circleOver = false; 
int start; 
int m = 0; 

void setup() { 
start = millis(); 
size(640, 360); 
circleColor = color(255); 
circleHighlight = color(204); 
baseColor = color(102); 
currentColor = baseColor; 
circleX = width/2+circleSize/2+10; 
circleY = height/2; 
ellipseMode(CENTER); 
} 

void draw() { 

update(mouseX, mouseY); 
background(currentColor); 

if (circleOver) { 
fill(circleHighlight); 
} else { 
fill(circleColor); 
} 
stroke(0); 
ellipse(circleX, circleY, circleSize, circleSize); 

int timer = millis()-start; 
fill(0, 102, 153); 
textSize(40); 
text(timer, 40, 40); 
textSize(20); 
text("milliseconds", 200,40); 

fill(0,102,153); 
    textSize(40); 
    text(m,400,40); 
    textSize(20); 
    text("hits", 450,40); 

} 

void update(int x, int y) { 
if (overCircle(circleX, circleY, circleSize)) { 
circleOver = true; 
} else { 
circleOver = false; 
} 
} 

void mousePressed() { 
if (circleOver) { 
currentColor = circleColor; 
m = m + 1; 
} 
} 

boolean overCircle(int x, int y, int diameter) { 
float disX = x - mouseX; 
float disY = y - mouseY; 
if (sqrt(sq(disX) + sq(disY)) < diameter/2) { 
return true; 
} else { 
return false; 
} 
} 

回答

1

draw()使用布爾作爲標誌,並改變它在mousePressed你可以說booleanX = !booleanX;這將在的toogle布爾值。

這裏:

int circleX, circleY; 

// Position of circle button 
int circleSize = 93; 

    // Diameter of circle 
color circleColor, baseColor; 
color circleHighlight; 
color currentColor; 

boolean color1 = false; 

int start;int m = 0; 


void setup() { 
    start = millis(); 
    size(640, 360); 
    circleColor = color(255); 
    circleHighlight = color(204); 
    baseColor = color(102); 
    currentColor = baseColor; 
    circleX = width/2+circleSize/2+10; 
    circleY = height/2; 
    ellipseMode(CENTER); 
    } 


void draw() { 
    if (color1){ 
     currentColor = color(255); 
    }else{ 
     currentColor = color(85); 
    } 
    background(currentColor); 

    if (overCircle(circleX, circleY, circleSize)) { 
    fill(circleHighlight);} else { 
    fill(circleColor);} 
    stroke(0); 
    ellipse(circleX, circleY, circleSize, circleSize); 
    int timer = millis()-start; 
    fill(0, 102, 153); 
    textSize(40); 
    text(timer, 40, 40); 
    textSize(20); 
    text("milliseconds", 200,40); 

    fill(0,102,153); 
    textSize(40); 
    text(m,400,40); 
    textSize(20); 
    text("hits", 450,40); 
} 


void mousePressed() { 
    if (overCircle(circleX, circleY, circleSize)) { 
     color1 = !color1; 
     m = m + 1; 
     } 
    } 

boolean overCircle(int x, int y, int diameter) { 
    float disX = x - mouseX; 
    float disY = y - mouseY; 
    if (sqrt(sq(disX) + sq(disY)) < diameter/2) { 
     return true; 
     } else { 
      return false; 
      } 
    } 
+0

太謝謝你了。真的有幫助。 – jhamer123

+0

我的榮幸。 :) –