简体   繁体   中英

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.

Alternately, you can add each rectangle to a list, and then draw all of the rectangles every frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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