简体   繁体   English

每次在特定区域单击鼠标时如何绘制矩形?

[英]How do I draw a rectangle every time the mouse is clicked in a certain area?

I'm programming in Processing, and trying to get a rectangle to appear at the location of the mouse and stay there every time the mouse is clicked. 我正在处理中编程,试图使矩形显示在鼠标的位置,并在每次单击鼠标时停留在该位置。 However, when I run the program and click the mouse, the rectangle only stays for second, before it disappears. 但是,当我运行该程序并单击鼠标时,该矩形仅停留第二秒钟,然后消失。 Is there another way to write it so that the rectangle isn't dependent on the mouse being clicked to exist? 还有另一种书写方式,以使矩形不依赖于单击的鼠标是否存在吗?

Here is my code: 这是我的代码:

void setup()
{
   size(250, 350); 
}

void draw()
{
  background(255);
  fill(255);
  tileAp();
}

void tileAp()
{
  fill(0);
  if(mousePressed && mouseX <= 250 && mouseX >= 0 && mouseY >= 0 && mouseY <= 250)
  {

      drawM(true);
  }
  else
  {

  }
}     
void drawM(boolean b)
{
  if(b == true)
  {  
    rect(mouseX, mouseY, 25, 25);
  }
}   

You can stop calling background(255) in your draw method - this should allow the rectangles to stay. 您可以停止在draw方法中调用background(255) -这应该允许矩形停留。

Alternately, you can add each rectangle to a list, and then draw all of the rectangles every frame. 或者,您可以将每个矩形添加到列表中,然后每帧绘制所有矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM