gpt4 book ai didi

java - java处理中如何一键来回改变背景颜色?

转载 作者:行者123 更新时间:2023-12-01 11:10:15 24 4
gpt4 key购买 nike

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

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

最佳答案

draw()中使用 boolean 值作为标志,并在mousePressed中更改它,你可以说booleanX = !booleanX;这将切换 boolean 值。

这里:

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

关于java - java处理中如何一键来回改变背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32466851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com