英文:
how do I change the colour of a row and column of the ellipses in response to the mouse being over one of the ellipses?
问题
void setup()
{
size(400, 400);
noStroke();
fill(180, 50, 50, 100);
}
void draw()
{
background(255);
for (int row = 0; row < 10; row = row + 1)
{
for (int col = 0; col < 10; col = col + 1)
{
ellipse(20 + col * 40, 20 + row * 40, 30, 30);
if (mousePressed && (mouseButton == LEFT))
fill(random(55));
ellipse(20 + col * 40, 20 + row * 40, 30, 30);
}
}
}
英文:
- I've built this sketch on processing where it generates ellipses on the grid. I wanted to know how I would change the color of a row and column when the mouse hovers over it.
void setup()
{
size(400,400);
noStroke();
fill(180,50, 50, 100);
}
void draw()
{
background(255);
for (int row=0; row<10; row = row+1)
{
for (int col=0; col<10; col = col+1)
{
ellipse(20 + col*40, 20 + row*40,30,30);
if (mousePressed && (mouseButton == LEFT))
fill (random (55));
ellipse(20 + col*40, 20 + row*40,30,30);
}
}
}
答案1
得分: 1
计算椭圆的中心点坐标:
int cY = 20 + row*40;
int cX = 20 + col*40;
鼠标的位置存储在内置变量 mouseX
和 mouseY
中。
椭圆排列成一个网格。测试鼠标的 x 坐标或 y 坐标是否在属于椭圆的网格区域内。根据测试结果设置颜色:
if ((mouseX > cX-20 && mouseX < cX+20) || mouseY > cY-20 && mouseY < cY+20) {
fill(255, 50, 50, 255);
} else {
fill(180, 50, 50, 100);
}
完整示例:
void setup() {
size(400,400);
noStroke();
}
void draw() {
background(255);
for (int row=0; row < 10; row++) {
int cY = 20 + row*40;
for (int col=0; col < 10; col++) {
int cX = 20 + col*40;
if ((mouseX > cX-20 && mouseX < cX+20) || mouseY > cY-20 && mouseY < cY+20) {
fill(255, 50, 50, 255);
} else {
fill(180, 50, 50, 100);
}
ellipse(cX, cY, 30, 30);
}
}
}
英文:
Compute the center point coordinates of the ellipse:
int cY = 20 + row*40;
int cX = 20 + col*40;
The position of the mouse is stored in the built-in variables mouseX
and mouseY
.
The ellipses are arranged in a grid. Test whether either the x- or the y-coordinate of the mouse is in the area of the grid that belongs to the ellipse. Set the color depending on the test result:
if ((mouseX > cX-20 && mouseX < cX+20) || mouseY > cY-20 && mouseY < cY+20) {
fill(255, 50, 50, 255);
} else {
fill(180, 50, 50, 100);
}
Complete example:
void setup() {
size(400,400);
noStroke();
}
void draw() {
background(255);
for (int row=0; row < 10; row++) {
int cY = 20 + row*40;
for (int col=0; col < 10; col++) {
int cX = 20 + col*40;
if ((mouseX > cX-20 && mouseX < cX+20) || mouseY > cY-20 && mouseY < cY+20) {
fill(255, 50, 50, 255);
} else {
fill(180, 50, 50, 100);
}
ellipse(cX, cY, 30, 30);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论