在Processing中有没有更简洁的方法来交替使用Java形状和背景颜色?
Is there a more concise way to alternate shape and background colour with Java in Processing?
在Processing中,当鼠标悬停在矩形内时,我需要window中间的矩形显示为紫色,背景为黄色。当鼠标在屏幕上的其他任何地方(触摸背景)时,矩形应该是黄色的,背景应该是紫色的。我已经实现了我的目标,但我想知道是否有更简洁的方法来编写这段代码,或者是否有其他方法?
void setup ()
{
size(500, 500);
}
void draw()
{
background(205, 86, 219); // purple
rectMode(CENTER);
fill(231, 240, 111); // yellow
rect(250, 250, 250, 200);
if (mouseX >= 125 && mouseX <= 375 && mouseY >= 100 && mouseY <= 300)
{
background(231, 240, 111); // yellow
fill(205, 86, 219); // purple
rect(250, 250, 250, 200);
}
}
使用 2 种颜色创建 2 color
个对象:
color color1 = color(231, 240, 111);
color color2 = color(205, 86, 219);
当鼠标在矩形上时设置一个boolean
变量:
boolean onRect = mouseX >= 125 && mouseX <= 375 && mouseY >= 100 && mouseY <= 300;
使用ternary (?:
) operator到select的颜色取决于onRect
的状态:
background(onRect ? color1 : color2);
fill(onRect ? color2 : color1);
完整示例:
color color1 = color(231, 240, 111);
color color2 = color(205, 86, 219);
void setup () {
size(500, 500);
}
void draw() {
boolean onRect = mouseX >= 125 && mouseX <= 375 && mouseY >= 100 && mouseY <= 300;
background(onRect ? color1 : color2);
rectMode(CENTER);
fill(onRect ? color2 : color1);
rect(250, 250, 250, 200);
}
在Processing中,当鼠标悬停在矩形内时,我需要window中间的矩形显示为紫色,背景为黄色。当鼠标在屏幕上的其他任何地方(触摸背景)时,矩形应该是黄色的,背景应该是紫色的。我已经实现了我的目标,但我想知道是否有更简洁的方法来编写这段代码,或者是否有其他方法?
void setup ()
{
size(500, 500);
}
void draw()
{
background(205, 86, 219); // purple
rectMode(CENTER);
fill(231, 240, 111); // yellow
rect(250, 250, 250, 200);
if (mouseX >= 125 && mouseX <= 375 && mouseY >= 100 && mouseY <= 300)
{
background(231, 240, 111); // yellow
fill(205, 86, 219); // purple
rect(250, 250, 250, 200);
}
}
使用 2 种颜色创建 2 color
个对象:
color color1 = color(231, 240, 111);
color color2 = color(205, 86, 219);
当鼠标在矩形上时设置一个boolean
变量:
boolean onRect = mouseX >= 125 && mouseX <= 375 && mouseY >= 100 && mouseY <= 300;
使用ternary (?:
) operator到select的颜色取决于onRect
的状态:
background(onRect ? color1 : color2);
fill(onRect ? color2 : color1);
完整示例:
color color1 = color(231, 240, 111);
color color2 = color(205, 86, 219);
void setup () {
size(500, 500);
}
void draw() {
boolean onRect = mouseX >= 125 && mouseX <= 375 && mouseY >= 100 && mouseY <= 300;
background(onRect ? color1 : color2);
rectMode(CENTER);
fill(onRect ? color2 : color1);
rect(250, 250, 250, 200);
}