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;
}
}
太謝謝你了。真的有幫助。 – jhamer123
我的榮幸。 :) –