Loading...
Logo
Processing Forum

Hi

I have the following code:
Copy code
  1. void setup()
    {
      size(400,400);
      background(255);
    }
    void draw()
    {
    if(((keyPressed == true)&(key =='a')))
    drawplease(50);
  2. if(((keyPressed == true)&&(key =='s')))
    drawplease(74);
  3. if(((keyPressed == true)&&(key =='d')))
    drawplease(98);
    }
  4. void drawplease(int x)
    {
      noStroke();
      fill(0);
    rect(x,278,24,31);
    }
    void keyReleased()
    {
      noStroke();
      fill(255,255,255);
      rect(50,278,24,31);
      rect(74,278,24,31);
      rect(98,278,24,31);
    }

What can I do in “void keyReleased()” which instead of writing all these lines of code:

rect(50,278,24,31);

  rect(74,278,24,31);

  rect(98,278,24,31);

Just write a brief code. Like using for loop or while or I don’t know function maybe!

But I want the exact result.
(the differences between x to x of each rectangle is 24)

thanks

Replies(4)

Hello, you can rewrite this in such way. Function drawRect takes initial coordinates of your rect, step and number of iterrations and draw it. 

void keyReleased()
{
      drawRect(50, 278,24,31,24,3); //24 - step, 3 - itterations
}

void drawRect (int p1, int p2, int p3, p4, int step, int iterrationsNumber)
{
      for (int i=0; i<iterrationsNumber; i=i+step)
      {
            rect(p1+step,p2,p3,p4);
      }   
}
You are trying to clear the black rect, when the button is released, right?
Instead of drawing white rects over your black rects, you could just paint the whole background white, in the process clearing all black rects you might have drawn:

Copy code
  1. void keyReleased() {
  2.    background(255,255,255);
  3. }

But it very much depends of what you are trying to do with the sketch. Maybe if you could describe what your goal is, there might be an even better solution.

can you please explain how to use it in my code? I couldn’t make it work :(

Actually yes, I want to clear the black rect when the button is released, but I purposely what to draw a white rect on it because of my original code.  I want the same black rect fill out with white and nowhere else .

If there is a way to undo the black rect after drawing it when the button released and not clear it(not drawing another rect on it), I’d rather that way. If there is a way please tell me how? If there is not please help me with this one.

This is what you are looking for:

Copy code
  1. void setup() {
  2.   size(400, 400);
  3.   background(255);
  4. }

  5. void draw() {
  6.   if (((keyPressed == true)&(key =='a'))) drawplease(50);
  7.   if (((keyPressed == true)&&(key =='s'))) drawplease(74);
  8.   if (((keyPressed == true)&&(key =='d'))) drawplease(98);  
  9. }

  10. void drawplease(int x) {
  11.   noStroke();
  12.   fill(0);
  13.   rect(x, 278, 24, 31);
  14. }

  15. void keyReleased() {
  16.   background(255); // this paints the whole stage white, thus erasing the black rect
  17. }
This code has the same effect as your original code. The background(255,255,255) does in fact "undo" the black rect by drawing over it. Test it out and see if this does the trick for you. If not, let us know.