如何在鼠标悬停在椭圆之一上时更改行和列的椭圆颜色?

huangapple go评论80阅读模式
英文:

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);
    }
  }
}
英文:
  1. 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&lt;10; row = row+1)
  {
    for (int col=0; col&lt;10; col = col+1)
    {
      ellipse(20 + col*40, 20 + row*40,30,30);
        if (mousePressed &amp;&amp; (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;

鼠标的位置存储在内置变量 mouseXmouseY 中。
椭圆排列成一个网格。测试鼠标的 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 &gt; cX-20 &amp;&amp; mouseX &lt; cX+20) || mouseY &gt; cY-20 &amp;&amp; mouseY &lt; 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 &lt; 10; row++) {
        int cY = 20 + row*40;
        for (int col=0; col &lt; 10; col++) {
            int cX = 20 + col*40;
            
            if ((mouseX &gt; cX-20 &amp;&amp; mouseX &lt; cX+20) || mouseY &gt; cY-20 &amp;&amp; mouseY &lt; cY+20) { 
                fill(255, 50, 50, 255);
            } else {
                fill(180, 50, 50, 100);
            }
            ellipse(cX, cY, 30, 30);
        }
    }
}

huangapple
  • 本文由 发表于 2020年9月18日 03:34:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63945085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定