如何在处理java时使一个按钮来回改变背景颜色?

时间:2015-09-08 20:39:49

标签: java colors background processing background-color

在我的代码中,我试图制作一个按钮,将背景颜色从黑色更改为白色,再将黑色更改为白色。这是我的代码。我包含了所有代码,所以你可以看到。这不是为了学校或任何只是为了好玩。

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 个答案:

答案 0 :(得分:1)

draw()中使用布尔值作为标记,并在mousePressed中更改它,您可以说booleanX = !booleanX;这将改变布尔值。

这里:

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;
            }
    }